1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Linux ethernet bridge
7 * Lennert Buytenhek <buytenh@gnu.org>
8 * Bart De Schuymer <bdschuym@pandora.be>
10 * Lennert dedicates this file to Kerstin Wurdinger.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
17 #include <linux/netdevice.h>
18 #include <linux/skbuff.h>
19 #include <linux/if_arp.h>
20 #include <linux/if_ether.h>
21 #include <linux/if_vlan.h>
22 #include <linux/if_pppox.h>
23 #include <linux/ppp_defs.h>
24 #include <linux/netfilter_bridge.h>
25 #include <uapi/linux/netfilter_bridge.h>
26 #include <linux/netfilter_ipv4.h>
27 #include <linux/netfilter_ipv6.h>
28 #include <linux/netfilter_arp.h>
29 #include <linux/in_route.h>
30 #include <linux/rculist.h>
31 #include <linux/inetdevice.h>
35 #include <net/addrconf.h>
36 #include <net/route.h>
37 #include <net/netfilter/br_netfilter.h>
38 #include <net/netns/generic.h>
40 #include <linux/uaccess.h>
41 #include "br_private.h"
43 #include <linux/sysctl.h>
46 static unsigned int brnf_net_id __read_mostly;
53 static struct ctl_table_header *brnf_sysctl_header;
54 static int brnf_call_iptables __read_mostly = 1;
55 static int brnf_call_ip6tables __read_mostly = 1;
56 static int brnf_call_arptables __read_mostly = 1;
57 static int brnf_filter_vlan_tagged __read_mostly;
58 static int brnf_filter_pppoe_tagged __read_mostly;
59 static int brnf_pass_vlan_indev __read_mostly;
61 #define brnf_call_iptables 1
62 #define brnf_call_ip6tables 1
63 #define brnf_call_arptables 1
64 #define brnf_filter_vlan_tagged 0
65 #define brnf_filter_pppoe_tagged 0
66 #define brnf_pass_vlan_indev 0
70 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
72 #define IS_IPV6(skb) \
73 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
76 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
78 static inline __be16 vlan_proto(const struct sk_buff *skb)
80 if (skb_vlan_tag_present(skb))
82 else if (skb->protocol == htons(ETH_P_8021Q))
83 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
88 #define IS_VLAN_IP(skb) \
89 (vlan_proto(skb) == htons(ETH_P_IP) && \
90 brnf_filter_vlan_tagged)
92 #define IS_VLAN_IPV6(skb) \
93 (vlan_proto(skb) == htons(ETH_P_IPV6) && \
94 brnf_filter_vlan_tagged)
96 #define IS_VLAN_ARP(skb) \
97 (vlan_proto(skb) == htons(ETH_P_ARP) && \
98 brnf_filter_vlan_tagged)
100 static inline __be16 pppoe_proto(const struct sk_buff *skb)
102 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
103 sizeof(struct pppoe_hdr)));
106 #define IS_PPPOE_IP(skb) \
107 (skb->protocol == htons(ETH_P_PPP_SES) && \
108 pppoe_proto(skb) == htons(PPP_IP) && \
109 brnf_filter_pppoe_tagged)
111 #define IS_PPPOE_IPV6(skb) \
112 (skb->protocol == htons(ETH_P_PPP_SES) && \
113 pppoe_proto(skb) == htons(PPP_IPV6) && \
114 brnf_filter_pppoe_tagged)
116 /* largest possible L2 header, see br_nf_dev_queue_xmit() */
117 #define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN)
119 struct brnf_frag_data {
120 char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH];
127 static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage);
129 static void nf_bridge_info_free(struct sk_buff *skb)
131 skb_ext_del(skb, SKB_EXT_BRIDGE_NF);
134 static inline struct net_device *bridge_parent(const struct net_device *dev)
136 struct net_bridge_port *port;
138 port = br_port_get_rcu(dev);
139 return port ? port->br->dev : NULL;
142 static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
144 return skb_ext_add(skb, SKB_EXT_BRIDGE_NF);
147 unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
149 switch (skb->protocol) {
150 case __cpu_to_be16(ETH_P_8021Q):
152 case __cpu_to_be16(ETH_P_PPP_SES):
153 return PPPOE_SES_HLEN;
159 static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
161 unsigned int len = nf_bridge_encap_header_len(skb);
164 skb->network_header += len;
167 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
169 unsigned int len = nf_bridge_encap_header_len(skb);
171 skb_pull_rcsum(skb, len);
172 skb->network_header += len;
175 /* When handing a packet over to the IP layer
176 * check whether we have a skb that is in the
180 static int br_validate_ipv4(struct net *net, struct sk_buff *skb)
182 const struct iphdr *iph;
185 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
190 /* Basic sanity checks */
191 if (iph->ihl < 5 || iph->version != 4)
194 if (!pskb_may_pull(skb, iph->ihl*4))
198 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
201 len = ntohs(iph->tot_len);
202 if (skb->len < len) {
203 __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS);
205 } else if (len < (iph->ihl*4))
208 if (pskb_trim_rcsum(skb, len)) {
209 __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
213 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
214 /* We should really parse IP options here but until
215 * somebody who actually uses IP options complains to
216 * us we'll just silently ignore the options because
222 __IP_INC_STATS(net, IPSTATS_MIB_CSUMERRORS);
224 __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
229 void nf_bridge_update_protocol(struct sk_buff *skb)
231 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
233 switch (nf_bridge->orig_proto) {
234 case BRNF_PROTO_8021Q:
235 skb->protocol = htons(ETH_P_8021Q);
237 case BRNF_PROTO_PPPOE:
238 skb->protocol = htons(ETH_P_PPP_SES);
240 case BRNF_PROTO_UNCHANGED:
245 /* Obtain the correct destination MAC address, while preserving the original
246 * source MAC address. If we already know this address, we just copy it. If we
247 * don't, we use the neighbour framework to find out. In both cases, we make
248 * sure that br_handle_frame_finish() is called afterwards.
250 int br_nf_pre_routing_finish_bridge(struct net *net, struct sock *sk, struct sk_buff *skb)
252 struct neighbour *neigh;
253 struct dst_entry *dst;
255 skb->dev = bridge_parent(skb->dev);
259 neigh = dst_neigh_lookup_skb(dst, skb);
261 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
264 if ((neigh->nud_state & NUD_CONNECTED) && neigh->hh.hh_len) {
265 neigh_hh_bridge(&neigh->hh, skb);
266 skb->dev = nf_bridge->physindev;
267 ret = br_handle_frame_finish(net, sk, skb);
269 /* the neighbour function below overwrites the complete
270 * MAC header, so we save the Ethernet source address and
273 skb_copy_from_linear_data_offset(skb,
274 -(ETH_HLEN-ETH_ALEN),
275 nf_bridge->neigh_header,
277 /* tell br_dev_xmit to continue with forwarding */
278 nf_bridge->bridged_dnat = 1;
279 /* FIXME Need to refragment */
280 ret = neigh->output(neigh, skb);
282 neigh_release(neigh);
291 br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb,
292 const struct nf_bridge_info *nf_bridge)
294 return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
297 /* This requires some explaining. If DNAT has taken place,
298 * we will need to fix up the destination Ethernet address.
299 * This is also true when SNAT takes place (for the reply direction).
301 * There are two cases to consider:
302 * 1. The packet was DNAT'ed to a device in the same bridge
303 * port group as it was received on. We can still bridge
305 * 2. The packet was DNAT'ed to a different device, either
306 * a non-bridged device or another bridge port group.
307 * The packet will need to be routed.
309 * The correct way of distinguishing between these two cases is to
310 * call ip_route_input() and to look at skb->dst->dev, which is
311 * changed to the destination device if ip_route_input() succeeds.
313 * Let's first consider the case that ip_route_input() succeeds:
315 * If the output device equals the logical bridge device the packet
316 * came in on, we can consider this bridging. The corresponding MAC
317 * address will be obtained in br_nf_pre_routing_finish_bridge.
318 * Otherwise, the packet is considered to be routed and we just
319 * change the destination MAC address so that the packet will
320 * later be passed up to the IP stack to be routed. For a redirected
321 * packet, ip_route_input() will give back the localhost as output device,
322 * which differs from the bridge device.
324 * Let's now consider the case that ip_route_input() fails:
326 * This can be because the destination address is martian, in which case
327 * the packet will be dropped.
328 * If IP forwarding is disabled, ip_route_input() will fail, while
329 * ip_route_output_key() can return success. The source
330 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
331 * thinks we're handling a locally generated packet and won't care
332 * if IP forwarding is enabled. If the output device equals the logical bridge
333 * device, we proceed as if ip_route_input() succeeded. If it differs from the
334 * logical bridge port or if ip_route_output_key() fails we drop the packet.
336 static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
338 struct net_device *dev = skb->dev;
339 struct iphdr *iph = ip_hdr(skb);
340 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
344 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
346 if (nf_bridge->pkt_otherhost) {
347 skb->pkt_type = PACKET_OTHERHOST;
348 nf_bridge->pkt_otherhost = false;
350 nf_bridge->in_prerouting = 0;
351 if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) {
352 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
353 struct in_device *in_dev = __in_dev_get_rcu(dev);
355 /* If err equals -EHOSTUNREACH the error is due to a
356 * martian destination or due to the fact that
357 * forwarding is disabled. For most martian packets,
358 * ip_route_output_key() will fail. It won't fail for 2 types of
359 * martian destinations: loopback destinations and destination
360 * 0.0.0.0. In both cases the packet will be dropped because the
361 * destination is the loopback device and not the bridge. */
362 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
365 rt = ip_route_output(net, iph->daddr, 0,
366 RT_TOS(iph->tos), 0);
368 /* - Bridged-and-DNAT'ed traffic doesn't
369 * require ip_forwarding. */
370 if (rt->dst.dev == dev) {
371 skb_dst_set(skb, &rt->dst);
380 if (skb_dst(skb)->dev == dev) {
382 skb->dev = nf_bridge->physindev;
383 nf_bridge_update_protocol(skb);
384 nf_bridge_push_encap_header(skb);
385 br_nf_hook_thresh(NF_BR_PRE_ROUTING,
386 net, sk, skb, skb->dev,
388 br_nf_pre_routing_finish_bridge);
391 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
392 skb->pkt_type = PACKET_HOST;
395 rt = bridge_parent_rtable(nf_bridge->physindev);
400 skb_dst_set_noref(skb, &rt->dst);
403 skb->dev = nf_bridge->physindev;
404 nf_bridge_update_protocol(skb);
405 nf_bridge_push_encap_header(skb);
406 br_nf_hook_thresh(NF_BR_PRE_ROUTING, net, sk, skb, skb->dev, NULL,
407 br_handle_frame_finish);
411 static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
413 struct net_device *vlan, *br;
415 br = bridge_parent(dev);
416 if (brnf_pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
419 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
420 skb_vlan_tag_get(skb) & VLAN_VID_MASK);
422 return vlan ? vlan : br;
425 /* Some common code for IPv4/IPv6 */
426 struct net_device *setup_pre_routing(struct sk_buff *skb)
428 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
430 if (skb->pkt_type == PACKET_OTHERHOST) {
431 skb->pkt_type = PACKET_HOST;
432 nf_bridge->pkt_otherhost = true;
435 nf_bridge->in_prerouting = 1;
436 nf_bridge->physindev = skb->dev;
437 skb->dev = brnf_get_logical_dev(skb, skb->dev);
439 if (skb->protocol == htons(ETH_P_8021Q))
440 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
441 else if (skb->protocol == htons(ETH_P_PPP_SES))
442 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
444 /* Must drop socket now because of tproxy. */
449 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
450 * Replicate the checks that IPv4 does on packet reception.
451 * Set skb->dev to the bridge device (i.e. parent of the
452 * receiving device) to make netfilter happy, the REDIRECT
453 * target in particular. Save the original destination IP
454 * address to be able to detect DNAT afterwards. */
455 static unsigned int br_nf_pre_routing(void *priv,
457 const struct nf_hook_state *state)
459 struct nf_bridge_info *nf_bridge;
460 struct net_bridge_port *p;
461 struct net_bridge *br;
462 __u32 len = nf_bridge_encap_header_len(skb);
464 if (unlikely(!pskb_may_pull(skb, len)))
467 p = br_port_get_rcu(state->in);
472 if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
473 if (!brnf_call_ip6tables &&
474 !br_opt_get(br, BROPT_NF_CALL_IP6TABLES))
477 nf_bridge_pull_encap_header_rcsum(skb);
478 return br_nf_pre_routing_ipv6(priv, skb, state);
481 if (!brnf_call_iptables && !br_opt_get(br, BROPT_NF_CALL_IPTABLES))
484 if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
487 nf_bridge_pull_encap_header_rcsum(skb);
489 if (br_validate_ipv4(state->net, skb))
492 if (!nf_bridge_alloc(skb))
494 if (!setup_pre_routing(skb))
497 nf_bridge = nf_bridge_info_get(skb);
498 nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
500 skb->protocol = htons(ETH_P_IP);
501 skb->transport_header = skb->network_header + ip_hdr(skb)->ihl * 4;
503 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->net, state->sk, skb,
505 br_nf_pre_routing_finish);
511 /* PF_BRIDGE/FORWARD *************************************************/
512 static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
514 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
515 struct net_device *in;
517 if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
519 if (skb->protocol == htons(ETH_P_IP))
520 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
522 if (skb->protocol == htons(ETH_P_IPV6))
523 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
525 in = nf_bridge->physindev;
526 if (nf_bridge->pkt_otherhost) {
527 skb->pkt_type = PACKET_OTHERHOST;
528 nf_bridge->pkt_otherhost = false;
530 nf_bridge_update_protocol(skb);
532 in = *((struct net_device **)(skb->cb));
534 nf_bridge_push_encap_header(skb);
536 br_nf_hook_thresh(NF_BR_FORWARD, net, sk, skb, in, skb->dev,
542 /* This is the 'purely bridged' case. For IP, we pass the packet to
543 * netfilter with indev and outdev set to the bridge device,
544 * but we are still able to filter on the 'real' indev/outdev
545 * because of the physdev module. For ARP, indev and outdev are the
547 static unsigned int br_nf_forward_ip(void *priv,
549 const struct nf_hook_state *state)
551 struct nf_bridge_info *nf_bridge;
552 struct net_device *parent;
555 nf_bridge = nf_bridge_info_get(skb);
559 /* Need exclusive nf_bridge_info since we might have multiple
560 * different physoutdevs. */
561 if (!nf_bridge_unshare(skb))
564 nf_bridge = nf_bridge_info_get(skb);
568 parent = bridge_parent(state->out);
572 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
574 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
579 nf_bridge_pull_encap_header(skb);
581 if (skb->pkt_type == PACKET_OTHERHOST) {
582 skb->pkt_type = PACKET_HOST;
583 nf_bridge->pkt_otherhost = true;
586 if (pf == NFPROTO_IPV4) {
587 if (br_validate_ipv4(state->net, skb))
589 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
592 if (pf == NFPROTO_IPV6) {
593 if (br_validate_ipv6(state->net, skb))
595 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
598 nf_bridge->physoutdev = skb->dev;
599 if (pf == NFPROTO_IPV4)
600 skb->protocol = htons(ETH_P_IP);
602 skb->protocol = htons(ETH_P_IPV6);
604 NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb,
605 brnf_get_logical_dev(skb, state->in),
606 parent, br_nf_forward_finish);
611 static unsigned int br_nf_forward_arp(void *priv,
613 const struct nf_hook_state *state)
615 struct net_bridge_port *p;
616 struct net_bridge *br;
617 struct net_device **d = (struct net_device **)(skb->cb);
619 p = br_port_get_rcu(state->out);
624 if (!brnf_call_arptables && !br_opt_get(br, BROPT_NF_CALL_ARPTABLES))
628 if (!IS_VLAN_ARP(skb))
630 nf_bridge_pull_encap_header(skb);
633 if (arp_hdr(skb)->ar_pln != 4) {
634 if (IS_VLAN_ARP(skb))
635 nf_bridge_push_encap_header(skb);
639 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb,
640 state->in, state->out, br_nf_forward_finish);
645 static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
647 struct brnf_frag_data *data;
650 data = this_cpu_ptr(&brnf_frag_data_storage);
651 err = skb_cow_head(skb, data->size);
658 if (data->vlan_proto)
659 __vlan_hwaccel_put_tag(skb, data->vlan_proto, data->vlan_tci);
661 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
662 __skb_push(skb, data->encap_size);
664 nf_bridge_info_free(skb);
665 return br_dev_queue_push_xmit(net, sk, skb);
669 br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
670 int (*output)(struct net *, struct sock *, struct sk_buff *))
672 unsigned int mtu = ip_skb_dst_mtu(sk, skb);
673 struct iphdr *iph = ip_hdr(skb);
675 if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
676 (IPCB(skb)->frag_max_size &&
677 IPCB(skb)->frag_max_size > mtu))) {
678 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
683 return ip_do_fragment(net, sk, skb, output);
686 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
688 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
690 if (nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
691 return PPPOE_SES_HLEN;
695 static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
697 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
698 unsigned int mtu, mtu_reserved;
700 mtu_reserved = nf_bridge_mtu_reduction(skb);
703 if (nf_bridge->frag_max_size && nf_bridge->frag_max_size < mtu)
704 mtu = nf_bridge->frag_max_size;
706 if (skb_is_gso(skb) || skb->len + mtu_reserved <= mtu) {
707 nf_bridge_info_free(skb);
708 return br_dev_queue_push_xmit(net, sk, skb);
711 /* This is wrong! We should preserve the original fragment
712 * boundaries by preserving frag_list rather than refragmenting.
714 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) &&
715 skb->protocol == htons(ETH_P_IP)) {
716 struct brnf_frag_data *data;
718 if (br_validate_ipv4(net, skb))
721 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
723 nf_bridge_update_protocol(skb);
725 data = this_cpu_ptr(&brnf_frag_data_storage);
727 if (skb_vlan_tag_present(skb)) {
728 data->vlan_tci = skb->vlan_tci;
729 data->vlan_proto = skb->vlan_proto;
731 data->vlan_proto = 0;
734 data->encap_size = nf_bridge_encap_header_len(skb);
735 data->size = ETH_HLEN + data->encap_size;
737 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
740 return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit);
742 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) &&
743 skb->protocol == htons(ETH_P_IPV6)) {
744 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
745 struct brnf_frag_data *data;
747 if (br_validate_ipv6(net, skb))
750 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
752 nf_bridge_update_protocol(skb);
754 data = this_cpu_ptr(&brnf_frag_data_storage);
755 data->encap_size = nf_bridge_encap_header_len(skb);
756 data->size = ETH_HLEN + data->encap_size;
758 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
762 return v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit);
767 nf_bridge_info_free(skb);
768 return br_dev_queue_push_xmit(net, sk, skb);
774 /* PF_BRIDGE/POST_ROUTING ********************************************/
775 static unsigned int br_nf_post_routing(void *priv,
777 const struct nf_hook_state *state)
779 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
780 struct net_device *realoutdev = bridge_parent(skb->dev);
783 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
784 * on a bridge, but was delivered locally and is now being routed:
786 * POST_ROUTING was already invoked from the ip stack.
788 if (!nf_bridge || !nf_bridge->physoutdev)
794 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
796 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
801 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
802 * about the value of skb->pkt_type. */
803 if (skb->pkt_type == PACKET_OTHERHOST) {
804 skb->pkt_type = PACKET_HOST;
805 nf_bridge->pkt_otherhost = true;
808 nf_bridge_pull_encap_header(skb);
809 if (pf == NFPROTO_IPV4)
810 skb->protocol = htons(ETH_P_IP);
812 skb->protocol = htons(ETH_P_IPV6);
814 NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb,
816 br_nf_dev_queue_xmit);
821 /* IP/SABOTAGE *****************************************************/
822 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
823 * for the second time. */
824 static unsigned int ip_sabotage_in(void *priv,
826 const struct nf_hook_state *state)
828 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
830 if (nf_bridge && !nf_bridge->in_prerouting &&
831 !netif_is_l3_master(skb->dev) &&
832 !netif_is_l3_slave(skb->dev)) {
833 state->okfn(state->net, state->sk, skb);
840 /* This is called when br_netfilter has called into iptables/netfilter,
841 * and DNAT has taken place on a bridge-forwarded packet.
843 * neigh->output has created a new MAC header, with local br0 MAC
846 * This restores the original MAC saddr of the bridged packet
847 * before invoking bridge forward logic to transmit the packet.
849 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
851 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
853 skb_pull(skb, ETH_HLEN);
854 nf_bridge->bridged_dnat = 0;
856 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
858 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
859 nf_bridge->neigh_header,
860 ETH_HLEN - ETH_ALEN);
861 skb->dev = nf_bridge->physindev;
863 nf_bridge->physoutdev = NULL;
864 br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
867 static int br_nf_dev_xmit(struct sk_buff *skb)
869 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
871 if (nf_bridge && nf_bridge->bridged_dnat) {
872 br_nf_pre_routing_finish_bridge_slow(skb);
878 static const struct nf_br_ops br_ops = {
879 .br_dev_xmit_hook = br_nf_dev_xmit,
882 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
883 * br_dev_queue_push_xmit is called afterwards */
884 static const struct nf_hook_ops br_nf_ops[] = {
886 .hook = br_nf_pre_routing,
887 .pf = NFPROTO_BRIDGE,
888 .hooknum = NF_BR_PRE_ROUTING,
889 .priority = NF_BR_PRI_BRNF,
892 .hook = br_nf_forward_ip,
893 .pf = NFPROTO_BRIDGE,
894 .hooknum = NF_BR_FORWARD,
895 .priority = NF_BR_PRI_BRNF - 1,
898 .hook = br_nf_forward_arp,
899 .pf = NFPROTO_BRIDGE,
900 .hooknum = NF_BR_FORWARD,
901 .priority = NF_BR_PRI_BRNF,
904 .hook = br_nf_post_routing,
905 .pf = NFPROTO_BRIDGE,
906 .hooknum = NF_BR_POST_ROUTING,
907 .priority = NF_BR_PRI_LAST,
910 .hook = ip_sabotage_in,
912 .hooknum = NF_INET_PRE_ROUTING,
913 .priority = NF_IP_PRI_FIRST,
916 .hook = ip_sabotage_in,
918 .hooknum = NF_INET_PRE_ROUTING,
919 .priority = NF_IP6_PRI_FIRST,
923 static int brnf_device_event(struct notifier_block *unused, unsigned long event,
926 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
927 struct brnf_net *brnet;
931 if (event != NETDEV_REGISTER || !(dev->priv_flags & IFF_EBRIDGE))
937 brnet = net_generic(net, brnf_net_id);
941 ret = nf_register_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
945 brnet->enabled = true;
949 static void __net_exit brnf_exit_net(struct net *net)
951 struct brnf_net *brnet = net_generic(net, brnf_net_id);
956 nf_unregister_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
957 brnet->enabled = false;
960 static struct pernet_operations brnf_net_ops __read_mostly = {
961 .exit = brnf_exit_net,
963 .size = sizeof(struct brnf_net),
966 static struct notifier_block brnf_notifier __read_mostly = {
967 .notifier_call = brnf_device_event,
970 /* recursively invokes nf_hook_slow (again), skipping already-called
971 * hooks (< NF_BR_PRI_BRNF).
973 * Called with rcu read lock held.
975 int br_nf_hook_thresh(unsigned int hook, struct net *net,
976 struct sock *sk, struct sk_buff *skb,
977 struct net_device *indev,
978 struct net_device *outdev,
979 int (*okfn)(struct net *, struct sock *,
982 const struct nf_hook_entries *e;
983 struct nf_hook_state state;
984 struct nf_hook_ops **ops;
988 e = rcu_dereference(net->nf.hooks_bridge[hook]);
990 return okfn(net, sk, skb);
992 ops = nf_hook_entries_get_hook_ops(e);
993 for (i = 0; i < e->num_hook_entries &&
994 ops[i]->priority <= NF_BR_PRI_BRNF; i++)
997 nf_hook_state_init(&state, hook, NFPROTO_BRIDGE, indev, outdev,
1000 ret = nf_hook_slow(skb, &state, e, i);
1002 ret = okfn(net, sk, skb);
1007 #ifdef CONFIG_SYSCTL
1009 int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
1010 void __user *buffer, size_t *lenp, loff_t *ppos)
1014 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1016 if (write && *(int *)(ctl->data))
1017 *(int *)(ctl->data) = 1;
1021 static struct ctl_table brnf_table[] = {
1023 .procname = "bridge-nf-call-arptables",
1024 .data = &brnf_call_arptables,
1025 .maxlen = sizeof(int),
1027 .proc_handler = brnf_sysctl_call_tables,
1030 .procname = "bridge-nf-call-iptables",
1031 .data = &brnf_call_iptables,
1032 .maxlen = sizeof(int),
1034 .proc_handler = brnf_sysctl_call_tables,
1037 .procname = "bridge-nf-call-ip6tables",
1038 .data = &brnf_call_ip6tables,
1039 .maxlen = sizeof(int),
1041 .proc_handler = brnf_sysctl_call_tables,
1044 .procname = "bridge-nf-filter-vlan-tagged",
1045 .data = &brnf_filter_vlan_tagged,
1046 .maxlen = sizeof(int),
1048 .proc_handler = brnf_sysctl_call_tables,
1051 .procname = "bridge-nf-filter-pppoe-tagged",
1052 .data = &brnf_filter_pppoe_tagged,
1053 .maxlen = sizeof(int),
1055 .proc_handler = brnf_sysctl_call_tables,
1058 .procname = "bridge-nf-pass-vlan-input-dev",
1059 .data = &brnf_pass_vlan_indev,
1060 .maxlen = sizeof(int),
1062 .proc_handler = brnf_sysctl_call_tables,
1068 static int __init br_netfilter_init(void)
1072 ret = register_pernet_subsys(&brnf_net_ops);
1076 ret = register_netdevice_notifier(&brnf_notifier);
1078 unregister_pernet_subsys(&brnf_net_ops);
1082 #ifdef CONFIG_SYSCTL
1083 brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
1084 if (brnf_sysctl_header == NULL) {
1086 "br_netfilter: can't register to sysctl.\n");
1087 unregister_netdevice_notifier(&brnf_notifier);
1088 unregister_pernet_subsys(&brnf_net_ops);
1092 RCU_INIT_POINTER(nf_br_ops, &br_ops);
1093 printk(KERN_NOTICE "Bridge firewalling registered\n");
1097 static void __exit br_netfilter_fini(void)
1099 RCU_INIT_POINTER(nf_br_ops, NULL);
1100 unregister_netdevice_notifier(&brnf_notifier);
1101 unregister_pernet_subsys(&brnf_net_ops);
1102 #ifdef CONFIG_SYSCTL
1103 unregister_net_sysctl_table(brnf_sysctl_header);
1107 module_init(br_netfilter_init);
1108 module_exit(br_netfilter_fini);
1110 MODULE_LICENSE("GPL");
1111 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1112 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1113 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");