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) = ip_hdr(skb)->daddr)
52 #define dnat_took_place(skb) (skb_origaddr(skb) != ip_hdr(skb)->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 static inline __be16
vlan_proto(const struct sk_buff
*skb
)
66 return vlan_eth_hdr(skb
)->h_vlan_encapsulated_proto
;
69 #define IS_VLAN_IP(skb) \
70 (skb->protocol == htons(ETH_P_8021Q) && \
71 vlan_proto(skb) == htons(ETH_P_IP) && \
72 brnf_filter_vlan_tagged)
74 #define IS_VLAN_IPV6(skb) \
75 (skb->protocol == htons(ETH_P_8021Q) && \
76 vlan_proto(skb) == htons(ETH_P_IPV6) &&\
77 brnf_filter_vlan_tagged)
79 #define IS_VLAN_ARP(skb) \
80 (skb->protocol == htons(ETH_P_8021Q) && \
81 vlan_proto(skb) == htons(ETH_P_ARP) && \
82 brnf_filter_vlan_tagged)
84 /* We need these fake structures to make netfilter happy --
85 * lots of places assume that skb->dst != NULL, which isn't
86 * all that unreasonable.
88 * Currently, we fill in the PMTU entry because netfilter
89 * refragmentation needs it, and the rt_flags entry because
90 * ipt_REJECT needs it. Future netfilter modules might
91 * require us to fill additional fields. */
92 static struct net_device __fake_net_device
= {
93 .hard_header_len
= ETH_HLEN
96 static struct rtable __fake_rtable
= {
99 .__refcnt
= ATOMIC_INIT(1),
100 .dev
= &__fake_net_device
,
101 .path
= &__fake_rtable
.u
.dst
,
102 .metrics
= {[RTAX_MTU
- 1] = 1500},
109 static inline struct net_device
*bridge_parent(const struct net_device
*dev
)
111 struct net_bridge_port
*port
= rcu_dereference(dev
->br_port
);
113 return port
? port
->br
->dev
: NULL
;
116 static inline struct nf_bridge_info
*nf_bridge_alloc(struct sk_buff
*skb
)
118 skb
->nf_bridge
= kzalloc(sizeof(struct nf_bridge_info
), GFP_ATOMIC
);
119 if (likely(skb
->nf_bridge
))
120 atomic_set(&(skb
->nf_bridge
->use
), 1);
122 return skb
->nf_bridge
;
125 static inline void nf_bridge_save_header(struct sk_buff
*skb
)
127 int header_size
= ETH_HLEN
;
129 if (skb
->protocol
== htons(ETH_P_8021Q
))
130 header_size
+= VLAN_HLEN
;
132 skb_copy_from_linear_data_offset(skb
, -header_size
,
133 skb
->nf_bridge
->data
, header_size
);
137 * When forwarding bridge frames, we save a copy of the original
138 * header before processing.
140 int nf_bridge_copy_header(struct sk_buff
*skb
)
143 int header_size
= ETH_HLEN
;
145 if (skb
->protocol
== htons(ETH_P_8021Q
))
146 header_size
+= VLAN_HLEN
;
148 err
= skb_cow(skb
, header_size
);
152 memcpy(skb
->data
- header_size
, skb
->nf_bridge
->data
, header_size
);
154 if (skb
->protocol
== htons(ETH_P_8021Q
))
155 __skb_push(skb
, VLAN_HLEN
);
159 /* PF_BRIDGE/PRE_ROUTING *********************************************/
160 /* Undo the changes made for ip6tables PREROUTING and continue the
161 * bridge PRE_ROUTING hook. */
162 static int br_nf_pre_routing_finish_ipv6(struct sk_buff
*skb
)
164 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
166 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
167 skb
->pkt_type
= PACKET_OTHERHOST
;
168 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
170 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
172 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
175 skb
->dev
= nf_bridge
->physindev
;
176 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
177 skb_push(skb
, VLAN_HLEN
);
178 skb
->network_header
-= VLAN_HLEN
;
180 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
181 br_handle_frame_finish
, 1);
186 static void __br_dnat_complain(void)
188 static unsigned long last_complaint
;
190 if (jiffies
- last_complaint
>= 5 * HZ
) {
191 printk(KERN_WARNING
"Performing cross-bridge DNAT requires IP "
192 "forwarding to be enabled\n");
193 last_complaint
= jiffies
;
197 /* This requires some explaining. If DNAT has taken place,
198 * we will need to fix up the destination Ethernet address,
199 * and this is a tricky process.
201 * There are two cases to consider:
202 * 1. The packet was DNAT'ed to a device in the same bridge
203 * port group as it was received on. We can still bridge
205 * 2. The packet was DNAT'ed to a different device, either
206 * a non-bridged device or another bridge port group.
207 * The packet will need to be routed.
209 * The correct way of distinguishing between these two cases is to
210 * call ip_route_input() and to look at skb->dst->dev, which is
211 * changed to the destination device if ip_route_input() succeeds.
213 * Let us first consider the case that ip_route_input() succeeds:
215 * If skb->dst->dev equals the logical bridge device the packet
216 * came in on, we can consider this bridging. We then call
217 * skb->dst->output() which will make the packet enter br_nf_local_out()
218 * not much later. In that function it is assured that the iptables
219 * FORWARD chain is traversed for the packet.
221 * Otherwise, the packet is considered to be routed and we just
222 * change the destination MAC address so that the packet will
223 * later be passed up to the IP stack to be routed. For a redirected
224 * packet, ip_route_input() will give back the localhost as output device,
225 * which differs from the bridge device.
227 * Let us now consider the case that ip_route_input() fails:
229 * This can be because the destination address is martian, in which case
230 * the packet will be dropped.
231 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
232 * will fail, while __ip_route_output_key() will return success. The source
233 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
234 * thinks we're handling a locally generated packet and won't care
235 * if IP forwarding is allowed. We send a warning message to the users's
236 * log telling her to put IP forwarding on.
238 * ip_route_input() will also fail if there is no route available.
239 * In that case we just drop the packet.
241 * --Lennert, 20020411
242 * --Bart, 20020416 (updated)
243 * --Bart, 20021007 (updated)
244 * --Bart, 20062711 (updated) */
245 static int br_nf_pre_routing_finish_bridge(struct sk_buff
*skb
)
247 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
248 skb
->pkt_type
= PACKET_HOST
;
249 skb
->nf_bridge
->mask
|= BRNF_PKT_TYPE
;
251 skb
->nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
253 skb
->dev
= bridge_parent(skb
->dev
);
257 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
258 skb_pull(skb
, VLAN_HLEN
);
259 skb
->network_header
+= VLAN_HLEN
;
261 skb
->dst
->output(skb
);
266 static int br_nf_pre_routing_finish(struct sk_buff
*skb
)
268 struct net_device
*dev
= skb
->dev
;
269 struct iphdr
*iph
= ip_hdr(skb
);
270 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
273 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
274 skb
->pkt_type
= PACKET_OTHERHOST
;
275 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
277 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
278 if (dnat_took_place(skb
)) {
279 if ((err
= ip_route_input(skb
, iph
->daddr
, iph
->saddr
, iph
->tos
, dev
))) {
286 .tos
= RT_TOS(iph
->tos
) },
290 struct in_device
*in_dev
= in_dev_get(dev
);
292 /* If err equals -EHOSTUNREACH the error is due to a
293 * martian destination or due to the fact that
294 * forwarding is disabled. For most martian packets,
295 * ip_route_output_key() will fail. It won't fail for 2 types of
296 * martian destinations: loopback destinations and destination
297 * 0.0.0.0. In both cases the packet will be dropped because the
298 * destination is the loopback device and not the bridge. */
299 if (err
!= -EHOSTUNREACH
|| !in_dev
|| IN_DEV_FORWARD(in_dev
))
302 if (!ip_route_output_key(&rt
, &fl
)) {
303 /* - Bridged-and-DNAT'ed traffic doesn't
304 * require ip_forwarding. */
305 if (((struct dst_entry
*)rt
)->dev
== dev
) {
306 skb
->dst
= (struct dst_entry
*)rt
;
309 /* we are sure that forwarding is disabled, so printing
310 * this message is no problem. Note that the packet could
311 * still have a martian destination address, in which case
312 * the packet could be dropped even if forwarding were enabled */
313 __br_dnat_complain();
314 dst_release((struct dst_entry
*)rt
);
320 if (skb
->dst
->dev
== dev
) {
322 /* Tell br_nf_local_out this is a
324 nf_bridge
->mask
|= BRNF_BRIDGED_DNAT
;
325 skb
->dev
= nf_bridge
->physindev
;
327 htons(ETH_P_8021Q
)) {
328 skb_push(skb
, VLAN_HLEN
);
329 skb
->network_header
-= VLAN_HLEN
;
331 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
,
333 br_nf_pre_routing_finish_bridge
,
337 memcpy(eth_hdr(skb
)->h_dest
, dev
->dev_addr
, ETH_ALEN
);
338 skb
->pkt_type
= PACKET_HOST
;
341 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
345 skb
->dev
= nf_bridge
->physindev
;
346 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
347 skb_push(skb
, VLAN_HLEN
);
348 skb
->network_header
-= VLAN_HLEN
;
350 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
351 br_handle_frame_finish
, 1);
356 /* Some common code for IPv4/IPv6 */
357 static struct net_device
*setup_pre_routing(struct sk_buff
*skb
)
359 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
361 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
362 skb
->pkt_type
= PACKET_HOST
;
363 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
366 nf_bridge
->mask
|= BRNF_NF_BRIDGE_PREROUTING
;
367 nf_bridge
->physindev
= skb
->dev
;
368 skb
->dev
= bridge_parent(skb
->dev
);
373 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
374 static int check_hbh_len(struct sk_buff
*skb
)
376 unsigned char *raw
= (u8
*)(ipv6_hdr(skb
) + 1);
378 const unsigned char *nh
= skb_network_header(skb
);
380 int len
= (raw
[1] + 1) << 3;
382 if ((raw
+ len
) - skb
->data
> skb_headlen(skb
))
389 int optlen
= nh
[off
+ 1] + 2;
400 if (nh
[off
+ 1] != 4 || (off
& 3) != 2)
402 pkt_len
= ntohl(*(__be32
*) (nh
+ off
+ 2));
403 if (pkt_len
<= IPV6_MAXPLEN
||
404 ipv6_hdr(skb
)->payload_len
)
406 if (pkt_len
> skb
->len
- sizeof(struct ipv6hdr
))
408 if (pskb_trim_rcsum(skb
,
409 pkt_len
+ sizeof(struct ipv6hdr
)))
411 nh
= skb_network_header(skb
);
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
->network_header
+= 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
->network_header
+= 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
->network_header
-= 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
)->network_header
+= 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
)->network_header
+= VLAN_HLEN
;
674 if (arp_hdr(skb
)->ar_pln
!= 4) {
675 if (IS_VLAN_ARP(skb
)) {
676 skb_push(*pskb
, VLAN_HLEN
);
677 (*pskb
)->network_header
-= 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 ***********************************************
690 * This function sees both locally originated IP packets and forwarded
691 * IP packets (in both cases the destination device is a bridge
692 * device). It also sees bridged-and-DNAT'ed packets.
694 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
695 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
696 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
697 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
700 static unsigned int br_nf_local_out(unsigned int hook
, struct sk_buff
**pskb
,
701 const struct net_device
*in
,
702 const struct net_device
*out
,
703 int (*okfn
)(struct sk_buff
*))
705 struct net_device
*realindev
;
706 struct sk_buff
*skb
= *pskb
;
707 struct nf_bridge_info
*nf_bridge
;
712 nf_bridge
= skb
->nf_bridge
;
713 if (!(nf_bridge
->mask
& BRNF_BRIDGED_DNAT
))
716 /* Bridged, take PF_BRIDGE/FORWARD.
717 * (see big note in front of br_nf_pre_routing_finish) */
718 nf_bridge
->physoutdev
= skb
->dev
;
719 realindev
= nf_bridge
->physindev
;
721 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
722 skb
->pkt_type
= PACKET_OTHERHOST
;
723 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
725 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
726 skb_push(skb
, VLAN_HLEN
);
727 skb
->network_header
-= VLAN_HLEN
;
730 NF_HOOK(PF_BRIDGE
, NF_BR_FORWARD
, skb
, realindev
, skb
->dev
,
735 static int br_nf_dev_queue_xmit(struct sk_buff
*skb
)
737 if (skb
->protocol
== htons(ETH_P_IP
) &&
738 skb
->len
> skb
->dev
->mtu
&&
740 return ip_fragment(skb
, br_dev_queue_push_xmit
);
742 return br_dev_queue_push_xmit(skb
);
745 /* PF_BRIDGE/POST_ROUTING ********************************************/
746 static unsigned int br_nf_post_routing(unsigned int hook
, struct sk_buff
**pskb
,
747 const struct net_device
*in
,
748 const struct net_device
*out
,
749 int (*okfn
)(struct sk_buff
*))
751 struct sk_buff
*skb
= *pskb
;
752 struct nf_bridge_info
*nf_bridge
= (*pskb
)->nf_bridge
;
753 struct net_device
*realoutdev
= bridge_parent(skb
->dev
);
756 #ifdef CONFIG_NETFILTER_DEBUG
757 /* Be very paranoid. This probably won't happen anymore, but let's
758 * keep the check just to be sure... */
759 if (skb_mac_header(skb
) < skb
->head
||
760 skb_mac_header(skb
) + ETH_HLEN
> skb
->data
) {
761 printk(KERN_CRIT
"br_netfilter: Argh!! br_nf_post_routing: "
762 "bad mac.raw pointer.\n");
773 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
778 #ifdef CONFIG_NETFILTER_DEBUG
779 if (skb
->dst
== NULL
) {
780 printk(KERN_INFO
"br_netfilter post_routing: skb->dst == NULL\n");
785 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
786 * about the value of skb->pkt_type. */
787 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
788 skb
->pkt_type
= PACKET_HOST
;
789 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
792 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
793 skb_pull(skb
, VLAN_HLEN
);
794 skb
->network_header
+= VLAN_HLEN
;
797 nf_bridge_save_header(skb
);
799 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
800 if (nf_bridge
->netoutdev
)
801 realoutdev
= nf_bridge
->netoutdev
;
803 NF_HOOK(pf
, NF_IP_POST_ROUTING
, skb
, NULL
, realoutdev
,
804 br_nf_dev_queue_xmit
);
808 #ifdef CONFIG_NETFILTER_DEBUG
810 if (skb
->dev
!= NULL
) {
811 printk("[%s]", skb
->dev
->name
);
813 printk("[%s]", realoutdev
->name
);
815 printk(" head:%p, raw:%p, data:%p\n", skb
->head
, skb_mac_header(skb
),
822 /* IP/SABOTAGE *****************************************************/
823 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
824 * for the second time. */
825 static unsigned int ip_sabotage_in(unsigned int hook
, struct sk_buff
**pskb
,
826 const struct net_device
*in
,
827 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 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
839 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
840 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
841 * ip_refrag() can return NF_STOLEN. */
842 static struct nf_hook_ops br_nf_ops
[] = {
843 { .hook
= br_nf_pre_routing
,
844 .owner
= THIS_MODULE
,
846 .hooknum
= NF_BR_PRE_ROUTING
,
847 .priority
= NF_BR_PRI_BRNF
, },
848 { .hook
= br_nf_local_in
,
849 .owner
= THIS_MODULE
,
851 .hooknum
= NF_BR_LOCAL_IN
,
852 .priority
= NF_BR_PRI_BRNF
, },
853 { .hook
= br_nf_forward_ip
,
854 .owner
= THIS_MODULE
,
856 .hooknum
= NF_BR_FORWARD
,
857 .priority
= NF_BR_PRI_BRNF
- 1, },
858 { .hook
= br_nf_forward_arp
,
859 .owner
= THIS_MODULE
,
861 .hooknum
= NF_BR_FORWARD
,
862 .priority
= NF_BR_PRI_BRNF
, },
863 { .hook
= br_nf_local_out
,
864 .owner
= THIS_MODULE
,
866 .hooknum
= NF_BR_LOCAL_OUT
,
867 .priority
= NF_BR_PRI_FIRST
, },
868 { .hook
= br_nf_post_routing
,
869 .owner
= THIS_MODULE
,
871 .hooknum
= NF_BR_POST_ROUTING
,
872 .priority
= NF_BR_PRI_LAST
, },
873 { .hook
= ip_sabotage_in
,
874 .owner
= THIS_MODULE
,
876 .hooknum
= NF_IP_PRE_ROUTING
,
877 .priority
= NF_IP_PRI_FIRST
, },
878 { .hook
= ip_sabotage_in
,
879 .owner
= THIS_MODULE
,
881 .hooknum
= NF_IP6_PRE_ROUTING
,
882 .priority
= NF_IP6_PRI_FIRST
, },
887 int brnf_sysctl_call_tables(ctl_table
* ctl
, int write
, struct file
*filp
,
888 void __user
* buffer
, size_t * lenp
, loff_t
* ppos
)
892 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
894 if (write
&& *(int *)(ctl
->data
))
895 *(int *)(ctl
->data
) = 1;
899 static ctl_table brnf_table
[] = {
901 .ctl_name
= NET_BRIDGE_NF_CALL_ARPTABLES
,
902 .procname
= "bridge-nf-call-arptables",
903 .data
= &brnf_call_arptables
,
904 .maxlen
= sizeof(int),
906 .proc_handler
= &brnf_sysctl_call_tables
,
909 .ctl_name
= NET_BRIDGE_NF_CALL_IPTABLES
,
910 .procname
= "bridge-nf-call-iptables",
911 .data
= &brnf_call_iptables
,
912 .maxlen
= sizeof(int),
914 .proc_handler
= &brnf_sysctl_call_tables
,
917 .ctl_name
= NET_BRIDGE_NF_CALL_IP6TABLES
,
918 .procname
= "bridge-nf-call-ip6tables",
919 .data
= &brnf_call_ip6tables
,
920 .maxlen
= sizeof(int),
922 .proc_handler
= &brnf_sysctl_call_tables
,
925 .ctl_name
= NET_BRIDGE_NF_FILTER_VLAN_TAGGED
,
926 .procname
= "bridge-nf-filter-vlan-tagged",
927 .data
= &brnf_filter_vlan_tagged
,
928 .maxlen
= sizeof(int),
930 .proc_handler
= &brnf_sysctl_call_tables
,
935 static ctl_table brnf_bridge_table
[] = {
937 .ctl_name
= NET_BRIDGE
,
938 .procname
= "bridge",
945 static ctl_table brnf_net_table
[] = {
950 .child
= brnf_bridge_table
,
956 int __init
br_netfilter_init(void)
960 ret
= nf_register_hooks(br_nf_ops
, ARRAY_SIZE(br_nf_ops
));
964 brnf_sysctl_header
= register_sysctl_table(brnf_net_table
);
965 if (brnf_sysctl_header
== NULL
) {
967 "br_netfilter: can't register to sysctl.\n");
968 nf_unregister_hooks(br_nf_ops
, ARRAY_SIZE(br_nf_ops
));
972 printk(KERN_NOTICE
"Bridge firewalling registered\n");
976 void br_netfilter_fini(void)
978 nf_unregister_hooks(br_nf_ops
, ARRAY_SIZE(br_nf_ops
));
980 unregister_sysctl_table(brnf_sysctl_header
);