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/addrconf.h>
38 #include <net/route.h>
39 #include <net/netfilter/br_netfilter.h>
41 #include <asm/uaccess.h>
42 #include "br_private.h"
44 #include <linux/sysctl.h>
48 static struct ctl_table_header *brnf_sysctl_header;
49 static int brnf_call_iptables __read_mostly = 1;
50 static int brnf_call_ip6tables __read_mostly = 1;
51 static int brnf_call_arptables __read_mostly = 1;
52 static int brnf_filter_vlan_tagged __read_mostly;
53 static int brnf_filter_pppoe_tagged __read_mostly;
54 static int brnf_pass_vlan_indev __read_mostly;
56 #define brnf_call_iptables 1
57 #define brnf_call_ip6tables 1
58 #define brnf_call_arptables 1
59 #define brnf_filter_vlan_tagged 0
60 #define brnf_filter_pppoe_tagged 0
61 #define brnf_pass_vlan_indev 0
65 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
67 #define IS_IPV6(skb) \
68 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
71 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
73 static inline __be16 vlan_proto(const struct sk_buff *skb)
75 if (skb_vlan_tag_present(skb))
77 else if (skb->protocol == htons(ETH_P_8021Q))
78 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
83 #define IS_VLAN_IP(skb) \
84 (vlan_proto(skb) == htons(ETH_P_IP) && \
85 brnf_filter_vlan_tagged)
87 #define IS_VLAN_IPV6(skb) \
88 (vlan_proto(skb) == htons(ETH_P_IPV6) && \
89 brnf_filter_vlan_tagged)
91 #define IS_VLAN_ARP(skb) \
92 (vlan_proto(skb) == htons(ETH_P_ARP) && \
93 brnf_filter_vlan_tagged)
95 static inline __be16 pppoe_proto(const struct sk_buff *skb)
97 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
98 sizeof(struct pppoe_hdr)));
101 #define IS_PPPOE_IP(skb) \
102 (skb->protocol == htons(ETH_P_PPP_SES) && \
103 pppoe_proto(skb) == htons(PPP_IP) && \
104 brnf_filter_pppoe_tagged)
106 #define IS_PPPOE_IPV6(skb) \
107 (skb->protocol == htons(ETH_P_PPP_SES) && \
108 pppoe_proto(skb) == htons(PPP_IPV6) && \
109 brnf_filter_pppoe_tagged)
111 /* largest possible L2 header, see br_nf_dev_queue_xmit() */
112 #define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN)
114 struct brnf_frag_data {
115 char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH];
122 static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage);
124 static void nf_bridge_info_free(struct sk_buff *skb)
126 if (skb->nf_bridge) {
127 nf_bridge_put(skb->nf_bridge);
128 skb->nf_bridge = NULL;
132 static inline struct net_device *bridge_parent(const struct net_device *dev)
134 struct net_bridge_port *port;
136 port = br_port_get_rcu(dev);
137 return port ? port->br->dev : NULL;
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 unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
159 switch (skb->protocol) {
160 case __cpu_to_be16(ETH_P_8021Q):
162 case __cpu_to_be16(ETH_P_PPP_SES):
163 return PPPOE_SES_HLEN;
169 static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
171 unsigned int len = nf_bridge_encap_header_len(skb);
174 skb->network_header += len;
177 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
179 unsigned int len = nf_bridge_encap_header_len(skb);
181 skb_pull_rcsum(skb, len);
182 skb->network_header += len;
185 /* When handing a packet over to the IP layer
186 * check whether we have a skb that is in the
190 static int br_validate_ipv4(struct net *net, struct sk_buff *skb)
192 const struct iphdr *iph;
195 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
200 /* Basic sanity checks */
201 if (iph->ihl < 5 || iph->version != 4)
204 if (!pskb_may_pull(skb, iph->ihl*4))
208 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
211 len = ntohs(iph->tot_len);
212 if (skb->len < len) {
213 IP_INC_STATS_BH(net, IPSTATS_MIB_INTRUNCATEDPKTS);
215 } else if (len < (iph->ihl*4))
218 if (pskb_trim_rcsum(skb, len)) {
219 IP_INC_STATS_BH(net, IPSTATS_MIB_INDISCARDS);
223 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
224 /* We should really parse IP options here but until
225 * somebody who actually uses IP options complains to
226 * us we'll just silently ignore the options because
232 IP_INC_STATS_BH(net, IPSTATS_MIB_INHDRERRORS);
237 void nf_bridge_update_protocol(struct sk_buff *skb)
239 switch (skb->nf_bridge->orig_proto) {
240 case BRNF_PROTO_8021Q:
241 skb->protocol = htons(ETH_P_8021Q);
243 case BRNF_PROTO_PPPOE:
244 skb->protocol = htons(ETH_P_PPP_SES);
246 case BRNF_PROTO_UNCHANGED:
251 /* Obtain the correct destination MAC address, while preserving the original
252 * source MAC address. If we already know this address, we just copy it. If we
253 * don't, we use the neighbour framework to find out. In both cases, we make
254 * sure that br_handle_frame_finish() is called afterwards.
256 int br_nf_pre_routing_finish_bridge(struct net *net, struct sock *sk, struct sk_buff *skb)
258 struct neighbour *neigh;
259 struct dst_entry *dst;
261 skb->dev = bridge_parent(skb->dev);
265 neigh = dst_neigh_lookup_skb(dst, skb);
267 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
270 if (neigh->hh.hh_len) {
271 neigh_hh_bridge(&neigh->hh, skb);
272 skb->dev = nf_bridge->physindev;
273 ret = br_handle_frame_finish(net, sk, skb);
275 /* the neighbour function below overwrites the complete
276 * MAC header, so we save the Ethernet source address and
279 skb_copy_from_linear_data_offset(skb,
280 -(ETH_HLEN-ETH_ALEN),
281 nf_bridge->neigh_header,
283 /* tell br_dev_xmit to continue with forwarding */
284 nf_bridge->bridged_dnat = 1;
285 /* FIXME Need to refragment */
286 ret = neigh->output(neigh, skb);
288 neigh_release(neigh);
297 br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb,
298 const struct nf_bridge_info *nf_bridge)
300 return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
303 /* This requires some explaining. If DNAT has taken place,
304 * we will need to fix up the destination Ethernet address.
305 * This is also true when SNAT takes place (for the reply direction).
307 * There are two cases to consider:
308 * 1. The packet was DNAT'ed to a device in the same bridge
309 * port group as it was received on. We can still bridge
311 * 2. The packet was DNAT'ed to a different device, either
312 * a non-bridged device or another bridge port group.
313 * The packet will need to be routed.
315 * The correct way of distinguishing between these two cases is to
316 * call ip_route_input() and to look at skb->dst->dev, which is
317 * changed to the destination device if ip_route_input() succeeds.
319 * Let's first consider the case that ip_route_input() succeeds:
321 * If the output device equals the logical bridge device the packet
322 * came in on, we can consider this bridging. The corresponding MAC
323 * address will be obtained in br_nf_pre_routing_finish_bridge.
324 * Otherwise, the packet is considered to be routed and we just
325 * change the destination MAC address so that the packet will
326 * later be passed up to the IP stack to be routed. For a redirected
327 * packet, ip_route_input() will give back the localhost as output device,
328 * which differs from the bridge device.
330 * Let's now consider the case that ip_route_input() fails:
332 * This can be because the destination address is martian, in which case
333 * the packet will be dropped.
334 * If IP forwarding is disabled, ip_route_input() will fail, while
335 * ip_route_output_key() can return success. The source
336 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
337 * thinks we're handling a locally generated packet and won't care
338 * if IP forwarding is enabled. If the output device equals the logical bridge
339 * device, we proceed as if ip_route_input() succeeded. If it differs from the
340 * logical bridge port or if ip_route_output_key() fails we drop the packet.
342 static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
344 struct net_device *dev = skb->dev;
345 struct iphdr *iph = ip_hdr(skb);
346 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
350 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
352 if (nf_bridge->pkt_otherhost) {
353 skb->pkt_type = PACKET_OTHERHOST;
354 nf_bridge->pkt_otherhost = false;
356 nf_bridge->in_prerouting = 0;
357 if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) {
358 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
359 struct in_device *in_dev = __in_dev_get_rcu(dev);
361 /* If err equals -EHOSTUNREACH the error is due to a
362 * martian destination or due to the fact that
363 * forwarding is disabled. For most martian packets,
364 * ip_route_output_key() will fail. It won't fail for 2 types of
365 * martian destinations: loopback destinations and destination
366 * 0.0.0.0. In both cases the packet will be dropped because the
367 * destination is the loopback device and not the bridge. */
368 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
371 rt = ip_route_output(net, iph->daddr, 0,
372 RT_TOS(iph->tos), 0);
374 /* - Bridged-and-DNAT'ed traffic doesn't
375 * require ip_forwarding. */
376 if (rt->dst.dev == dev) {
377 skb_dst_set(skb, &rt->dst);
386 if (skb_dst(skb)->dev == dev) {
388 skb->dev = nf_bridge->physindev;
389 nf_bridge_update_protocol(skb);
390 nf_bridge_push_encap_header(skb);
391 NF_HOOK_THRESH(NFPROTO_BRIDGE,
393 net, sk, skb, skb->dev, NULL,
394 br_nf_pre_routing_finish_bridge,
398 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
399 skb->pkt_type = PACKET_HOST;
402 rt = bridge_parent_rtable(nf_bridge->physindev);
407 skb_dst_set_noref(skb, &rt->dst);
410 skb->dev = nf_bridge->physindev;
411 nf_bridge_update_protocol(skb);
412 nf_bridge_push_encap_header(skb);
413 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, net, sk, skb,
415 br_handle_frame_finish, 1);
420 static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
422 struct net_device *vlan, *br;
424 br = bridge_parent(dev);
425 if (brnf_pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
428 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
429 skb_vlan_tag_get(skb) & VLAN_VID_MASK);
431 return vlan ? vlan : br;
434 /* Some common code for IPv4/IPv6 */
435 struct net_device *setup_pre_routing(struct sk_buff *skb)
437 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
439 if (skb->pkt_type == PACKET_OTHERHOST) {
440 skb->pkt_type = PACKET_HOST;
441 nf_bridge->pkt_otherhost = true;
444 nf_bridge->in_prerouting = 1;
445 nf_bridge->physindev = skb->dev;
446 skb->dev = brnf_get_logical_dev(skb, skb->dev);
448 if (skb->protocol == htons(ETH_P_8021Q))
449 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
450 else if (skb->protocol == htons(ETH_P_PPP_SES))
451 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
453 /* Must drop socket now because of tproxy. */
458 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
459 * Replicate the checks that IPv4 does on packet reception.
460 * Set skb->dev to the bridge device (i.e. parent of the
461 * receiving device) to make netfilter happy, the REDIRECT
462 * target in particular. Save the original destination IP
463 * address to be able to detect DNAT afterwards. */
464 static unsigned int br_nf_pre_routing(void *priv,
466 const struct nf_hook_state *state)
468 struct nf_bridge_info *nf_bridge;
469 struct net_bridge_port *p;
470 struct net_bridge *br;
471 __u32 len = nf_bridge_encap_header_len(skb);
473 if (unlikely(!pskb_may_pull(skb, len)))
476 p = br_port_get_rcu(state->in);
481 if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
482 if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
485 nf_bridge_pull_encap_header_rcsum(skb);
486 return br_nf_pre_routing_ipv6(priv, skb, state);
489 if (!brnf_call_iptables && !br->nf_call_iptables)
492 if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
495 nf_bridge_pull_encap_header_rcsum(skb);
497 if (br_validate_ipv4(state->net, skb))
500 nf_bridge_put(skb->nf_bridge);
501 if (!nf_bridge_alloc(skb))
503 if (!setup_pre_routing(skb))
506 nf_bridge = nf_bridge_info_get(skb);
507 nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
509 skb->protocol = htons(ETH_P_IP);
511 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->net, state->sk, skb,
513 br_nf_pre_routing_finish);
519 /* PF_BRIDGE/LOCAL_IN ************************************************/
520 /* The packet is locally destined, which requires a real
521 * dst_entry, so detach the fake one. On the way up, the
522 * packet would pass through PRE_ROUTING again (which already
523 * took place when the packet entered the bridge), but we
524 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
525 * prevent this from happening. */
526 static unsigned int br_nf_local_in(void *priv,
528 const struct nf_hook_state *state)
530 br_drop_fake_rtable(skb);
534 /* PF_BRIDGE/FORWARD *************************************************/
535 static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
537 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
538 struct net_device *in;
540 if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
542 if (skb->protocol == htons(ETH_P_IP))
543 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
545 if (skb->protocol == htons(ETH_P_IPV6))
546 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
548 in = nf_bridge->physindev;
549 if (nf_bridge->pkt_otherhost) {
550 skb->pkt_type = PACKET_OTHERHOST;
551 nf_bridge->pkt_otherhost = false;
553 nf_bridge_update_protocol(skb);
555 in = *((struct net_device **)(skb->cb));
557 nf_bridge_push_encap_header(skb);
559 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, net, sk, skb,
560 in, skb->dev, br_forward_finish, 1);
565 /* This is the 'purely bridged' case. For IP, we pass the packet to
566 * netfilter with indev and outdev set to the bridge device,
567 * but we are still able to filter on the 'real' indev/outdev
568 * because of the physdev module. For ARP, indev and outdev are the
570 static unsigned int br_nf_forward_ip(void *priv,
572 const struct nf_hook_state *state)
574 struct nf_bridge_info *nf_bridge;
575 struct net_device *parent;
581 /* Need exclusive nf_bridge_info since we might have multiple
582 * different physoutdevs. */
583 if (!nf_bridge_unshare(skb))
586 nf_bridge = nf_bridge_info_get(skb);
590 parent = bridge_parent(state->out);
594 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
596 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
601 nf_bridge_pull_encap_header(skb);
603 if (skb->pkt_type == PACKET_OTHERHOST) {
604 skb->pkt_type = PACKET_HOST;
605 nf_bridge->pkt_otherhost = true;
608 if (pf == NFPROTO_IPV4) {
609 if (br_validate_ipv4(state->net, skb))
611 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
614 if (pf == NFPROTO_IPV6) {
615 if (br_validate_ipv6(state->net, skb))
617 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
620 nf_bridge->physoutdev = skb->dev;
621 if (pf == NFPROTO_IPV4)
622 skb->protocol = htons(ETH_P_IP);
624 skb->protocol = htons(ETH_P_IPV6);
626 NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb,
627 brnf_get_logical_dev(skb, state->in),
628 parent, br_nf_forward_finish);
633 static unsigned int br_nf_forward_arp(void *priv,
635 const struct nf_hook_state *state)
637 struct net_bridge_port *p;
638 struct net_bridge *br;
639 struct net_device **d = (struct net_device **)(skb->cb);
641 p = br_port_get_rcu(state->out);
646 if (!brnf_call_arptables && !br->nf_call_arptables)
650 if (!IS_VLAN_ARP(skb))
652 nf_bridge_pull_encap_header(skb);
655 if (arp_hdr(skb)->ar_pln != 4) {
656 if (IS_VLAN_ARP(skb))
657 nf_bridge_push_encap_header(skb);
661 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb,
662 state->in, state->out, br_nf_forward_finish);
667 static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
669 struct brnf_frag_data *data;
672 data = this_cpu_ptr(&brnf_frag_data_storage);
673 err = skb_cow_head(skb, data->size);
680 if (data->vlan_tci) {
681 skb->vlan_tci = data->vlan_tci;
682 skb->vlan_proto = data->vlan_proto;
685 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
686 __skb_push(skb, data->encap_size);
688 nf_bridge_info_free(skb);
689 return br_dev_queue_push_xmit(net, sk, skb);
693 br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
694 int (*output)(struct net *, struct sock *, struct sk_buff *))
696 unsigned int mtu = ip_skb_dst_mtu(skb);
697 struct iphdr *iph = ip_hdr(skb);
699 if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
700 (IPCB(skb)->frag_max_size &&
701 IPCB(skb)->frag_max_size > mtu))) {
702 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
707 return ip_do_fragment(net, sk, skb, output);
710 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
712 if (skb->nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
713 return PPPOE_SES_HLEN;
717 static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
719 struct nf_bridge_info *nf_bridge;
720 unsigned int mtu_reserved;
722 mtu_reserved = nf_bridge_mtu_reduction(skb);
724 if (skb_is_gso(skb) || skb->len + mtu_reserved <= skb->dev->mtu) {
725 nf_bridge_info_free(skb);
726 return br_dev_queue_push_xmit(net, sk, skb);
729 nf_bridge = nf_bridge_info_get(skb);
731 /* This is wrong! We should preserve the original fragment
732 * boundaries by preserving frag_list rather than refragmenting.
734 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) &&
735 skb->protocol == htons(ETH_P_IP)) {
736 struct brnf_frag_data *data;
738 if (br_validate_ipv4(net, skb))
741 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
743 nf_bridge_update_protocol(skb);
745 data = this_cpu_ptr(&brnf_frag_data_storage);
747 data->vlan_tci = skb->vlan_tci;
748 data->vlan_proto = skb->vlan_proto;
749 data->encap_size = nf_bridge_encap_header_len(skb);
750 data->size = ETH_HLEN + data->encap_size;
752 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
755 return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit);
757 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) &&
758 skb->protocol == htons(ETH_P_IPV6)) {
759 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
760 struct brnf_frag_data *data;
762 if (br_validate_ipv6(net, skb))
765 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
767 nf_bridge_update_protocol(skb);
769 data = this_cpu_ptr(&brnf_frag_data_storage);
770 data->encap_size = nf_bridge_encap_header_len(skb);
771 data->size = ETH_HLEN + data->encap_size;
773 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
777 return v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit);
782 nf_bridge_info_free(skb);
783 return br_dev_queue_push_xmit(net, sk, skb);
789 /* PF_BRIDGE/POST_ROUTING ********************************************/
790 static unsigned int br_nf_post_routing(void *priv,
792 const struct nf_hook_state *state)
794 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
795 struct net_device *realoutdev = bridge_parent(skb->dev);
798 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
799 * on a bridge, but was delivered locally and is now being routed:
801 * POST_ROUTING was already invoked from the ip stack.
803 if (!nf_bridge || !nf_bridge->physoutdev)
809 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
811 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
816 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
817 * about the value of skb->pkt_type. */
818 if (skb->pkt_type == PACKET_OTHERHOST) {
819 skb->pkt_type = PACKET_HOST;
820 nf_bridge->pkt_otherhost = true;
823 nf_bridge_pull_encap_header(skb);
824 if (pf == NFPROTO_IPV4)
825 skb->protocol = htons(ETH_P_IP);
827 skb->protocol = htons(ETH_P_IPV6);
829 NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb,
831 br_nf_dev_queue_xmit);
836 /* IP/SABOTAGE *****************************************************/
837 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
838 * for the second time. */
839 static unsigned int ip_sabotage_in(void *priv,
841 const struct nf_hook_state *state)
843 if (skb->nf_bridge && !skb->nf_bridge->in_prerouting)
849 /* This is called when br_netfilter has called into iptables/netfilter,
850 * and DNAT has taken place on a bridge-forwarded packet.
852 * neigh->output has created a new MAC header, with local br0 MAC
855 * This restores the original MAC saddr of the bridged packet
856 * before invoking bridge forward logic to transmit the packet.
858 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
860 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
862 skb_pull(skb, ETH_HLEN);
863 nf_bridge->bridged_dnat = 0;
865 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
867 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
868 nf_bridge->neigh_header,
869 ETH_HLEN - ETH_ALEN);
870 skb->dev = nf_bridge->physindev;
872 nf_bridge->physoutdev = NULL;
873 br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
876 static int br_nf_dev_xmit(struct sk_buff *skb)
878 if (skb->nf_bridge && skb->nf_bridge->bridged_dnat) {
879 br_nf_pre_routing_finish_bridge_slow(skb);
885 static const struct nf_br_ops br_ops = {
886 .br_dev_xmit_hook = br_nf_dev_xmit,
889 void br_netfilter_enable(void)
892 EXPORT_SYMBOL_GPL(br_netfilter_enable);
894 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
895 * br_dev_queue_push_xmit is called afterwards */
896 static struct nf_hook_ops br_nf_ops[] __read_mostly = {
898 .hook = br_nf_pre_routing,
899 .pf = NFPROTO_BRIDGE,
900 .hooknum = NF_BR_PRE_ROUTING,
901 .priority = NF_BR_PRI_BRNF,
904 .hook = br_nf_local_in,
905 .pf = NFPROTO_BRIDGE,
906 .hooknum = NF_BR_LOCAL_IN,
907 .priority = NF_BR_PRI_BRNF,
910 .hook = br_nf_forward_ip,
911 .pf = NFPROTO_BRIDGE,
912 .hooknum = NF_BR_FORWARD,
913 .priority = NF_BR_PRI_BRNF - 1,
916 .hook = br_nf_forward_arp,
917 .pf = NFPROTO_BRIDGE,
918 .hooknum = NF_BR_FORWARD,
919 .priority = NF_BR_PRI_BRNF,
922 .hook = br_nf_post_routing,
923 .pf = NFPROTO_BRIDGE,
924 .hooknum = NF_BR_POST_ROUTING,
925 .priority = NF_BR_PRI_LAST,
928 .hook = ip_sabotage_in,
930 .hooknum = NF_INET_PRE_ROUTING,
931 .priority = NF_IP_PRI_FIRST,
934 .hook = ip_sabotage_in,
936 .hooknum = NF_INET_PRE_ROUTING,
937 .priority = NF_IP6_PRI_FIRST,
943 int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
944 void __user *buffer, size_t *lenp, loff_t *ppos)
948 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
950 if (write && *(int *)(ctl->data))
951 *(int *)(ctl->data) = 1;
955 static struct ctl_table brnf_table[] = {
957 .procname = "bridge-nf-call-arptables",
958 .data = &brnf_call_arptables,
959 .maxlen = sizeof(int),
961 .proc_handler = brnf_sysctl_call_tables,
964 .procname = "bridge-nf-call-iptables",
965 .data = &brnf_call_iptables,
966 .maxlen = sizeof(int),
968 .proc_handler = brnf_sysctl_call_tables,
971 .procname = "bridge-nf-call-ip6tables",
972 .data = &brnf_call_ip6tables,
973 .maxlen = sizeof(int),
975 .proc_handler = brnf_sysctl_call_tables,
978 .procname = "bridge-nf-filter-vlan-tagged",
979 .data = &brnf_filter_vlan_tagged,
980 .maxlen = sizeof(int),
982 .proc_handler = brnf_sysctl_call_tables,
985 .procname = "bridge-nf-filter-pppoe-tagged",
986 .data = &brnf_filter_pppoe_tagged,
987 .maxlen = sizeof(int),
989 .proc_handler = brnf_sysctl_call_tables,
992 .procname = "bridge-nf-pass-vlan-input-dev",
993 .data = &brnf_pass_vlan_indev,
994 .maxlen = sizeof(int),
996 .proc_handler = brnf_sysctl_call_tables,
1002 static int __init br_netfilter_init(void)
1006 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1010 #ifdef CONFIG_SYSCTL
1011 brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
1012 if (brnf_sysctl_header == NULL) {
1014 "br_netfilter: can't register to sysctl.\n");
1015 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1019 RCU_INIT_POINTER(nf_br_ops, &br_ops);
1020 printk(KERN_NOTICE "Bridge firewalling registered\n");
1024 static void __exit br_netfilter_fini(void)
1026 RCU_INIT_POINTER(nf_br_ops, NULL);
1027 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1028 #ifdef CONFIG_SYSCTL
1029 unregister_net_sysctl_table(brnf_sysctl_header);
1033 module_init(br_netfilter_init);
1034 module_exit(br_netfilter_fini);
1036 MODULE_LICENSE("GPL");
1037 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1038 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1039 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");