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 skb_copy_to_linear_data_offset(skb, -header_size,
153 skb->nf_bridge->data, header_size);
155 if (skb->protocol == htons(ETH_P_8021Q))
156 __skb_push(skb, VLAN_HLEN);
160 /* PF_BRIDGE/PRE_ROUTING *********************************************/
161 /* Undo the changes made for ip6tables PREROUTING and continue the
162 * bridge PRE_ROUTING hook. */
163 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
165 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
167 if (nf_bridge->mask & BRNF_PKT_TYPE) {
168 skb->pkt_type = PACKET_OTHERHOST;
169 nf_bridge->mask ^= BRNF_PKT_TYPE;
171 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
173 skb->dst = (struct dst_entry *)&__fake_rtable;
176 skb->dev = nf_bridge->physindev;
177 if (skb->protocol == htons(ETH_P_8021Q)) {
178 skb_push(skb, VLAN_HLEN);
179 skb->network_header -= VLAN_HLEN;
181 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
182 br_handle_frame_finish, 1);
187 static void __br_dnat_complain(void)
189 static unsigned long last_complaint;
191 if (jiffies - last_complaint >= 5 * HZ) {
192 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
193 "forwarding to be enabled\n");
194 last_complaint = jiffies;
198 /* This requires some explaining. If DNAT has taken place,
199 * we will need to fix up the destination Ethernet address,
200 * and this is a tricky process.
202 * There are two cases to consider:
203 * 1. The packet was DNAT'ed to a device in the same bridge
204 * port group as it was received on. We can still bridge
206 * 2. The packet was DNAT'ed to a different device, either
207 * a non-bridged device or another bridge port group.
208 * The packet will need to be routed.
210 * The correct way of distinguishing between these two cases is to
211 * call ip_route_input() and to look at skb->dst->dev, which is
212 * changed to the destination device if ip_route_input() succeeds.
214 * Let us first consider the case that ip_route_input() succeeds:
216 * If skb->dst->dev equals the logical bridge device the packet
217 * came in on, we can consider this bridging. We then call
218 * skb->dst->output() which will make the packet enter br_nf_local_out()
219 * not much later. In that function it is assured that the iptables
220 * FORWARD chain is traversed for the packet.
222 * Otherwise, the packet is considered to be routed and we just
223 * change the destination MAC address so that the packet will
224 * later be passed up to the IP stack to be routed. For a redirected
225 * packet, ip_route_input() will give back the localhost as output device,
226 * which differs from the bridge device.
228 * Let us now consider the case that ip_route_input() fails:
230 * This can be because the destination address is martian, in which case
231 * the packet will be dropped.
232 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
233 * will fail, while __ip_route_output_key() will return success. The source
234 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
235 * thinks we're handling a locally generated packet and won't care
236 * if IP forwarding is allowed. We send a warning message to the users's
237 * log telling her to put IP forwarding on.
239 * ip_route_input() will also fail if there is no route available.
240 * In that case we just drop the packet.
242 * --Lennert, 20020411
243 * --Bart, 20020416 (updated)
244 * --Bart, 20021007 (updated)
245 * --Bart, 20062711 (updated) */
246 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
248 if (skb->pkt_type == PACKET_OTHERHOST) {
249 skb->pkt_type = PACKET_HOST;
250 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
252 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
254 skb->dev = bridge_parent(skb->dev);
258 if (skb->protocol == htons(ETH_P_8021Q)) {
259 skb_pull(skb, VLAN_HLEN);
260 skb->network_header += VLAN_HLEN;
262 skb->dst->output(skb);
267 static int br_nf_pre_routing_finish(struct sk_buff *skb)
269 struct net_device *dev = skb->dev;
270 struct iphdr *iph = ip_hdr(skb);
271 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
274 if (nf_bridge->mask & BRNF_PKT_TYPE) {
275 skb->pkt_type = PACKET_OTHERHOST;
276 nf_bridge->mask ^= BRNF_PKT_TYPE;
278 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
279 if (dnat_took_place(skb)) {
280 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
287 .tos = RT_TOS(iph->tos) },
291 struct in_device *in_dev = in_dev_get(dev);
293 /* If err equals -EHOSTUNREACH the error is due to a
294 * martian destination or due to the fact that
295 * forwarding is disabled. For most martian packets,
296 * ip_route_output_key() will fail. It won't fail for 2 types of
297 * martian destinations: loopback destinations and destination
298 * 0.0.0.0. In both cases the packet will be dropped because the
299 * destination is the loopback device and not the bridge. */
300 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
303 if (!ip_route_output_key(&rt, &fl)) {
304 /* - Bridged-and-DNAT'ed traffic doesn't
305 * require ip_forwarding. */
306 if (((struct dst_entry *)rt)->dev == dev) {
307 skb->dst = (struct dst_entry *)rt;
310 /* we are sure that forwarding is disabled, so printing
311 * this message is no problem. Note that the packet could
312 * still have a martian destination address, in which case
313 * the packet could be dropped even if forwarding were enabled */
314 __br_dnat_complain();
315 dst_release((struct dst_entry *)rt);
321 if (skb->dst->dev == dev) {
323 /* Tell br_nf_local_out this is a
325 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
326 skb->dev = nf_bridge->physindev;
328 htons(ETH_P_8021Q)) {
329 skb_push(skb, VLAN_HLEN);
330 skb->network_header -= VLAN_HLEN;
332 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
334 br_nf_pre_routing_finish_bridge,
338 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
339 skb->pkt_type = PACKET_HOST;
342 skb->dst = (struct dst_entry *)&__fake_rtable;
346 skb->dev = nf_bridge->physindev;
347 if (skb->protocol == htons(ETH_P_8021Q)) {
348 skb_push(skb, VLAN_HLEN);
349 skb->network_header -= VLAN_HLEN;
351 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
352 br_handle_frame_finish, 1);
357 /* Some common code for IPv4/IPv6 */
358 static struct net_device *setup_pre_routing(struct sk_buff *skb)
360 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
362 if (skb->pkt_type == PACKET_OTHERHOST) {
363 skb->pkt_type = PACKET_HOST;
364 nf_bridge->mask |= BRNF_PKT_TYPE;
367 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
368 nf_bridge->physindev = skb->dev;
369 skb->dev = bridge_parent(skb->dev);
374 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
375 static int check_hbh_len(struct sk_buff *skb)
377 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
379 const unsigned char *nh = skb_network_header(skb);
381 int len = (raw[1] + 1) << 3;
383 if ((raw + len) - skb->data > skb_headlen(skb))
390 int optlen = nh[off + 1] + 2;
401 if (nh[off + 1] != 4 || (off & 3) != 2)
403 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
404 if (pkt_len <= IPV6_MAXPLEN ||
405 ipv6_hdr(skb)->payload_len)
407 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
409 if (pskb_trim_rcsum(skb,
410 pkt_len + sizeof(struct ipv6hdr)))
412 nh = skb_network_header(skb);
429 /* Replicate the checks that IPv6 does on packet reception and pass the packet
430 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
431 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
433 const struct net_device *in,
434 const struct net_device *out,
435 int (*okfn)(struct sk_buff *))
440 if (skb->len < sizeof(struct ipv6hdr))
443 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
448 if (hdr->version != 6)
451 pkt_len = ntohs(hdr->payload_len);
453 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
454 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
456 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
459 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
462 nf_bridge_put(skb->nf_bridge);
463 if (!nf_bridge_alloc(skb))
465 if (!setup_pre_routing(skb))
468 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
469 br_nf_pre_routing_finish_ipv6);
477 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
478 * Replicate the checks that IPv4 does on packet reception.
479 * Set skb->dev to the bridge device (i.e. parent of the
480 * receiving device) to make netfilter happy, the REDIRECT
481 * target in particular. Save the original destination IP
482 * address to be able to detect DNAT afterwards. */
483 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
484 const struct net_device *in,
485 const struct net_device *out,
486 int (*okfn)(struct sk_buff *))
490 struct sk_buff *skb = *pskb;
492 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb)) {
494 if (!brnf_call_ip6tables)
497 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
500 if (skb->protocol == htons(ETH_P_8021Q)) {
501 skb_pull_rcsum(skb, VLAN_HLEN);
502 skb->network_header += VLAN_HLEN;
504 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
507 if (!brnf_call_iptables)
511 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb))
514 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
517 if (skb->protocol == htons(ETH_P_8021Q)) {
518 skb_pull_rcsum(skb, VLAN_HLEN);
519 skb->network_header += VLAN_HLEN;
522 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
526 if (iph->ihl < 5 || iph->version != 4)
529 if (!pskb_may_pull(skb, 4 * iph->ihl))
533 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
536 len = ntohs(iph->tot_len);
537 if (skb->len < len || len < 4 * iph->ihl)
540 pskb_trim_rcsum(skb, len);
542 nf_bridge_put(skb->nf_bridge);
543 if (!nf_bridge_alloc(skb))
545 if (!setup_pre_routing(skb))
547 store_orig_dstaddr(skb);
549 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
550 br_nf_pre_routing_finish);
555 // IP_INC_STATS_BH(IpInHdrErrors);
561 /* PF_BRIDGE/LOCAL_IN ************************************************/
562 /* The packet is locally destined, which requires a real
563 * dst_entry, so detach the fake one. On the way up, the
564 * packet would pass through PRE_ROUTING again (which already
565 * took place when the packet entered the bridge), but we
566 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
567 * prevent this from happening. */
568 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
569 const struct net_device *in,
570 const struct net_device *out,
571 int (*okfn)(struct sk_buff *))
573 struct sk_buff *skb = *pskb;
575 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
576 dst_release(skb->dst);
583 /* PF_BRIDGE/FORWARD *************************************************/
584 static int br_nf_forward_finish(struct sk_buff *skb)
586 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
587 struct net_device *in;
589 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
590 in = nf_bridge->physindev;
591 if (nf_bridge->mask & BRNF_PKT_TYPE) {
592 skb->pkt_type = PACKET_OTHERHOST;
593 nf_bridge->mask ^= BRNF_PKT_TYPE;
596 in = *((struct net_device **)(skb->cb));
598 if (skb->protocol == htons(ETH_P_8021Q)) {
599 skb_push(skb, VLAN_HLEN);
600 skb->network_header -= VLAN_HLEN;
602 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
603 skb->dev, br_forward_finish, 1);
607 /* This is the 'purely bridged' case. For IP, we pass the packet to
608 * netfilter with indev and outdev set to the bridge device,
609 * but we are still able to filter on the 'real' indev/outdev
610 * because of the physdev module. For ARP, indev and outdev are the
612 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
613 const struct net_device *in,
614 const struct net_device *out,
615 int (*okfn)(struct sk_buff *))
617 struct sk_buff *skb = *pskb;
618 struct nf_bridge_info *nf_bridge;
619 struct net_device *parent;
625 parent = bridge_parent(out);
629 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
634 if (skb->protocol == htons(ETH_P_8021Q)) {
635 skb_pull(*pskb, VLAN_HLEN);
636 (*pskb)->network_header += VLAN_HLEN;
639 nf_bridge = skb->nf_bridge;
640 if (skb->pkt_type == PACKET_OTHERHOST) {
641 skb->pkt_type = PACKET_HOST;
642 nf_bridge->mask |= BRNF_PKT_TYPE;
645 /* The physdev module checks on this */
646 nf_bridge->mask |= BRNF_BRIDGED;
647 nf_bridge->physoutdev = skb->dev;
649 NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
650 br_nf_forward_finish);
655 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
656 const struct net_device *in,
657 const struct net_device *out,
658 int (*okfn)(struct sk_buff *))
660 struct sk_buff *skb = *pskb;
661 struct net_device **d = (struct net_device **)(skb->cb);
664 if (!brnf_call_arptables)
668 if (skb->protocol != htons(ETH_P_ARP)) {
669 if (!IS_VLAN_ARP(skb))
671 skb_pull(*pskb, VLAN_HLEN);
672 (*pskb)->network_header += VLAN_HLEN;
675 if (arp_hdr(skb)->ar_pln != 4) {
676 if (IS_VLAN_ARP(skb)) {
677 skb_push(*pskb, VLAN_HLEN);
678 (*pskb)->network_header -= VLAN_HLEN;
682 *d = (struct net_device *)in;
683 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
684 (struct net_device *)out, br_nf_forward_finish);
689 /* PF_BRIDGE/LOCAL_OUT ***********************************************
691 * This function sees both locally originated IP packets and forwarded
692 * IP packets (in both cases the destination device is a bridge
693 * device). It also sees bridged-and-DNAT'ed packets.
695 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
696 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
697 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
698 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
701 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
702 const struct net_device *in,
703 const struct net_device *out,
704 int (*okfn)(struct sk_buff *))
706 struct net_device *realindev;
707 struct sk_buff *skb = *pskb;
708 struct nf_bridge_info *nf_bridge;
713 nf_bridge = skb->nf_bridge;
714 if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
717 /* Bridged, take PF_BRIDGE/FORWARD.
718 * (see big note in front of br_nf_pre_routing_finish) */
719 nf_bridge->physoutdev = skb->dev;
720 realindev = nf_bridge->physindev;
722 if (nf_bridge->mask & BRNF_PKT_TYPE) {
723 skb->pkt_type = PACKET_OTHERHOST;
724 nf_bridge->mask ^= BRNF_PKT_TYPE;
726 if (skb->protocol == htons(ETH_P_8021Q)) {
727 skb_push(skb, VLAN_HLEN);
728 skb->network_header -= VLAN_HLEN;
731 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
736 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
738 if (skb->protocol == htons(ETH_P_IP) &&
739 skb->len > skb->dev->mtu &&
741 return ip_fragment(skb, br_dev_queue_push_xmit);
743 return br_dev_queue_push_xmit(skb);
746 /* PF_BRIDGE/POST_ROUTING ********************************************/
747 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
748 const struct net_device *in,
749 const struct net_device *out,
750 int (*okfn)(struct sk_buff *))
752 struct sk_buff *skb = *pskb;
753 struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
754 struct net_device *realoutdev = bridge_parent(skb->dev);
757 #ifdef CONFIG_NETFILTER_DEBUG
758 /* Be very paranoid. This probably won't happen anymore, but let's
759 * keep the check just to be sure... */
760 if (skb_mac_header(skb) < skb->head ||
761 skb_mac_header(skb) + ETH_HLEN > skb->data) {
762 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
763 "bad mac.raw pointer.\n");
774 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
779 #ifdef CONFIG_NETFILTER_DEBUG
780 if (skb->dst == NULL) {
781 printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n");
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 == htons(ETH_P_8021Q)) {
794 skb_pull(skb, VLAN_HLEN);
795 skb->network_header += 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_nf_dev_queue_xmit);
809 #ifdef CONFIG_NETFILTER_DEBUG
811 if (skb->dev != NULL) {
812 printk("[%s]", skb->dev->name);
814 printk("[%s]", realoutdev->name);
816 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb_mac_header(skb),
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,
828 const struct net_device *out,
829 int (*okfn)(struct sk_buff *))
831 if ((*pskb)->nf_bridge &&
832 !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
839 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
840 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
841 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
842 * ip_refrag() can return NF_STOLEN. */
843 static struct nf_hook_ops br_nf_ops[] = {
844 { .hook = br_nf_pre_routing,
845 .owner = THIS_MODULE,
847 .hooknum = NF_BR_PRE_ROUTING,
848 .priority = NF_BR_PRI_BRNF, },
849 { .hook = br_nf_local_in,
850 .owner = THIS_MODULE,
852 .hooknum = NF_BR_LOCAL_IN,
853 .priority = NF_BR_PRI_BRNF, },
854 { .hook = br_nf_forward_ip,
855 .owner = THIS_MODULE,
857 .hooknum = NF_BR_FORWARD,
858 .priority = NF_BR_PRI_BRNF - 1, },
859 { .hook = br_nf_forward_arp,
860 .owner = THIS_MODULE,
862 .hooknum = NF_BR_FORWARD,
863 .priority = NF_BR_PRI_BRNF, },
864 { .hook = br_nf_local_out,
865 .owner = THIS_MODULE,
867 .hooknum = NF_BR_LOCAL_OUT,
868 .priority = NF_BR_PRI_FIRST, },
869 { .hook = br_nf_post_routing,
870 .owner = THIS_MODULE,
872 .hooknum = NF_BR_POST_ROUTING,
873 .priority = NF_BR_PRI_LAST, },
874 { .hook = ip_sabotage_in,
875 .owner = THIS_MODULE,
877 .hooknum = NF_IP_PRE_ROUTING,
878 .priority = NF_IP_PRI_FIRST, },
879 { .hook = ip_sabotage_in,
880 .owner = THIS_MODULE,
882 .hooknum = NF_IP6_PRE_ROUTING,
883 .priority = NF_IP6_PRI_FIRST, },
888 int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
889 void __user * buffer, size_t * lenp, loff_t * ppos)
893 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
895 if (write && *(int *)(ctl->data))
896 *(int *)(ctl->data) = 1;
900 static ctl_table brnf_table[] = {
902 .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES,
903 .procname = "bridge-nf-call-arptables",
904 .data = &brnf_call_arptables,
905 .maxlen = sizeof(int),
907 .proc_handler = &brnf_sysctl_call_tables,
910 .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES,
911 .procname = "bridge-nf-call-iptables",
912 .data = &brnf_call_iptables,
913 .maxlen = sizeof(int),
915 .proc_handler = &brnf_sysctl_call_tables,
918 .ctl_name = NET_BRIDGE_NF_CALL_IP6TABLES,
919 .procname = "bridge-nf-call-ip6tables",
920 .data = &brnf_call_ip6tables,
921 .maxlen = sizeof(int),
923 .proc_handler = &brnf_sysctl_call_tables,
926 .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
927 .procname = "bridge-nf-filter-vlan-tagged",
928 .data = &brnf_filter_vlan_tagged,
929 .maxlen = sizeof(int),
931 .proc_handler = &brnf_sysctl_call_tables,
936 static ctl_table brnf_bridge_table[] = {
938 .ctl_name = NET_BRIDGE,
939 .procname = "bridge",
946 static ctl_table brnf_net_table[] = {
951 .child = brnf_bridge_table,
957 int __init br_netfilter_init(void)
961 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
965 brnf_sysctl_header = register_sysctl_table(brnf_net_table);
966 if (brnf_sysctl_header == NULL) {
968 "br_netfilter: can't register to sysctl.\n");
969 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
973 printk(KERN_NOTICE "Bridge firewalling registered\n");
977 void br_netfilter_fini(void)
979 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
981 unregister_sysctl_table(brnf_sysctl_header);