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 <uapi/linux/netfilter_bridge.h>
30 #include <linux/netfilter_ipv4.h>
31 #include <linux/netfilter_ipv6.h>
32 #include <linux/netfilter_arp.h>
33 #include <linux/in_route.h>
34 #include <linux/rculist.h>
35 #include <linux/inetdevice.h>
39 #include <net/addrconf.h>
40 #include <net/route.h>
41 #include <net/netfilter/br_netfilter.h>
42 #include <net/netns/generic.h>
44 #include <linux/uaccess.h>
45 #include "br_private.h"
47 #include <linux/sysctl.h>
50 static unsigned int brnf_net_id __read_mostly;
57 static struct ctl_table_header *brnf_sysctl_header;
58 static int brnf_call_iptables __read_mostly = 1;
59 static int brnf_call_ip6tables __read_mostly = 1;
60 static int brnf_call_arptables __read_mostly = 1;
61 static int brnf_filter_vlan_tagged __read_mostly;
62 static int brnf_filter_pppoe_tagged __read_mostly;
63 static int brnf_pass_vlan_indev __read_mostly;
65 #define brnf_call_iptables 1
66 #define brnf_call_ip6tables 1
67 #define brnf_call_arptables 1
68 #define brnf_filter_vlan_tagged 0
69 #define brnf_filter_pppoe_tagged 0
70 #define brnf_pass_vlan_indev 0
74 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
76 #define IS_IPV6(skb) \
77 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
80 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
82 static inline __be16 vlan_proto(const struct sk_buff *skb)
84 if (skb_vlan_tag_present(skb))
86 else if (skb->protocol == htons(ETH_P_8021Q))
87 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
92 #define IS_VLAN_IP(skb) \
93 (vlan_proto(skb) == htons(ETH_P_IP) && \
94 brnf_filter_vlan_tagged)
96 #define IS_VLAN_IPV6(skb) \
97 (vlan_proto(skb) == htons(ETH_P_IPV6) && \
98 brnf_filter_vlan_tagged)
100 #define IS_VLAN_ARP(skb) \
101 (vlan_proto(skb) == htons(ETH_P_ARP) && \
102 brnf_filter_vlan_tagged)
104 static inline __be16 pppoe_proto(const struct sk_buff *skb)
106 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
107 sizeof(struct pppoe_hdr)));
110 #define IS_PPPOE_IP(skb) \
111 (skb->protocol == htons(ETH_P_PPP_SES) && \
112 pppoe_proto(skb) == htons(PPP_IP) && \
113 brnf_filter_pppoe_tagged)
115 #define IS_PPPOE_IPV6(skb) \
116 (skb->protocol == htons(ETH_P_PPP_SES) && \
117 pppoe_proto(skb) == htons(PPP_IPV6) && \
118 brnf_filter_pppoe_tagged)
120 /* largest possible L2 header, see br_nf_dev_queue_xmit() */
121 #define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN)
123 struct brnf_frag_data {
124 char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH];
131 static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage);
133 static void nf_bridge_info_free(struct sk_buff *skb)
135 if (skb->nf_bridge) {
136 nf_bridge_put(skb->nf_bridge);
137 skb->nf_bridge = NULL;
141 static inline struct net_device *bridge_parent(const struct net_device *dev)
143 struct net_bridge_port *port;
145 port = br_port_get_rcu(dev);
146 return port ? port->br->dev : NULL;
149 static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
151 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
153 if (refcount_read(&nf_bridge->use) > 1) {
154 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
157 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
158 refcount_set(&tmp->use, 1);
160 nf_bridge_put(nf_bridge);
166 unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
168 switch (skb->protocol) {
169 case __cpu_to_be16(ETH_P_8021Q):
171 case __cpu_to_be16(ETH_P_PPP_SES):
172 return PPPOE_SES_HLEN;
178 static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
180 unsigned int len = nf_bridge_encap_header_len(skb);
183 skb->network_header += len;
186 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
188 unsigned int len = nf_bridge_encap_header_len(skb);
190 skb_pull_rcsum(skb, len);
191 skb->network_header += len;
194 /* When handing a packet over to the IP layer
195 * check whether we have a skb that is in the
199 static int br_validate_ipv4(struct net *net, struct sk_buff *skb)
201 const struct iphdr *iph;
204 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
209 /* Basic sanity checks */
210 if (iph->ihl < 5 || iph->version != 4)
213 if (!pskb_may_pull(skb, iph->ihl*4))
217 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
220 len = ntohs(iph->tot_len);
221 if (skb->len < len) {
222 __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS);
224 } else if (len < (iph->ihl*4))
227 if (pskb_trim_rcsum(skb, len)) {
228 __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
232 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
233 /* We should really parse IP options here but until
234 * somebody who actually uses IP options complains to
235 * us we'll just silently ignore the options because
241 __IP_INC_STATS(net, IPSTATS_MIB_CSUMERRORS);
243 __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
248 void nf_bridge_update_protocol(struct sk_buff *skb)
250 switch (skb->nf_bridge->orig_proto) {
251 case BRNF_PROTO_8021Q:
252 skb->protocol = htons(ETH_P_8021Q);
254 case BRNF_PROTO_PPPOE:
255 skb->protocol = htons(ETH_P_PPP_SES);
257 case BRNF_PROTO_UNCHANGED:
262 /* Obtain the correct destination MAC address, while preserving the original
263 * source MAC address. If we already know this address, we just copy it. If we
264 * don't, we use the neighbour framework to find out. In both cases, we make
265 * sure that br_handle_frame_finish() is called afterwards.
267 int br_nf_pre_routing_finish_bridge(struct net *net, struct sock *sk, struct sk_buff *skb)
269 struct neighbour *neigh;
270 struct dst_entry *dst;
272 skb->dev = bridge_parent(skb->dev);
276 neigh = dst_neigh_lookup_skb(dst, skb);
278 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
281 if (neigh->hh.hh_len) {
282 neigh_hh_bridge(&neigh->hh, skb);
283 skb->dev = nf_bridge->physindev;
284 ret = br_handle_frame_finish(net, sk, skb);
286 /* the neighbour function below overwrites the complete
287 * MAC header, so we save the Ethernet source address and
290 skb_copy_from_linear_data_offset(skb,
291 -(ETH_HLEN-ETH_ALEN),
292 nf_bridge->neigh_header,
294 /* tell br_dev_xmit to continue with forwarding */
295 nf_bridge->bridged_dnat = 1;
296 /* FIXME Need to refragment */
297 ret = neigh->output(neigh, skb);
299 neigh_release(neigh);
308 br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb,
309 const struct nf_bridge_info *nf_bridge)
311 return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
314 /* This requires some explaining. If DNAT has taken place,
315 * we will need to fix up the destination Ethernet address.
316 * This is also true when SNAT takes place (for the reply direction).
318 * There are two cases to consider:
319 * 1. The packet was DNAT'ed to a device in the same bridge
320 * port group as it was received on. We can still bridge
322 * 2. The packet was DNAT'ed to a different device, either
323 * a non-bridged device or another bridge port group.
324 * The packet will need to be routed.
326 * The correct way of distinguishing between these two cases is to
327 * call ip_route_input() and to look at skb->dst->dev, which is
328 * changed to the destination device if ip_route_input() succeeds.
330 * Let's first consider the case that ip_route_input() succeeds:
332 * If the output device equals the logical bridge device the packet
333 * came in on, we can consider this bridging. The corresponding MAC
334 * address will be obtained in br_nf_pre_routing_finish_bridge.
335 * Otherwise, the packet is considered to be routed and we just
336 * change the destination MAC address so that the packet will
337 * later be passed up to the IP stack to be routed. For a redirected
338 * packet, ip_route_input() will give back the localhost as output device,
339 * which differs from the bridge device.
341 * Let's now consider the case that ip_route_input() fails:
343 * This can be because the destination address is martian, in which case
344 * the packet will be dropped.
345 * If IP forwarding is disabled, ip_route_input() will fail, while
346 * ip_route_output_key() can return success. The source
347 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
348 * thinks we're handling a locally generated packet and won't care
349 * if IP forwarding is enabled. If the output device equals the logical bridge
350 * device, we proceed as if ip_route_input() succeeded. If it differs from the
351 * logical bridge port or if ip_route_output_key() fails we drop the packet.
353 static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
355 struct net_device *dev = skb->dev;
356 struct iphdr *iph = ip_hdr(skb);
357 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
361 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
363 if (nf_bridge->pkt_otherhost) {
364 skb->pkt_type = PACKET_OTHERHOST;
365 nf_bridge->pkt_otherhost = false;
367 nf_bridge->in_prerouting = 0;
368 if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) {
369 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
370 struct in_device *in_dev = __in_dev_get_rcu(dev);
372 /* If err equals -EHOSTUNREACH the error is due to a
373 * martian destination or due to the fact that
374 * forwarding is disabled. For most martian packets,
375 * ip_route_output_key() will fail. It won't fail for 2 types of
376 * martian destinations: loopback destinations and destination
377 * 0.0.0.0. In both cases the packet will be dropped because the
378 * destination is the loopback device and not the bridge. */
379 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
382 rt = ip_route_output(net, iph->daddr, 0,
383 RT_TOS(iph->tos), 0);
385 /* - Bridged-and-DNAT'ed traffic doesn't
386 * require ip_forwarding. */
387 if (rt->dst.dev == dev) {
388 skb_dst_set(skb, &rt->dst);
397 if (skb_dst(skb)->dev == dev) {
399 skb->dev = nf_bridge->physindev;
400 nf_bridge_update_protocol(skb);
401 nf_bridge_push_encap_header(skb);
402 br_nf_hook_thresh(NF_BR_PRE_ROUTING,
403 net, sk, skb, skb->dev,
405 br_nf_pre_routing_finish_bridge);
408 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
409 skb->pkt_type = PACKET_HOST;
412 rt = bridge_parent_rtable(nf_bridge->physindev);
417 skb_dst_set_noref(skb, &rt->dst);
420 skb->dev = nf_bridge->physindev;
421 nf_bridge_update_protocol(skb);
422 nf_bridge_push_encap_header(skb);
423 br_nf_hook_thresh(NF_BR_PRE_ROUTING, net, sk, skb, skb->dev, NULL,
424 br_handle_frame_finish);
428 static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
430 struct net_device *vlan, *br;
432 br = bridge_parent(dev);
433 if (brnf_pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
436 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
437 skb_vlan_tag_get(skb) & VLAN_VID_MASK);
439 return vlan ? vlan : br;
442 /* Some common code for IPv4/IPv6 */
443 struct net_device *setup_pre_routing(struct sk_buff *skb)
445 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
447 if (skb->pkt_type == PACKET_OTHERHOST) {
448 skb->pkt_type = PACKET_HOST;
449 nf_bridge->pkt_otherhost = true;
452 nf_bridge->in_prerouting = 1;
453 nf_bridge->physindev = skb->dev;
454 skb->dev = brnf_get_logical_dev(skb, skb->dev);
456 if (skb->protocol == htons(ETH_P_8021Q))
457 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
458 else if (skb->protocol == htons(ETH_P_PPP_SES))
459 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
461 /* Must drop socket now because of tproxy. */
466 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
467 * Replicate the checks that IPv4 does on packet reception.
468 * Set skb->dev to the bridge device (i.e. parent of the
469 * receiving device) to make netfilter happy, the REDIRECT
470 * target in particular. Save the original destination IP
471 * address to be able to detect DNAT afterwards. */
472 static unsigned int br_nf_pre_routing(void *priv,
474 const struct nf_hook_state *state)
476 struct nf_bridge_info *nf_bridge;
477 struct net_bridge_port *p;
478 struct net_bridge *br;
479 __u32 len = nf_bridge_encap_header_len(skb);
481 if (unlikely(!pskb_may_pull(skb, len)))
484 p = br_port_get_rcu(state->in);
489 if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
490 if (!brnf_call_ip6tables &&
491 !br_opt_get(br, BROPT_NF_CALL_IP6TABLES))
494 nf_bridge_pull_encap_header_rcsum(skb);
495 return br_nf_pre_routing_ipv6(priv, skb, state);
498 if (!brnf_call_iptables && !br_opt_get(br, BROPT_NF_CALL_IPTABLES))
501 if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
504 nf_bridge_pull_encap_header_rcsum(skb);
506 if (br_validate_ipv4(state->net, skb))
509 nf_bridge_put(skb->nf_bridge);
510 if (!nf_bridge_alloc(skb))
512 if (!setup_pre_routing(skb))
515 nf_bridge = nf_bridge_info_get(skb);
516 nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
518 skb->protocol = htons(ETH_P_IP);
520 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->net, state->sk, skb,
522 br_nf_pre_routing_finish);
528 /* PF_BRIDGE/FORWARD *************************************************/
529 static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
531 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
532 struct net_device *in;
534 if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
536 if (skb->protocol == htons(ETH_P_IP))
537 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
539 if (skb->protocol == htons(ETH_P_IPV6))
540 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
542 in = nf_bridge->physindev;
543 if (nf_bridge->pkt_otherhost) {
544 skb->pkt_type = PACKET_OTHERHOST;
545 nf_bridge->pkt_otherhost = false;
547 nf_bridge_update_protocol(skb);
549 in = *((struct net_device **)(skb->cb));
551 nf_bridge_push_encap_header(skb);
553 br_nf_hook_thresh(NF_BR_FORWARD, net, sk, skb, in, skb->dev,
559 /* This is the 'purely bridged' case. For IP, we pass the packet to
560 * netfilter with indev and outdev set to the bridge device,
561 * but we are still able to filter on the 'real' indev/outdev
562 * because of the physdev module. For ARP, indev and outdev are the
564 static unsigned int br_nf_forward_ip(void *priv,
566 const struct nf_hook_state *state)
568 struct nf_bridge_info *nf_bridge;
569 struct net_device *parent;
575 /* Need exclusive nf_bridge_info since we might have multiple
576 * different physoutdevs. */
577 if (!nf_bridge_unshare(skb))
580 nf_bridge = nf_bridge_info_get(skb);
584 parent = bridge_parent(state->out);
588 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
590 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
595 nf_bridge_pull_encap_header(skb);
597 if (skb->pkt_type == PACKET_OTHERHOST) {
598 skb->pkt_type = PACKET_HOST;
599 nf_bridge->pkt_otherhost = true;
602 if (pf == NFPROTO_IPV4) {
603 if (br_validate_ipv4(state->net, skb))
605 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
608 if (pf == NFPROTO_IPV6) {
609 if (br_validate_ipv6(state->net, skb))
611 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
614 nf_bridge->physoutdev = skb->dev;
615 if (pf == NFPROTO_IPV4)
616 skb->protocol = htons(ETH_P_IP);
618 skb->protocol = htons(ETH_P_IPV6);
620 NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb,
621 brnf_get_logical_dev(skb, state->in),
622 parent, br_nf_forward_finish);
627 static unsigned int br_nf_forward_arp(void *priv,
629 const struct nf_hook_state *state)
631 struct net_bridge_port *p;
632 struct net_bridge *br;
633 struct net_device **d = (struct net_device **)(skb->cb);
635 p = br_port_get_rcu(state->out);
640 if (!brnf_call_arptables && !br_opt_get(br, BROPT_NF_CALL_ARPTABLES))
644 if (!IS_VLAN_ARP(skb))
646 nf_bridge_pull_encap_header(skb);
649 if (arp_hdr(skb)->ar_pln != 4) {
650 if (IS_VLAN_ARP(skb))
651 nf_bridge_push_encap_header(skb);
655 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb,
656 state->in, state->out, br_nf_forward_finish);
661 static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
663 struct brnf_frag_data *data;
666 data = this_cpu_ptr(&brnf_frag_data_storage);
667 err = skb_cow_head(skb, data->size);
674 if (data->vlan_tci) {
675 skb->vlan_tci = data->vlan_tci;
676 skb->vlan_proto = data->vlan_proto;
679 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
680 __skb_push(skb, data->encap_size);
682 nf_bridge_info_free(skb);
683 return br_dev_queue_push_xmit(net, sk, skb);
687 br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
688 int (*output)(struct net *, struct sock *, struct sk_buff *))
690 unsigned int mtu = ip_skb_dst_mtu(sk, skb);
691 struct iphdr *iph = ip_hdr(skb);
693 if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
694 (IPCB(skb)->frag_max_size &&
695 IPCB(skb)->frag_max_size > mtu))) {
696 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
701 return ip_do_fragment(net, sk, skb, output);
704 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
706 if (skb->nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
707 return PPPOE_SES_HLEN;
711 static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
713 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
714 unsigned int mtu, mtu_reserved;
716 mtu_reserved = nf_bridge_mtu_reduction(skb);
719 if (nf_bridge->frag_max_size && nf_bridge->frag_max_size < mtu)
720 mtu = nf_bridge->frag_max_size;
722 if (skb_is_gso(skb) || skb->len + mtu_reserved <= mtu) {
723 nf_bridge_info_free(skb);
724 return br_dev_queue_push_xmit(net, sk, skb);
727 /* This is wrong! We should preserve the original fragment
728 * boundaries by preserving frag_list rather than refragmenting.
730 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) &&
731 skb->protocol == htons(ETH_P_IP)) {
732 struct brnf_frag_data *data;
734 if (br_validate_ipv4(net, skb))
737 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
739 nf_bridge_update_protocol(skb);
741 data = this_cpu_ptr(&brnf_frag_data_storage);
743 data->vlan_tci = skb->vlan_tci;
744 data->vlan_proto = skb->vlan_proto;
745 data->encap_size = nf_bridge_encap_header_len(skb);
746 data->size = ETH_HLEN + data->encap_size;
748 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
751 return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit);
753 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) &&
754 skb->protocol == htons(ETH_P_IPV6)) {
755 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
756 struct brnf_frag_data *data;
758 if (br_validate_ipv6(net, skb))
761 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
763 nf_bridge_update_protocol(skb);
765 data = this_cpu_ptr(&brnf_frag_data_storage);
766 data->encap_size = nf_bridge_encap_header_len(skb);
767 data->size = ETH_HLEN + data->encap_size;
769 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
773 return v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit);
778 nf_bridge_info_free(skb);
779 return br_dev_queue_push_xmit(net, sk, skb);
785 /* PF_BRIDGE/POST_ROUTING ********************************************/
786 static unsigned int br_nf_post_routing(void *priv,
788 const struct nf_hook_state *state)
790 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
791 struct net_device *realoutdev = bridge_parent(skb->dev);
794 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
795 * on a bridge, but was delivered locally and is now being routed:
797 * POST_ROUTING was already invoked from the ip stack.
799 if (!nf_bridge || !nf_bridge->physoutdev)
805 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
807 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
812 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
813 * about the value of skb->pkt_type. */
814 if (skb->pkt_type == PACKET_OTHERHOST) {
815 skb->pkt_type = PACKET_HOST;
816 nf_bridge->pkt_otherhost = true;
819 nf_bridge_pull_encap_header(skb);
820 if (pf == NFPROTO_IPV4)
821 skb->protocol = htons(ETH_P_IP);
823 skb->protocol = htons(ETH_P_IPV6);
825 NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb,
827 br_nf_dev_queue_xmit);
832 /* IP/SABOTAGE *****************************************************/
833 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
834 * for the second time. */
835 static unsigned int ip_sabotage_in(void *priv,
837 const struct nf_hook_state *state)
839 if (skb->nf_bridge && !skb->nf_bridge->in_prerouting &&
840 !netif_is_l3_master(skb->dev)) {
841 state->okfn(state->net, state->sk, skb);
848 /* This is called when br_netfilter has called into iptables/netfilter,
849 * and DNAT has taken place on a bridge-forwarded packet.
851 * neigh->output has created a new MAC header, with local br0 MAC
854 * This restores the original MAC saddr of the bridged packet
855 * before invoking bridge forward logic to transmit the packet.
857 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
859 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
861 skb_pull(skb, ETH_HLEN);
862 nf_bridge->bridged_dnat = 0;
864 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
866 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
867 nf_bridge->neigh_header,
868 ETH_HLEN - ETH_ALEN);
869 skb->dev = nf_bridge->physindev;
871 nf_bridge->physoutdev = NULL;
872 br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
875 static int br_nf_dev_xmit(struct sk_buff *skb)
877 if (skb->nf_bridge && skb->nf_bridge->bridged_dnat) {
878 br_nf_pre_routing_finish_bridge_slow(skb);
884 static const struct nf_br_ops br_ops = {
885 .br_dev_xmit_hook = br_nf_dev_xmit,
888 void br_netfilter_enable(void)
891 EXPORT_SYMBOL_GPL(br_netfilter_enable);
893 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
894 * br_dev_queue_push_xmit is called afterwards */
895 static const struct nf_hook_ops br_nf_ops[] = {
897 .hook = br_nf_pre_routing,
898 .pf = NFPROTO_BRIDGE,
899 .hooknum = NF_BR_PRE_ROUTING,
900 .priority = NF_BR_PRI_BRNF,
903 .hook = br_nf_forward_ip,
904 .pf = NFPROTO_BRIDGE,
905 .hooknum = NF_BR_FORWARD,
906 .priority = NF_BR_PRI_BRNF - 1,
909 .hook = br_nf_forward_arp,
910 .pf = NFPROTO_BRIDGE,
911 .hooknum = NF_BR_FORWARD,
912 .priority = NF_BR_PRI_BRNF,
915 .hook = br_nf_post_routing,
916 .pf = NFPROTO_BRIDGE,
917 .hooknum = NF_BR_POST_ROUTING,
918 .priority = NF_BR_PRI_LAST,
921 .hook = ip_sabotage_in,
923 .hooknum = NF_INET_PRE_ROUTING,
924 .priority = NF_IP_PRI_FIRST,
927 .hook = ip_sabotage_in,
929 .hooknum = NF_INET_PRE_ROUTING,
930 .priority = NF_IP6_PRI_FIRST,
934 static int brnf_device_event(struct notifier_block *unused, unsigned long event,
937 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
938 struct brnf_net *brnet;
942 if (event != NETDEV_REGISTER || !(dev->priv_flags & IFF_EBRIDGE))
948 brnet = net_generic(net, brnf_net_id);
952 ret = nf_register_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
956 brnet->enabled = true;
960 static void __net_exit brnf_exit_net(struct net *net)
962 struct brnf_net *brnet = net_generic(net, brnf_net_id);
967 nf_unregister_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
968 brnet->enabled = false;
971 static struct pernet_operations brnf_net_ops __read_mostly = {
972 .exit = brnf_exit_net,
974 .size = sizeof(struct brnf_net),
977 static struct notifier_block brnf_notifier __read_mostly = {
978 .notifier_call = brnf_device_event,
981 /* recursively invokes nf_hook_slow (again), skipping already-called
982 * hooks (< NF_BR_PRI_BRNF).
984 * Called with rcu read lock held.
986 int br_nf_hook_thresh(unsigned int hook, struct net *net,
987 struct sock *sk, struct sk_buff *skb,
988 struct net_device *indev,
989 struct net_device *outdev,
990 int (*okfn)(struct net *, struct sock *,
993 const struct nf_hook_entries *e;
994 struct nf_hook_state state;
995 struct nf_hook_ops **ops;
999 e = rcu_dereference(net->nf.hooks_bridge[hook]);
1001 return okfn(net, sk, skb);
1003 ops = nf_hook_entries_get_hook_ops(e);
1004 for (i = 0; i < e->num_hook_entries &&
1005 ops[i]->priority <= NF_BR_PRI_BRNF; i++)
1008 nf_hook_state_init(&state, hook, NFPROTO_BRIDGE, indev, outdev,
1011 ret = nf_hook_slow(skb, &state, e, i);
1013 ret = okfn(net, sk, skb);
1018 #ifdef CONFIG_SYSCTL
1020 int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
1021 void __user *buffer, size_t *lenp, loff_t *ppos)
1025 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1027 if (write && *(int *)(ctl->data))
1028 *(int *)(ctl->data) = 1;
1032 static struct ctl_table brnf_table[] = {
1034 .procname = "bridge-nf-call-arptables",
1035 .data = &brnf_call_arptables,
1036 .maxlen = sizeof(int),
1038 .proc_handler = brnf_sysctl_call_tables,
1041 .procname = "bridge-nf-call-iptables",
1042 .data = &brnf_call_iptables,
1043 .maxlen = sizeof(int),
1045 .proc_handler = brnf_sysctl_call_tables,
1048 .procname = "bridge-nf-call-ip6tables",
1049 .data = &brnf_call_ip6tables,
1050 .maxlen = sizeof(int),
1052 .proc_handler = brnf_sysctl_call_tables,
1055 .procname = "bridge-nf-filter-vlan-tagged",
1056 .data = &brnf_filter_vlan_tagged,
1057 .maxlen = sizeof(int),
1059 .proc_handler = brnf_sysctl_call_tables,
1062 .procname = "bridge-nf-filter-pppoe-tagged",
1063 .data = &brnf_filter_pppoe_tagged,
1064 .maxlen = sizeof(int),
1066 .proc_handler = brnf_sysctl_call_tables,
1069 .procname = "bridge-nf-pass-vlan-input-dev",
1070 .data = &brnf_pass_vlan_indev,
1071 .maxlen = sizeof(int),
1073 .proc_handler = brnf_sysctl_call_tables,
1079 static int __init br_netfilter_init(void)
1083 ret = register_pernet_subsys(&brnf_net_ops);
1087 ret = register_netdevice_notifier(&brnf_notifier);
1089 unregister_pernet_subsys(&brnf_net_ops);
1093 #ifdef CONFIG_SYSCTL
1094 brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
1095 if (brnf_sysctl_header == NULL) {
1097 "br_netfilter: can't register to sysctl.\n");
1098 unregister_netdevice_notifier(&brnf_notifier);
1099 unregister_pernet_subsys(&brnf_net_ops);
1103 RCU_INIT_POINTER(nf_br_ops, &br_ops);
1104 printk(KERN_NOTICE "Bridge firewalling registered\n");
1108 static void __exit br_netfilter_fini(void)
1110 RCU_INIT_POINTER(nf_br_ops, NULL);
1111 unregister_netdevice_notifier(&brnf_notifier);
1112 unregister_pernet_subsys(&brnf_net_ops);
1113 #ifdef CONFIG_SYSCTL
1114 unregister_net_sysctl_table(brnf_sysctl_header);
1118 module_init(br_netfilter_init);
1119 module_exit(br_netfilter_fini);
1121 MODULE_LICENSE("GPL");
1122 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1123 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1124 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");