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 memcpy(skb
->nf_bridge
->data
, skb
->data
- header_size
, header_size
);
136 * When forwarding bridge frames, we save a copy of the original
137 * header before processing.
139 int nf_bridge_copy_header(struct sk_buff
*skb
)
142 int header_size
= ETH_HLEN
;
144 if (skb
->protocol
== htons(ETH_P_8021Q
))
145 header_size
+= VLAN_HLEN
;
147 err
= skb_cow(skb
, header_size
);
151 memcpy(skb
->data
- header_size
, skb
->nf_bridge
->data
, header_size
);
153 if (skb
->protocol
== htons(ETH_P_8021Q
))
154 __skb_push(skb
, VLAN_HLEN
);
158 /* PF_BRIDGE/PRE_ROUTING *********************************************/
159 /* Undo the changes made for ip6tables PREROUTING and continue the
160 * bridge PRE_ROUTING hook. */
161 static int br_nf_pre_routing_finish_ipv6(struct sk_buff
*skb
)
163 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
165 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
166 skb
->pkt_type
= PACKET_OTHERHOST
;
167 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
169 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
171 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
174 skb
->dev
= nf_bridge
->physindev
;
175 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
176 skb_push(skb
, VLAN_HLEN
);
177 skb
->network_header
-= VLAN_HLEN
;
179 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
180 br_handle_frame_finish
, 1);
185 static void __br_dnat_complain(void)
187 static unsigned long last_complaint
;
189 if (jiffies
- last_complaint
>= 5 * HZ
) {
190 printk(KERN_WARNING
"Performing cross-bridge DNAT requires IP "
191 "forwarding to be enabled\n");
192 last_complaint
= jiffies
;
196 /* This requires some explaining. If DNAT has taken place,
197 * we will need to fix up the destination Ethernet address,
198 * and this is a tricky process.
200 * There are two cases to consider:
201 * 1. The packet was DNAT'ed to a device in the same bridge
202 * port group as it was received on. We can still bridge
204 * 2. The packet was DNAT'ed to a different device, either
205 * a non-bridged device or another bridge port group.
206 * The packet will need to be routed.
208 * The correct way of distinguishing between these two cases is to
209 * call ip_route_input() and to look at skb->dst->dev, which is
210 * changed to the destination device if ip_route_input() succeeds.
212 * Let us first consider the case that ip_route_input() succeeds:
214 * If skb->dst->dev equals the logical bridge device the packet
215 * came in on, we can consider this bridging. We then call
216 * skb->dst->output() which will make the packet enter br_nf_local_out()
217 * not much later. In that function it is assured that the iptables
218 * FORWARD chain is traversed for the packet.
220 * Otherwise, the packet is considered to be routed and we just
221 * change the destination MAC address so that the packet will
222 * later be passed up to the IP stack to be routed. For a redirected
223 * packet, ip_route_input() will give back the localhost as output device,
224 * which differs from the bridge device.
226 * Let us now consider the case that ip_route_input() fails:
228 * This can be because the destination address is martian, in which case
229 * the packet will be dropped.
230 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
231 * will fail, while __ip_route_output_key() will return success. The source
232 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
233 * thinks we're handling a locally generated packet and won't care
234 * if IP forwarding is allowed. We send a warning message to the users's
235 * log telling her to put IP forwarding on.
237 * ip_route_input() will also fail if there is no route available.
238 * In that case we just drop the packet.
240 * --Lennert, 20020411
241 * --Bart, 20020416 (updated)
242 * --Bart, 20021007 (updated)
243 * --Bart, 20062711 (updated) */
244 static int br_nf_pre_routing_finish_bridge(struct sk_buff
*skb
)
246 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
247 skb
->pkt_type
= PACKET_HOST
;
248 skb
->nf_bridge
->mask
|= BRNF_PKT_TYPE
;
250 skb
->nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
252 skb
->dev
= bridge_parent(skb
->dev
);
256 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
257 skb_pull(skb
, VLAN_HLEN
);
258 skb
->network_header
+= VLAN_HLEN
;
260 skb
->dst
->output(skb
);
265 static int br_nf_pre_routing_finish(struct sk_buff
*skb
)
267 struct net_device
*dev
= skb
->dev
;
268 struct iphdr
*iph
= ip_hdr(skb
);
269 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
272 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
273 skb
->pkt_type
= PACKET_OTHERHOST
;
274 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
276 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
277 if (dnat_took_place(skb
)) {
278 if ((err
= ip_route_input(skb
, iph
->daddr
, iph
->saddr
, iph
->tos
, dev
))) {
285 .tos
= RT_TOS(iph
->tos
) },
289 struct in_device
*in_dev
= in_dev_get(dev
);
291 /* If err equals -EHOSTUNREACH the error is due to a
292 * martian destination or due to the fact that
293 * forwarding is disabled. For most martian packets,
294 * ip_route_output_key() will fail. It won't fail for 2 types of
295 * martian destinations: loopback destinations and destination
296 * 0.0.0.0. In both cases the packet will be dropped because the
297 * destination is the loopback device and not the bridge. */
298 if (err
!= -EHOSTUNREACH
|| !in_dev
|| IN_DEV_FORWARD(in_dev
))
301 if (!ip_route_output_key(&rt
, &fl
)) {
302 /* - Bridged-and-DNAT'ed traffic doesn't
303 * require ip_forwarding. */
304 if (((struct dst_entry
*)rt
)->dev
== dev
) {
305 skb
->dst
= (struct dst_entry
*)rt
;
308 /* we are sure that forwarding is disabled, so printing
309 * this message is no problem. Note that the packet could
310 * still have a martian destination address, in which case
311 * the packet could be dropped even if forwarding were enabled */
312 __br_dnat_complain();
313 dst_release((struct dst_entry
*)rt
);
319 if (skb
->dst
->dev
== dev
) {
321 /* Tell br_nf_local_out this is a
323 nf_bridge
->mask
|= BRNF_BRIDGED_DNAT
;
324 skb
->dev
= nf_bridge
->physindev
;
326 htons(ETH_P_8021Q
)) {
327 skb_push(skb
, VLAN_HLEN
);
328 skb
->network_header
-= VLAN_HLEN
;
330 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
,
332 br_nf_pre_routing_finish_bridge
,
336 memcpy(eth_hdr(skb
)->h_dest
, dev
->dev_addr
, ETH_ALEN
);
337 skb
->pkt_type
= PACKET_HOST
;
340 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
344 skb
->dev
= nf_bridge
->physindev
;
345 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
346 skb_push(skb
, VLAN_HLEN
);
347 skb
->network_header
-= VLAN_HLEN
;
349 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
350 br_handle_frame_finish
, 1);
355 /* Some common code for IPv4/IPv6 */
356 static struct net_device
*setup_pre_routing(struct sk_buff
*skb
)
358 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
360 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
361 skb
->pkt_type
= PACKET_HOST
;
362 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
365 nf_bridge
->mask
|= BRNF_NF_BRIDGE_PREROUTING
;
366 nf_bridge
->physindev
= skb
->dev
;
367 skb
->dev
= bridge_parent(skb
->dev
);
372 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
373 static int check_hbh_len(struct sk_buff
*skb
)
375 unsigned char *raw
= (u8
*)(ipv6_hdr(skb
) + 1);
377 const unsigned char *nh
= skb_network_header(skb
);
379 int len
= (raw
[1] + 1) << 3;
381 if ((raw
+ len
) - skb
->data
> skb_headlen(skb
))
388 int optlen
= nh
[off
+ 1] + 2;
399 if (nh
[off
+ 1] != 4 || (off
& 3) != 2)
401 pkt_len
= ntohl(*(__be32
*) (nh
+ off
+ 2));
402 if (pkt_len
<= IPV6_MAXPLEN
||
403 ipv6_hdr(skb
)->payload_len
)
405 if (pkt_len
> skb
->len
- sizeof(struct ipv6hdr
))
407 if (pskb_trim_rcsum(skb
,
408 pkt_len
+ sizeof(struct ipv6hdr
)))
410 nh
= skb_network_header(skb
);
427 /* Replicate the checks that IPv6 does on packet reception and pass the packet
428 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
429 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook
,
431 const struct net_device
*in
,
432 const struct net_device
*out
,
433 int (*okfn
)(struct sk_buff
*))
438 if (skb
->len
< sizeof(struct ipv6hdr
))
441 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
446 if (hdr
->version
!= 6)
449 pkt_len
= ntohs(hdr
->payload_len
);
451 if (pkt_len
|| hdr
->nexthdr
!= NEXTHDR_HOP
) {
452 if (pkt_len
+ sizeof(struct ipv6hdr
) > skb
->len
)
454 if (pskb_trim_rcsum(skb
, pkt_len
+ sizeof(struct ipv6hdr
)))
457 if (hdr
->nexthdr
== NEXTHDR_HOP
&& check_hbh_len(skb
))
460 nf_bridge_put(skb
->nf_bridge
);
461 if (!nf_bridge_alloc(skb
))
463 if (!setup_pre_routing(skb
))
466 NF_HOOK(PF_INET6
, NF_IP6_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
467 br_nf_pre_routing_finish_ipv6
);
475 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
476 * Replicate the checks that IPv4 does on packet reception.
477 * Set skb->dev to the bridge device (i.e. parent of the
478 * receiving device) to make netfilter happy, the REDIRECT
479 * target in particular. Save the original destination IP
480 * address to be able to detect DNAT afterwards. */
481 static unsigned int br_nf_pre_routing(unsigned int hook
, struct sk_buff
**pskb
,
482 const struct net_device
*in
,
483 const struct net_device
*out
,
484 int (*okfn
)(struct sk_buff
*))
488 struct sk_buff
*skb
= *pskb
;
490 if (skb
->protocol
== htons(ETH_P_IPV6
) || IS_VLAN_IPV6(skb
)) {
492 if (!brnf_call_ip6tables
)
495 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
498 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
499 skb_pull_rcsum(skb
, VLAN_HLEN
);
500 skb
->network_header
+= VLAN_HLEN
;
502 return br_nf_pre_routing_ipv6(hook
, skb
, in
, out
, okfn
);
505 if (!brnf_call_iptables
)
509 if (skb
->protocol
!= htons(ETH_P_IP
) && !IS_VLAN_IP(skb
))
512 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
515 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
516 skb_pull_rcsum(skb
, VLAN_HLEN
);
517 skb
->network_header
+= VLAN_HLEN
;
520 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
524 if (iph
->ihl
< 5 || iph
->version
!= 4)
527 if (!pskb_may_pull(skb
, 4 * iph
->ihl
))
531 if (ip_fast_csum((__u8
*) iph
, iph
->ihl
) != 0)
534 len
= ntohs(iph
->tot_len
);
535 if (skb
->len
< len
|| len
< 4 * iph
->ihl
)
538 pskb_trim_rcsum(skb
, len
);
540 nf_bridge_put(skb
->nf_bridge
);
541 if (!nf_bridge_alloc(skb
))
543 if (!setup_pre_routing(skb
))
545 store_orig_dstaddr(skb
);
547 NF_HOOK(PF_INET
, NF_IP_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
548 br_nf_pre_routing_finish
);
553 // IP_INC_STATS_BH(IpInHdrErrors);
559 /* PF_BRIDGE/LOCAL_IN ************************************************/
560 /* The packet is locally destined, which requires a real
561 * dst_entry, so detach the fake one. On the way up, the
562 * packet would pass through PRE_ROUTING again (which already
563 * took place when the packet entered the bridge), but we
564 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
565 * prevent this from happening. */
566 static unsigned int br_nf_local_in(unsigned int hook
, struct sk_buff
**pskb
,
567 const struct net_device
*in
,
568 const struct net_device
*out
,
569 int (*okfn
)(struct sk_buff
*))
571 struct sk_buff
*skb
= *pskb
;
573 if (skb
->dst
== (struct dst_entry
*)&__fake_rtable
) {
574 dst_release(skb
->dst
);
581 /* PF_BRIDGE/FORWARD *************************************************/
582 static int br_nf_forward_finish(struct sk_buff
*skb
)
584 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
585 struct net_device
*in
;
587 if (skb
->protocol
!= htons(ETH_P_ARP
) && !IS_VLAN_ARP(skb
)) {
588 in
= nf_bridge
->physindev
;
589 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
590 skb
->pkt_type
= PACKET_OTHERHOST
;
591 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
594 in
= *((struct net_device
**)(skb
->cb
));
596 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
597 skb_push(skb
, VLAN_HLEN
);
598 skb
->network_header
-= VLAN_HLEN
;
600 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_FORWARD
, skb
, in
,
601 skb
->dev
, br_forward_finish
, 1);
605 /* This is the 'purely bridged' case. For IP, we pass the packet to
606 * netfilter with indev and outdev set to the bridge device,
607 * but we are still able to filter on the 'real' indev/outdev
608 * because of the physdev module. For ARP, indev and outdev are the
610 static unsigned int br_nf_forward_ip(unsigned int hook
, struct sk_buff
**pskb
,
611 const struct net_device
*in
,
612 const struct net_device
*out
,
613 int (*okfn
)(struct sk_buff
*))
615 struct sk_buff
*skb
= *pskb
;
616 struct nf_bridge_info
*nf_bridge
;
617 struct net_device
*parent
;
623 parent
= bridge_parent(out
);
627 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
632 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
633 skb_pull(*pskb
, VLAN_HLEN
);
634 (*pskb
)->network_header
+= VLAN_HLEN
;
637 nf_bridge
= skb
->nf_bridge
;
638 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
639 skb
->pkt_type
= PACKET_HOST
;
640 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
643 /* The physdev module checks on this */
644 nf_bridge
->mask
|= BRNF_BRIDGED
;
645 nf_bridge
->physoutdev
= skb
->dev
;
647 NF_HOOK(pf
, NF_IP_FORWARD
, skb
, bridge_parent(in
), parent
,
648 br_nf_forward_finish
);
653 static unsigned int br_nf_forward_arp(unsigned int hook
, struct sk_buff
**pskb
,
654 const struct net_device
*in
,
655 const struct net_device
*out
,
656 int (*okfn
)(struct sk_buff
*))
658 struct sk_buff
*skb
= *pskb
;
659 struct net_device
**d
= (struct net_device
**)(skb
->cb
);
662 if (!brnf_call_arptables
)
666 if (skb
->protocol
!= htons(ETH_P_ARP
)) {
667 if (!IS_VLAN_ARP(skb
))
669 skb_pull(*pskb
, VLAN_HLEN
);
670 (*pskb
)->network_header
+= VLAN_HLEN
;
673 if (arp_hdr(skb
)->ar_pln
!= 4) {
674 if (IS_VLAN_ARP(skb
)) {
675 skb_push(*pskb
, VLAN_HLEN
);
676 (*pskb
)->network_header
-= VLAN_HLEN
;
680 *d
= (struct net_device
*)in
;
681 NF_HOOK(NF_ARP
, NF_ARP_FORWARD
, skb
, (struct net_device
*)in
,
682 (struct net_device
*)out
, br_nf_forward_finish
);
687 /* PF_BRIDGE/LOCAL_OUT ***********************************************
689 * This function sees both locally originated IP packets and forwarded
690 * IP packets (in both cases the destination device is a bridge
691 * device). It also sees bridged-and-DNAT'ed packets.
693 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
694 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
695 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
696 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
699 static unsigned int br_nf_local_out(unsigned int hook
, struct sk_buff
**pskb
,
700 const struct net_device
*in
,
701 const struct net_device
*out
,
702 int (*okfn
)(struct sk_buff
*))
704 struct net_device
*realindev
;
705 struct sk_buff
*skb
= *pskb
;
706 struct nf_bridge_info
*nf_bridge
;
711 nf_bridge
= skb
->nf_bridge
;
712 if (!(nf_bridge
->mask
& BRNF_BRIDGED_DNAT
))
715 /* Bridged, take PF_BRIDGE/FORWARD.
716 * (see big note in front of br_nf_pre_routing_finish) */
717 nf_bridge
->physoutdev
= skb
->dev
;
718 realindev
= nf_bridge
->physindev
;
720 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
721 skb
->pkt_type
= PACKET_OTHERHOST
;
722 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
724 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
725 skb_push(skb
, VLAN_HLEN
);
726 skb
->network_header
-= VLAN_HLEN
;
729 NF_HOOK(PF_BRIDGE
, NF_BR_FORWARD
, skb
, realindev
, skb
->dev
,
734 static int br_nf_dev_queue_xmit(struct sk_buff
*skb
)
736 if (skb
->protocol
== htons(ETH_P_IP
) &&
737 skb
->len
> skb
->dev
->mtu
&&
739 return ip_fragment(skb
, br_dev_queue_push_xmit
);
741 return br_dev_queue_push_xmit(skb
);
744 /* PF_BRIDGE/POST_ROUTING ********************************************/
745 static unsigned int br_nf_post_routing(unsigned int hook
, struct sk_buff
**pskb
,
746 const struct net_device
*in
,
747 const struct net_device
*out
,
748 int (*okfn
)(struct sk_buff
*))
750 struct sk_buff
*skb
= *pskb
;
751 struct nf_bridge_info
*nf_bridge
= (*pskb
)->nf_bridge
;
752 struct net_device
*realoutdev
= bridge_parent(skb
->dev
);
755 #ifdef CONFIG_NETFILTER_DEBUG
756 /* Be very paranoid. This probably won't happen anymore, but let's
757 * keep the check just to be sure... */
758 if (skb_mac_header(skb
) < skb
->head
||
759 skb_mac_header(skb
) + ETH_HLEN
> skb
->data
) {
760 printk(KERN_CRIT
"br_netfilter: Argh!! br_nf_post_routing: "
761 "bad mac.raw pointer.\n");
772 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
777 #ifdef CONFIG_NETFILTER_DEBUG
778 if (skb
->dst
== NULL
) {
779 printk(KERN_INFO
"br_netfilter post_routing: skb->dst == NULL\n");
784 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
785 * about the value of skb->pkt_type. */
786 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
787 skb
->pkt_type
= PACKET_HOST
;
788 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
791 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
792 skb_pull(skb
, VLAN_HLEN
);
793 skb
->network_header
+= VLAN_HLEN
;
796 nf_bridge_save_header(skb
);
798 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
799 if (nf_bridge
->netoutdev
)
800 realoutdev
= nf_bridge
->netoutdev
;
802 NF_HOOK(pf
, NF_IP_POST_ROUTING
, skb
, NULL
, realoutdev
,
803 br_nf_dev_queue_xmit
);
807 #ifdef CONFIG_NETFILTER_DEBUG
809 if (skb
->dev
!= NULL
) {
810 printk("[%s]", skb
->dev
->name
);
812 printk("[%s]", realoutdev
->name
);
814 printk(" head:%p, raw:%p, data:%p\n", skb
->head
, skb_mac_header(skb
),
821 /* IP/SABOTAGE *****************************************************/
822 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
823 * for the second time. */
824 static unsigned int ip_sabotage_in(unsigned int hook
, struct sk_buff
**pskb
,
825 const struct net_device
*in
,
826 const struct net_device
*out
,
827 int (*okfn
)(struct sk_buff
*))
829 if ((*pskb
)->nf_bridge
&&
830 !((*pskb
)->nf_bridge
->mask
& BRNF_NF_BRIDGE_PREROUTING
)) {
837 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
838 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
839 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
840 * ip_refrag() can return NF_STOLEN. */
841 static struct nf_hook_ops br_nf_ops
[] = {
842 { .hook
= br_nf_pre_routing
,
843 .owner
= THIS_MODULE
,
845 .hooknum
= NF_BR_PRE_ROUTING
,
846 .priority
= NF_BR_PRI_BRNF
, },
847 { .hook
= br_nf_local_in
,
848 .owner
= THIS_MODULE
,
850 .hooknum
= NF_BR_LOCAL_IN
,
851 .priority
= NF_BR_PRI_BRNF
, },
852 { .hook
= br_nf_forward_ip
,
853 .owner
= THIS_MODULE
,
855 .hooknum
= NF_BR_FORWARD
,
856 .priority
= NF_BR_PRI_BRNF
- 1, },
857 { .hook
= br_nf_forward_arp
,
858 .owner
= THIS_MODULE
,
860 .hooknum
= NF_BR_FORWARD
,
861 .priority
= NF_BR_PRI_BRNF
, },
862 { .hook
= br_nf_local_out
,
863 .owner
= THIS_MODULE
,
865 .hooknum
= NF_BR_LOCAL_OUT
,
866 .priority
= NF_BR_PRI_FIRST
, },
867 { .hook
= br_nf_post_routing
,
868 .owner
= THIS_MODULE
,
870 .hooknum
= NF_BR_POST_ROUTING
,
871 .priority
= NF_BR_PRI_LAST
, },
872 { .hook
= ip_sabotage_in
,
873 .owner
= THIS_MODULE
,
875 .hooknum
= NF_IP_PRE_ROUTING
,
876 .priority
= NF_IP_PRI_FIRST
, },
877 { .hook
= ip_sabotage_in
,
878 .owner
= THIS_MODULE
,
880 .hooknum
= NF_IP6_PRE_ROUTING
,
881 .priority
= NF_IP6_PRI_FIRST
, },
886 int brnf_sysctl_call_tables(ctl_table
* ctl
, int write
, struct file
*filp
,
887 void __user
* buffer
, size_t * lenp
, loff_t
* ppos
)
891 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
893 if (write
&& *(int *)(ctl
->data
))
894 *(int *)(ctl
->data
) = 1;
898 static ctl_table brnf_table
[] = {
900 .ctl_name
= NET_BRIDGE_NF_CALL_ARPTABLES
,
901 .procname
= "bridge-nf-call-arptables",
902 .data
= &brnf_call_arptables
,
903 .maxlen
= sizeof(int),
905 .proc_handler
= &brnf_sysctl_call_tables
,
908 .ctl_name
= NET_BRIDGE_NF_CALL_IPTABLES
,
909 .procname
= "bridge-nf-call-iptables",
910 .data
= &brnf_call_iptables
,
911 .maxlen
= sizeof(int),
913 .proc_handler
= &brnf_sysctl_call_tables
,
916 .ctl_name
= NET_BRIDGE_NF_CALL_IP6TABLES
,
917 .procname
= "bridge-nf-call-ip6tables",
918 .data
= &brnf_call_ip6tables
,
919 .maxlen
= sizeof(int),
921 .proc_handler
= &brnf_sysctl_call_tables
,
924 .ctl_name
= NET_BRIDGE_NF_FILTER_VLAN_TAGGED
,
925 .procname
= "bridge-nf-filter-vlan-tagged",
926 .data
= &brnf_filter_vlan_tagged
,
927 .maxlen
= sizeof(int),
929 .proc_handler
= &brnf_sysctl_call_tables
,
934 static ctl_table brnf_bridge_table
[] = {
936 .ctl_name
= NET_BRIDGE
,
937 .procname
= "bridge",
944 static ctl_table brnf_net_table
[] = {
949 .child
= brnf_bridge_table
,
955 int __init
br_netfilter_init(void)
959 ret
= nf_register_hooks(br_nf_ops
, ARRAY_SIZE(br_nf_ops
));
963 brnf_sysctl_header
= register_sysctl_table(brnf_net_table
);
964 if (brnf_sysctl_header
== NULL
) {
966 "br_netfilter: can't register to sysctl.\n");
967 nf_unregister_hooks(br_nf_ops
, ARRAY_SIZE(br_nf_ops
));
971 printk(KERN_NOTICE
"Bridge firewalling registered\n");
975 void br_netfilter_fini(void)
977 nf_unregister_hooks(br_nf_ops
, ARRAY_SIZE(br_nf_ops
));
979 unregister_sysctl_table(brnf_sysctl_header
);