3 * Linux ethernet bridge
6 * Lennert Buytenhek <buytenh@gnu.org>
7 * Bart De Schuymer <bdschuym@pandora.be>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 * Lennert dedicates this file to Kerstin Wurdinger.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <linux/if_pppox.h>
27 #include <linux/ppp_defs.h>
28 #include <linux/netfilter_bridge.h>
29 #include <linux/netfilter_ipv4.h>
30 #include <linux/netfilter_ipv6.h>
31 #include <linux/netfilter_arp.h>
32 #include <linux/in_route.h>
33 #include <linux/inetdevice.h>
37 #include <net/route.h>
38 #include <net/netfilter/br_netfilter.h>
40 #include <asm/uaccess.h>
41 #include "br_private.h"
43 #include <linux/sysctl.h>
46 #define skb_origaddr(skb) (((struct bridge_skb_cb *) \
47 (skb->nf_bridge->data))->daddr.ipv4)
48 #define store_orig_dstaddr(skb) (skb_origaddr(skb) = ip_hdr(skb)->daddr)
49 #define dnat_took_place(skb) (skb_origaddr(skb) != ip_hdr(skb)->daddr)
52 static struct ctl_table_header *brnf_sysctl_header;
53 static int brnf_call_iptables __read_mostly = 1;
54 static int brnf_call_ip6tables __read_mostly = 1;
55 static int brnf_call_arptables __read_mostly = 1;
56 static int brnf_filter_vlan_tagged __read_mostly = 0;
57 static int brnf_filter_pppoe_tagged __read_mostly = 0;
58 static int brnf_pass_vlan_indev __read_mostly = 0;
60 #define brnf_call_iptables 1
61 #define brnf_call_ip6tables 1
62 #define brnf_call_arptables 1
63 #define brnf_filter_vlan_tagged 0
64 #define brnf_filter_pppoe_tagged 0
65 #define brnf_pass_vlan_indev 0
69 (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
71 #define IS_IPV6(skb) \
72 (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
75 (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
77 static inline __be16 vlan_proto(const struct sk_buff *skb)
79 if (vlan_tx_tag_present(skb))
81 else if (skb->protocol == htons(ETH_P_8021Q))
82 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
87 #define IS_VLAN_IP(skb) \
88 (vlan_proto(skb) == htons(ETH_P_IP) && \
89 brnf_filter_vlan_tagged)
91 #define IS_VLAN_IPV6(skb) \
92 (vlan_proto(skb) == htons(ETH_P_IPV6) && \
93 brnf_filter_vlan_tagged)
95 #define IS_VLAN_ARP(skb) \
96 (vlan_proto(skb) == htons(ETH_P_ARP) && \
97 brnf_filter_vlan_tagged)
99 static inline __be16 pppoe_proto(const struct sk_buff *skb)
101 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
102 sizeof(struct pppoe_hdr)));
105 #define IS_PPPOE_IP(skb) \
106 (skb->protocol == htons(ETH_P_PPP_SES) && \
107 pppoe_proto(skb) == htons(PPP_IP) && \
108 brnf_filter_pppoe_tagged)
110 #define IS_PPPOE_IPV6(skb) \
111 (skb->protocol == htons(ETH_P_PPP_SES) && \
112 pppoe_proto(skb) == htons(PPP_IPV6) && \
113 brnf_filter_pppoe_tagged)
115 static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
117 struct net_bridge_port *port;
119 port = br_port_get_rcu(dev);
120 return port ? &port->br->fake_rtable : NULL;
123 static inline struct net_device *bridge_parent(const struct net_device *dev)
125 struct net_bridge_port *port;
127 port = br_port_get_rcu(dev);
128 return port ? port->br->dev : NULL;
131 static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
133 skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
134 if (likely(skb->nf_bridge))
135 atomic_set(&(skb->nf_bridge->use), 1);
137 return skb->nf_bridge;
140 static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
142 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
144 if (atomic_read(&nf_bridge->use) > 1) {
145 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
148 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
149 atomic_set(&tmp->use, 1);
151 nf_bridge_put(nf_bridge);
157 static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
159 unsigned int len = nf_bridge_encap_header_len(skb);
162 skb->network_header -= len;
165 static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
167 unsigned int len = nf_bridge_encap_header_len(skb);
170 skb->network_header += len;
173 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
175 unsigned int len = nf_bridge_encap_header_len(skb);
177 skb_pull_rcsum(skb, len);
178 skb->network_header += len;
181 static inline void nf_bridge_save_header(struct sk_buff *skb)
183 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
185 skb_copy_from_linear_data_offset(skb, -header_size,
186 skb->nf_bridge->data, header_size);
189 /* When handing a packet over to the IP layer
190 * check whether we have a skb that is in the
194 static int br_parse_ip_options(struct sk_buff *skb)
196 struct ip_options *opt;
197 const struct iphdr *iph;
198 struct net_device *dev = skb->dev;
201 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
205 opt = &(IPCB(skb)->opt);
207 /* Basic sanity checks */
208 if (iph->ihl < 5 || iph->version != 4)
211 if (!pskb_may_pull(skb, iph->ihl*4))
215 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
218 len = ntohs(iph->tot_len);
219 if (skb->len < len) {
220 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INTRUNCATEDPKTS);
222 } else if (len < (iph->ihl*4))
225 if (pskb_trim_rcsum(skb, len)) {
226 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
230 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
234 opt->optlen = iph->ihl*4 - sizeof(struct iphdr);
235 if (ip_options_compile(dev_net(dev), opt, skb))
238 /* Check correct handling of SRR option */
239 if (unlikely(opt->srr)) {
240 struct in_device *in_dev = __in_dev_get_rcu(dev);
241 if (in_dev && !IN_DEV_SOURCE_ROUTE(in_dev))
244 if (ip_options_rcv_srr(skb))
251 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
256 /* PF_BRIDGE/PRE_ROUTING *********************************************/
257 /* Undo the changes made for ip6tables PREROUTING and continue the
258 * bridge PRE_ROUTING hook. */
259 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
261 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
264 if (nf_bridge->mask & BRNF_PKT_TYPE) {
265 skb->pkt_type = PACKET_OTHERHOST;
266 nf_bridge->mask ^= BRNF_PKT_TYPE;
268 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
270 rt = bridge_parent_rtable(nf_bridge->physindev);
275 skb_dst_set_noref(skb, &rt->dst);
277 skb->dev = nf_bridge->physindev;
278 nf_bridge_update_protocol(skb);
279 nf_bridge_push_encap_header(skb);
280 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
281 br_handle_frame_finish, 1);
286 /* Obtain the correct destination MAC address, while preserving the original
287 * source MAC address. If we already know this address, we just copy it. If we
288 * don't, we use the neighbour framework to find out. In both cases, we make
289 * sure that br_handle_frame_finish() is called afterwards.
291 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
293 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
294 struct neighbour *neigh;
295 struct dst_entry *dst;
297 skb->dev = bridge_parent(skb->dev);
301 neigh = dst_neigh_lookup_skb(dst, skb);
305 if (neigh->hh.hh_len) {
306 neigh_hh_bridge(&neigh->hh, skb);
307 skb->dev = nf_bridge->physindev;
308 ret = br_handle_frame_finish(skb);
310 /* the neighbour function below overwrites the complete
311 * MAC header, so we save the Ethernet source address and
314 skb_copy_from_linear_data_offset(skb,
315 -(ETH_HLEN-ETH_ALEN),
316 skb->nf_bridge->data,
318 /* tell br_dev_xmit to continue with forwarding */
319 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
320 /* FIXME Need to refragment */
321 ret = neigh->output(neigh, skb);
323 neigh_release(neigh);
331 /* This requires some explaining. If DNAT has taken place,
332 * we will need to fix up the destination Ethernet address.
334 * There are two cases to consider:
335 * 1. The packet was DNAT'ed to a device in the same bridge
336 * port group as it was received on. We can still bridge
338 * 2. The packet was DNAT'ed to a different device, either
339 * a non-bridged device or another bridge port group.
340 * The packet will need to be routed.
342 * The correct way of distinguishing between these two cases is to
343 * call ip_route_input() and to look at skb->dst->dev, which is
344 * changed to the destination device if ip_route_input() succeeds.
346 * Let's first consider the case that ip_route_input() succeeds:
348 * If the output device equals the logical bridge device the packet
349 * came in on, we can consider this bridging. The corresponding MAC
350 * address will be obtained in br_nf_pre_routing_finish_bridge.
351 * Otherwise, the packet is considered to be routed and we just
352 * change the destination MAC address so that the packet will
353 * later be passed up to the IP stack to be routed. For a redirected
354 * packet, ip_route_input() will give back the localhost as output device,
355 * which differs from the bridge device.
357 * Let's now consider the case that ip_route_input() fails:
359 * This can be because the destination address is martian, in which case
360 * the packet will be dropped.
361 * If IP forwarding is disabled, ip_route_input() will fail, while
362 * ip_route_output_key() can return success. The source
363 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
364 * thinks we're handling a locally generated packet and won't care
365 * if IP forwarding is enabled. If the output device equals the logical bridge
366 * device, we proceed as if ip_route_input() succeeded. If it differs from the
367 * logical bridge port or if ip_route_output_key() fails we drop the packet.
369 static int br_nf_pre_routing_finish(struct sk_buff *skb)
371 struct net_device *dev = skb->dev;
372 struct iphdr *iph = ip_hdr(skb);
373 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
378 frag_max_size = IPCB(skb)->frag_max_size;
379 BR_INPUT_SKB_CB(skb)->frag_max_size = frag_max_size;
381 if (nf_bridge->mask & BRNF_PKT_TYPE) {
382 skb->pkt_type = PACKET_OTHERHOST;
383 nf_bridge->mask ^= BRNF_PKT_TYPE;
385 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
386 if (dnat_took_place(skb)) {
387 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
388 struct in_device *in_dev = __in_dev_get_rcu(dev);
390 /* If err equals -EHOSTUNREACH the error is due to a
391 * martian destination or due to the fact that
392 * forwarding is disabled. For most martian packets,
393 * ip_route_output_key() will fail. It won't fail for 2 types of
394 * martian destinations: loopback destinations and destination
395 * 0.0.0.0. In both cases the packet will be dropped because the
396 * destination is the loopback device and not the bridge. */
397 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
400 rt = ip_route_output(dev_net(dev), iph->daddr, 0,
401 RT_TOS(iph->tos), 0);
403 /* - Bridged-and-DNAT'ed traffic doesn't
404 * require ip_forwarding. */
405 if (rt->dst.dev == dev) {
406 skb_dst_set(skb, &rt->dst);
415 if (skb_dst(skb)->dev == dev) {
417 skb->dev = nf_bridge->physindev;
418 nf_bridge_update_protocol(skb);
419 nf_bridge_push_encap_header(skb);
420 NF_HOOK_THRESH(NFPROTO_BRIDGE,
423 br_nf_pre_routing_finish_bridge,
427 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
428 skb->pkt_type = PACKET_HOST;
431 rt = bridge_parent_rtable(nf_bridge->physindev);
436 skb_dst_set_noref(skb, &rt->dst);
439 skb->dev = nf_bridge->physindev;
440 nf_bridge_update_protocol(skb);
441 nf_bridge_push_encap_header(skb);
442 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
443 br_handle_frame_finish, 1);
448 static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
450 struct net_device *vlan, *br;
452 br = bridge_parent(dev);
453 if (brnf_pass_vlan_indev == 0 || !vlan_tx_tag_present(skb))
456 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
457 vlan_tx_tag_get(skb) & VLAN_VID_MASK);
459 return vlan ? vlan : br;
462 /* Some common code for IPv4/IPv6 */
463 static struct net_device *setup_pre_routing(struct sk_buff *skb)
465 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
467 if (skb->pkt_type == PACKET_OTHERHOST) {
468 skb->pkt_type = PACKET_HOST;
469 nf_bridge->mask |= BRNF_PKT_TYPE;
472 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
473 nf_bridge->physindev = skb->dev;
474 skb->dev = brnf_get_logical_dev(skb, skb->dev);
475 if (skb->protocol == htons(ETH_P_8021Q))
476 nf_bridge->mask |= BRNF_8021Q;
477 else if (skb->protocol == htons(ETH_P_PPP_SES))
478 nf_bridge->mask |= BRNF_PPPoE;
480 /* Must drop socket now because of tproxy. */
485 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
486 static int check_hbh_len(struct sk_buff *skb)
488 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
490 const unsigned char *nh = skb_network_header(skb);
492 int len = (raw[1] + 1) << 3;
494 if ((raw + len) - skb->data > skb_headlen(skb))
501 int optlen = nh[off + 1] + 2;
512 if (nh[off + 1] != 4 || (off & 3) != 2)
514 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
515 if (pkt_len <= IPV6_MAXPLEN ||
516 ipv6_hdr(skb)->payload_len)
518 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
520 if (pskb_trim_rcsum(skb,
521 pkt_len + sizeof(struct ipv6hdr)))
523 nh = skb_network_header(skb);
540 /* Replicate the checks that IPv6 does on packet reception and pass the packet
541 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
542 static unsigned int br_nf_pre_routing_ipv6(const struct nf_hook_ops *ops,
544 const struct net_device *in,
545 const struct net_device *out,
546 int (*okfn)(struct sk_buff *))
548 const struct ipv6hdr *hdr;
551 if (skb->len < sizeof(struct ipv6hdr))
554 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
559 if (hdr->version != 6)
562 pkt_len = ntohs(hdr->payload_len);
564 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
565 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
567 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
570 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
573 nf_bridge_put(skb->nf_bridge);
574 if (!nf_bridge_alloc(skb))
576 if (!setup_pre_routing(skb))
579 skb->protocol = htons(ETH_P_IPV6);
580 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
581 br_nf_pre_routing_finish_ipv6);
586 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
587 * Replicate the checks that IPv4 does on packet reception.
588 * Set skb->dev to the bridge device (i.e. parent of the
589 * receiving device) to make netfilter happy, the REDIRECT
590 * target in particular. Save the original destination IP
591 * address to be able to detect DNAT afterwards. */
592 static unsigned int br_nf_pre_routing(const struct nf_hook_ops *ops,
594 const struct net_device *in,
595 const struct net_device *out,
596 int (*okfn)(struct sk_buff *))
598 struct net_bridge_port *p;
599 struct net_bridge *br;
600 __u32 len = nf_bridge_encap_header_len(skb);
602 if (unlikely(!pskb_may_pull(skb, len)))
605 p = br_port_get_rcu(in);
610 if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
611 if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
614 nf_bridge_pull_encap_header_rcsum(skb);
615 return br_nf_pre_routing_ipv6(ops, skb, in, out, okfn);
618 if (!brnf_call_iptables && !br->nf_call_iptables)
621 if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
624 nf_bridge_pull_encap_header_rcsum(skb);
626 if (br_parse_ip_options(skb))
629 nf_bridge_put(skb->nf_bridge);
630 if (!nf_bridge_alloc(skb))
632 if (!setup_pre_routing(skb))
634 store_orig_dstaddr(skb);
635 skb->protocol = htons(ETH_P_IP);
637 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
638 br_nf_pre_routing_finish);
644 /* PF_BRIDGE/LOCAL_IN ************************************************/
645 /* The packet is locally destined, which requires a real
646 * dst_entry, so detach the fake one. On the way up, the
647 * packet would pass through PRE_ROUTING again (which already
648 * took place when the packet entered the bridge), but we
649 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
650 * prevent this from happening. */
651 static unsigned int br_nf_local_in(const struct nf_hook_ops *ops,
653 const struct net_device *in,
654 const struct net_device *out,
655 int (*okfn)(struct sk_buff *))
657 br_drop_fake_rtable(skb);
661 /* PF_BRIDGE/FORWARD *************************************************/
662 static int br_nf_forward_finish(struct sk_buff *skb)
664 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
665 struct net_device *in;
667 if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
668 in = nf_bridge->physindev;
669 if (nf_bridge->mask & BRNF_PKT_TYPE) {
670 skb->pkt_type = PACKET_OTHERHOST;
671 nf_bridge->mask ^= BRNF_PKT_TYPE;
673 nf_bridge_update_protocol(skb);
675 in = *((struct net_device **)(skb->cb));
677 nf_bridge_push_encap_header(skb);
679 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, in,
680 skb->dev, br_forward_finish, 1);
685 /* This is the 'purely bridged' case. For IP, we pass the packet to
686 * netfilter with indev and outdev set to the bridge device,
687 * but we are still able to filter on the 'real' indev/outdev
688 * because of the physdev module. For ARP, indev and outdev are the
690 static unsigned int br_nf_forward_ip(const struct nf_hook_ops *ops,
692 const struct net_device *in,
693 const struct net_device *out,
694 int (*okfn)(struct sk_buff *))
696 struct nf_bridge_info *nf_bridge;
697 struct net_device *parent;
703 /* Need exclusive nf_bridge_info since we might have multiple
704 * different physoutdevs. */
705 if (!nf_bridge_unshare(skb))
708 parent = bridge_parent(out);
712 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
714 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
719 nf_bridge_pull_encap_header(skb);
721 nf_bridge = skb->nf_bridge;
722 if (skb->pkt_type == PACKET_OTHERHOST) {
723 skb->pkt_type = PACKET_HOST;
724 nf_bridge->mask |= BRNF_PKT_TYPE;
727 if (pf == NFPROTO_IPV4 && br_parse_ip_options(skb))
730 /* The physdev module checks on this */
731 nf_bridge->mask |= BRNF_BRIDGED;
732 nf_bridge->physoutdev = skb->dev;
733 if (pf == NFPROTO_IPV4)
734 skb->protocol = htons(ETH_P_IP);
736 skb->protocol = htons(ETH_P_IPV6);
738 NF_HOOK(pf, NF_INET_FORWARD, skb, brnf_get_logical_dev(skb, in), parent,
739 br_nf_forward_finish);
744 static unsigned int br_nf_forward_arp(const struct nf_hook_ops *ops,
746 const struct net_device *in,
747 const struct net_device *out,
748 int (*okfn)(struct sk_buff *))
750 struct net_bridge_port *p;
751 struct net_bridge *br;
752 struct net_device **d = (struct net_device **)(skb->cb);
754 p = br_port_get_rcu(out);
759 if (!brnf_call_arptables && !br->nf_call_arptables)
763 if (!IS_VLAN_ARP(skb))
765 nf_bridge_pull_encap_header(skb);
768 if (arp_hdr(skb)->ar_pln != 4) {
769 if (IS_VLAN_ARP(skb))
770 nf_bridge_push_encap_header(skb);
773 *d = (struct net_device *)in;
774 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
775 (struct net_device *)out, br_nf_forward_finish);
780 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4)
781 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
786 /* This is wrong! We should preserve the original fragment
787 * boundaries by preserving frag_list rather than refragmenting.
789 if (skb->protocol == htons(ETH_P_IP) &&
790 skb->len + nf_bridge_mtu_reduction(skb) > skb->dev->mtu &&
792 frag_max_size = BR_INPUT_SKB_CB(skb)->frag_max_size;
793 if (br_parse_ip_options(skb))
794 /* Drop invalid packet */
796 IPCB(skb)->frag_max_size = frag_max_size;
797 ret = ip_fragment(skb, br_dev_queue_push_xmit);
799 ret = br_dev_queue_push_xmit(skb);
804 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
806 return br_dev_queue_push_xmit(skb);
810 /* PF_BRIDGE/POST_ROUTING ********************************************/
811 static unsigned int br_nf_post_routing(const struct nf_hook_ops *ops,
813 const struct net_device *in,
814 const struct net_device *out,
815 int (*okfn)(struct sk_buff *))
817 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
818 struct net_device *realoutdev = bridge_parent(skb->dev);
821 if (!nf_bridge || !(nf_bridge->mask & BRNF_BRIDGED))
827 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
829 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
834 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
835 * about the value of skb->pkt_type. */
836 if (skb->pkt_type == PACKET_OTHERHOST) {
837 skb->pkt_type = PACKET_HOST;
838 nf_bridge->mask |= BRNF_PKT_TYPE;
841 nf_bridge_pull_encap_header(skb);
842 nf_bridge_save_header(skb);
843 if (pf == NFPROTO_IPV4)
844 skb->protocol = htons(ETH_P_IP);
846 skb->protocol = htons(ETH_P_IPV6);
848 NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
849 br_nf_dev_queue_xmit);
854 /* IP/SABOTAGE *****************************************************/
855 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
856 * for the second time. */
857 static unsigned int ip_sabotage_in(const struct nf_hook_ops *ops,
859 const struct net_device *in,
860 const struct net_device *out,
861 int (*okfn)(struct sk_buff *))
863 if (skb->nf_bridge &&
864 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
871 void br_netfilter_enable(void)
874 EXPORT_SYMBOL_GPL(br_netfilter_enable);
876 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
877 * br_dev_queue_push_xmit is called afterwards */
878 static struct nf_hook_ops br_nf_ops[] __read_mostly = {
880 .hook = br_nf_pre_routing,
881 .owner = THIS_MODULE,
882 .pf = NFPROTO_BRIDGE,
883 .hooknum = NF_BR_PRE_ROUTING,
884 .priority = NF_BR_PRI_BRNF,
887 .hook = br_nf_local_in,
888 .owner = THIS_MODULE,
889 .pf = NFPROTO_BRIDGE,
890 .hooknum = NF_BR_LOCAL_IN,
891 .priority = NF_BR_PRI_BRNF,
894 .hook = br_nf_forward_ip,
895 .owner = THIS_MODULE,
896 .pf = NFPROTO_BRIDGE,
897 .hooknum = NF_BR_FORWARD,
898 .priority = NF_BR_PRI_BRNF - 1,
901 .hook = br_nf_forward_arp,
902 .owner = THIS_MODULE,
903 .pf = NFPROTO_BRIDGE,
904 .hooknum = NF_BR_FORWARD,
905 .priority = NF_BR_PRI_BRNF,
908 .hook = br_nf_post_routing,
909 .owner = THIS_MODULE,
910 .pf = NFPROTO_BRIDGE,
911 .hooknum = NF_BR_POST_ROUTING,
912 .priority = NF_BR_PRI_LAST,
915 .hook = ip_sabotage_in,
916 .owner = THIS_MODULE,
918 .hooknum = NF_INET_PRE_ROUTING,
919 .priority = NF_IP_PRI_FIRST,
922 .hook = ip_sabotage_in,
923 .owner = THIS_MODULE,
925 .hooknum = NF_INET_PRE_ROUTING,
926 .priority = NF_IP6_PRI_FIRST,
932 int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
933 void __user *buffer, size_t *lenp, loff_t *ppos)
937 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
939 if (write && *(int *)(ctl->data))
940 *(int *)(ctl->data) = 1;
944 static struct ctl_table brnf_table[] = {
946 .procname = "bridge-nf-call-arptables",
947 .data = &brnf_call_arptables,
948 .maxlen = sizeof(int),
950 .proc_handler = brnf_sysctl_call_tables,
953 .procname = "bridge-nf-call-iptables",
954 .data = &brnf_call_iptables,
955 .maxlen = sizeof(int),
957 .proc_handler = brnf_sysctl_call_tables,
960 .procname = "bridge-nf-call-ip6tables",
961 .data = &brnf_call_ip6tables,
962 .maxlen = sizeof(int),
964 .proc_handler = brnf_sysctl_call_tables,
967 .procname = "bridge-nf-filter-vlan-tagged",
968 .data = &brnf_filter_vlan_tagged,
969 .maxlen = sizeof(int),
971 .proc_handler = brnf_sysctl_call_tables,
974 .procname = "bridge-nf-filter-pppoe-tagged",
975 .data = &brnf_filter_pppoe_tagged,
976 .maxlen = sizeof(int),
978 .proc_handler = brnf_sysctl_call_tables,
981 .procname = "bridge-nf-pass-vlan-input-dev",
982 .data = &brnf_pass_vlan_indev,
983 .maxlen = sizeof(int),
985 .proc_handler = brnf_sysctl_call_tables,
991 static int __init br_netfilter_init(void)
995 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1000 brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
1001 if (brnf_sysctl_header == NULL) {
1003 "br_netfilter: can't register to sysctl.\n");
1008 printk(KERN_NOTICE "Bridge firewalling registered\n");
1011 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1015 static void __exit br_netfilter_fini(void)
1017 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1018 #ifdef CONFIG_SYSCTL
1019 unregister_net_sysctl_table(brnf_sysctl_header);
1023 module_init(br_netfilter_init);
1024 module_exit(br_netfilter_fini);
1026 MODULE_LICENSE("GPL");
1027 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1028 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1029 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");