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 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
47 #include <net/netfilter/nf_conntrack_core.h>
50 static unsigned int brnf_net_id __read_mostly;
56 struct ctl_table_header *ctl_hdr;
59 /* default value is 1 */
64 /* default value is 0 */
65 int filter_vlan_tagged;
66 int filter_pppoe_tagged;
71 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
73 #define IS_IPV6(skb) \
74 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
77 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
79 static inline __be16 vlan_proto(const struct sk_buff *skb)
81 if (skb_vlan_tag_present(skb))
83 else if (skb->protocol == htons(ETH_P_8021Q))
84 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
89 static inline bool is_vlan_ip(const struct sk_buff *skb, const struct net *net)
91 struct brnf_net *brnet = net_generic(net, brnf_net_id);
93 return vlan_proto(skb) == htons(ETH_P_IP) && brnet->filter_vlan_tagged;
96 static inline bool is_vlan_ipv6(const struct sk_buff *skb,
97 const struct net *net)
99 struct brnf_net *brnet = net_generic(net, brnf_net_id);
101 return vlan_proto(skb) == htons(ETH_P_IPV6) &&
102 brnet->filter_vlan_tagged;
105 static inline bool is_vlan_arp(const struct sk_buff *skb, const struct net *net)
107 struct brnf_net *brnet = net_generic(net, brnf_net_id);
109 return vlan_proto(skb) == htons(ETH_P_ARP) && brnet->filter_vlan_tagged;
112 static inline __be16 pppoe_proto(const struct sk_buff *skb)
114 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
115 sizeof(struct pppoe_hdr)));
118 static inline bool is_pppoe_ip(const struct sk_buff *skb, const struct net *net)
120 struct brnf_net *brnet = net_generic(net, brnf_net_id);
122 return skb->protocol == htons(ETH_P_PPP_SES) &&
123 pppoe_proto(skb) == htons(PPP_IP) && brnet->filter_pppoe_tagged;
126 static inline bool is_pppoe_ipv6(const struct sk_buff *skb,
127 const struct net *net)
129 struct brnf_net *brnet = net_generic(net, brnf_net_id);
131 return skb->protocol == htons(ETH_P_PPP_SES) &&
132 pppoe_proto(skb) == htons(PPP_IPV6) &&
133 brnet->filter_pppoe_tagged;
136 /* largest possible L2 header, see br_nf_dev_queue_xmit() */
137 #define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN)
139 struct brnf_frag_data {
140 char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH];
147 static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage);
149 static void nf_bridge_info_free(struct sk_buff *skb)
151 skb_ext_del(skb, SKB_EXT_BRIDGE_NF);
154 static inline struct net_device *bridge_parent(const struct net_device *dev)
156 struct net_bridge_port *port;
158 port = br_port_get_rcu(dev);
159 return port ? port->br->dev : NULL;
162 static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
164 return skb_ext_add(skb, SKB_EXT_BRIDGE_NF);
167 unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
169 switch (skb->protocol) {
170 case __cpu_to_be16(ETH_P_8021Q):
172 case __cpu_to_be16(ETH_P_PPP_SES):
173 return PPPOE_SES_HLEN;
179 static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
181 unsigned int len = nf_bridge_encap_header_len(skb);
184 skb->network_header += len;
187 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
189 unsigned int len = nf_bridge_encap_header_len(skb);
191 skb_pull_rcsum(skb, len);
192 skb->network_header += len;
195 /* When handing a packet over to the IP layer
196 * check whether we have a skb that is in the
200 static int br_validate_ipv4(struct net *net, struct sk_buff *skb)
202 const struct iphdr *iph;
205 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
210 /* Basic sanity checks */
211 if (iph->ihl < 5 || iph->version != 4)
214 if (!pskb_may_pull(skb, iph->ihl*4))
218 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
221 len = skb_ip_totlen(skb);
222 if (skb->len < len) {
223 __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS);
225 } else if (len < (iph->ihl*4))
228 if (pskb_trim_rcsum(skb, len)) {
229 __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
233 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
234 /* We should really parse IP options here but until
235 * somebody who actually uses IP options complains to
236 * us we'll just silently ignore the options because
242 __IP_INC_STATS(net, IPSTATS_MIB_CSUMERRORS);
244 __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
249 void nf_bridge_update_protocol(struct sk_buff *skb)
251 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
253 switch (nf_bridge->orig_proto) {
254 case BRNF_PROTO_8021Q:
255 skb->protocol = htons(ETH_P_8021Q);
257 case BRNF_PROTO_PPPOE:
258 skb->protocol = htons(ETH_P_PPP_SES);
260 case BRNF_PROTO_UNCHANGED:
265 /* Obtain the correct destination MAC address, while preserving the original
266 * source MAC address. If we already know this address, we just copy it. If we
267 * don't, we use the neighbour framework to find out. In both cases, we make
268 * sure that br_handle_frame_finish() is called afterwards.
270 int br_nf_pre_routing_finish_bridge(struct net *net, struct sock *sk, struct sk_buff *skb)
272 struct neighbour *neigh;
273 struct dst_entry *dst;
275 skb->dev = bridge_parent(skb->dev);
279 neigh = dst_neigh_lookup_skb(dst, skb);
281 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
284 if ((READ_ONCE(neigh->nud_state) & NUD_CONNECTED) &&
285 READ_ONCE(neigh->hh.hh_len)) {
286 struct net_device *br_indev;
288 br_indev = nf_bridge_get_physindev(skb, net);
290 neigh_release(neigh);
294 neigh_hh_bridge(&neigh->hh, skb);
297 ret = br_handle_frame_finish(net, sk, skb);
299 /* the neighbour function below overwrites the complete
300 * MAC header, so we save the Ethernet source address and
303 skb_copy_from_linear_data_offset(skb,
304 -(ETH_HLEN-ETH_ALEN),
305 nf_bridge->neigh_header,
307 /* tell br_dev_xmit to continue with forwarding */
308 nf_bridge->bridged_dnat = 1;
309 /* FIXME Need to refragment */
310 ret = READ_ONCE(neigh->output)(neigh, skb);
312 neigh_release(neigh);
321 br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb,
322 const struct nf_bridge_info *nf_bridge)
324 return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
327 /* This requires some explaining. If DNAT has taken place,
328 * we will need to fix up the destination Ethernet address.
329 * This is also true when SNAT takes place (for the reply direction).
331 * There are two cases to consider:
332 * 1. The packet was DNAT'ed to a device in the same bridge
333 * port group as it was received on. We can still bridge
335 * 2. The packet was DNAT'ed to a different device, either
336 * a non-bridged device or another bridge port group.
337 * The packet will need to be routed.
339 * The correct way of distinguishing between these two cases is to
340 * call ip_route_input() and to look at skb->dst->dev, which is
341 * changed to the destination device if ip_route_input() succeeds.
343 * Let's first consider the case that ip_route_input() succeeds:
345 * If the output device equals the logical bridge device the packet
346 * came in on, we can consider this bridging. The corresponding MAC
347 * address will be obtained in br_nf_pre_routing_finish_bridge.
348 * Otherwise, the packet is considered to be routed and we just
349 * change the destination MAC address so that the packet will
350 * later be passed up to the IP stack to be routed. For a redirected
351 * packet, ip_route_input() will give back the localhost as output device,
352 * which differs from the bridge device.
354 * Let's now consider the case that ip_route_input() fails:
356 * This can be because the destination address is martian, in which case
357 * the packet will be dropped.
358 * If IP forwarding is disabled, ip_route_input() will fail, while
359 * ip_route_output_key() can return success. The source
360 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
361 * thinks we're handling a locally generated packet and won't care
362 * if IP forwarding is enabled. If the output device equals the logical bridge
363 * device, we proceed as if ip_route_input() succeeded. If it differs from the
364 * logical bridge port or if ip_route_output_key() fails we drop the packet.
366 static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
368 struct net_device *dev = skb->dev, *br_indev;
369 struct iphdr *iph = ip_hdr(skb);
370 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
374 br_indev = nf_bridge_get_physindev(skb, net);
380 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
382 if (nf_bridge->pkt_otherhost) {
383 skb->pkt_type = PACKET_OTHERHOST;
384 nf_bridge->pkt_otherhost = false;
386 nf_bridge->in_prerouting = 0;
387 if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) {
388 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
389 struct in_device *in_dev = __in_dev_get_rcu(dev);
391 /* If err equals -EHOSTUNREACH the error is due to a
392 * martian destination or due to the fact that
393 * forwarding is disabled. For most martian packets,
394 * ip_route_output_key() will fail. It won't fail for 2 types of
395 * martian destinations: loopback destinations and destination
396 * 0.0.0.0. In both cases the packet will be dropped because the
397 * destination is the loopback device and not the bridge. */
398 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
401 rt = ip_route_output(net, iph->daddr, 0,
402 RT_TOS(iph->tos), 0);
404 /* - Bridged-and-DNAT'ed traffic doesn't
405 * require ip_forwarding. */
406 if (rt->dst.dev == dev) {
408 skb_dst_set(skb, &rt->dst);
417 if (skb_dst(skb)->dev == dev) {
420 nf_bridge_update_protocol(skb);
421 nf_bridge_push_encap_header(skb);
422 br_nf_hook_thresh(NF_BR_PRE_ROUTING,
423 net, sk, skb, skb->dev,
425 br_nf_pre_routing_finish_bridge);
428 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
429 skb->pkt_type = PACKET_HOST;
432 rt = bridge_parent_rtable(br_indev);
438 skb_dst_set_noref(skb, &rt->dst);
442 nf_bridge_update_protocol(skb);
443 nf_bridge_push_encap_header(skb);
444 br_nf_hook_thresh(NF_BR_PRE_ROUTING, net, sk, skb, skb->dev, NULL,
445 br_handle_frame_finish);
449 static struct net_device *brnf_get_logical_dev(struct sk_buff *skb,
450 const struct net_device *dev,
451 const struct net *net)
453 struct net_device *vlan, *br;
454 struct brnf_net *brnet = net_generic(net, brnf_net_id);
456 br = bridge_parent(dev);
458 if (brnet->pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
461 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
462 skb_vlan_tag_get(skb) & VLAN_VID_MASK);
464 return vlan ? vlan : br;
467 /* Some common code for IPv4/IPv6 */
468 struct net_device *setup_pre_routing(struct sk_buff *skb, const struct net *net)
470 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
472 if (skb->pkt_type == PACKET_OTHERHOST) {
473 skb->pkt_type = PACKET_HOST;
474 nf_bridge->pkt_otherhost = true;
477 nf_bridge->in_prerouting = 1;
478 nf_bridge->physinif = skb->dev->ifindex;
479 skb->dev = brnf_get_logical_dev(skb, skb->dev, net);
481 if (skb->protocol == htons(ETH_P_8021Q))
482 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
483 else if (skb->protocol == htons(ETH_P_PPP_SES))
484 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
486 /* Must drop socket now because of tproxy. */
491 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
492 * Replicate the checks that IPv4 does on packet reception.
493 * Set skb->dev to the bridge device (i.e. parent of the
494 * receiving device) to make netfilter happy, the REDIRECT
495 * target in particular. Save the original destination IP
496 * address to be able to detect DNAT afterwards. */
497 static unsigned int br_nf_pre_routing(void *priv,
499 const struct nf_hook_state *state)
501 struct nf_bridge_info *nf_bridge;
502 struct net_bridge_port *p;
503 struct net_bridge *br;
504 __u32 len = nf_bridge_encap_header_len(skb);
505 struct brnf_net *brnet;
507 if (unlikely(!pskb_may_pull(skb, len)))
508 return NF_DROP_REASON(skb, SKB_DROP_REASON_PKT_TOO_SMALL, 0);
510 p = br_port_get_rcu(state->in);
512 return NF_DROP_REASON(skb, SKB_DROP_REASON_DEV_READY, 0);
515 brnet = net_generic(state->net, brnf_net_id);
516 if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
517 is_pppoe_ipv6(skb, state->net)) {
518 if (!brnet->call_ip6tables &&
519 !br_opt_get(br, BROPT_NF_CALL_IP6TABLES))
521 if (!ipv6_mod_enabled()) {
522 pr_warn_once("Module ipv6 is disabled, so call_ip6tables is not supported.");
523 return NF_DROP_REASON(skb, SKB_DROP_REASON_IPV6DISABLED, 0);
526 nf_bridge_pull_encap_header_rcsum(skb);
527 return br_nf_pre_routing_ipv6(priv, skb, state);
530 if (!brnet->call_iptables && !br_opt_get(br, BROPT_NF_CALL_IPTABLES))
533 if (!IS_IP(skb) && !is_vlan_ip(skb, state->net) &&
534 !is_pppoe_ip(skb, state->net))
537 nf_bridge_pull_encap_header_rcsum(skb);
539 if (br_validate_ipv4(state->net, skb))
540 return NF_DROP_REASON(skb, SKB_DROP_REASON_IP_INHDR, 0);
542 if (!nf_bridge_alloc(skb))
543 return NF_DROP_REASON(skb, SKB_DROP_REASON_NOMEM, 0);
544 if (!setup_pre_routing(skb, state->net))
545 return NF_DROP_REASON(skb, SKB_DROP_REASON_DEV_READY, 0);
547 nf_bridge = nf_bridge_info_get(skb);
548 nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
550 skb->protocol = htons(ETH_P_IP);
551 skb->transport_header = skb->network_header + ip_hdr(skb)->ihl * 4;
553 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->net, state->sk, skb,
555 br_nf_pre_routing_finish);
560 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
561 /* conntracks' nf_confirm logic cannot handle cloned skbs referencing
562 * the same nf_conn entry, which will happen for multicast (broadcast)
570 * ethX (or Y) receives multicast or broadcast packet containing
571 * an IP packet, not yet in conntrack table.
573 * 1. skb passes through bridge and fake-ip (br_netfilter)Prerouting.
574 * -> skb->_nfct now references a unconfirmed entry
575 * 2. skb is broad/mcast packet. bridge now passes clones out on each bridge
577 * 3. skb gets passed up the stack.
578 * 4. In macvlan case, macvlan driver retains clone(s) of the mcast skb
579 * and schedules a work queue to send them out on the lower devices.
581 * The clone skb->_nfct is not a copy, it is the same entry as the
582 * original skb. The macvlan rx handler then returns RX_HANDLER_PASS.
583 * 5. Normal conntrack hooks (in NF_INET_LOCAL_IN) confirm the orig skb.
585 * The Macvlan broadcast worker and normal confirm path will race.
587 * This race will not happen if step 2 already confirmed a clone. In that
588 * case later steps perform skb_clone() with skb->_nfct already confirmed (in
589 * hash table). This works fine.
591 * But such confirmation won't happen when eb/ip/nftables rules dropped the
592 * packets before they reached the nf_confirm step in postrouting.
594 * Work around this problem by explicit confirmation of the entry at
595 * LOCAL_IN time, before upper layer has a chance to clone the unconfirmed
599 static unsigned int br_nf_local_in(void *priv,
601 const struct nf_hook_state *state)
603 bool promisc = BR_INPUT_SKB_CB(skb)->promisc;
604 struct nf_conntrack *nfct = skb_nfct(skb);
605 const struct nf_ct_hook *ct_hook;
614 if (!nfct || skb->pkt_type == PACKET_HOST)
617 ct = container_of(nfct, struct nf_conn, ct_general);
618 if (likely(nf_ct_is_confirmed(ct)))
621 WARN_ON_ONCE(skb_shared(skb));
622 WARN_ON_ONCE(refcount_read(&nfct->use) != 1);
624 /* We can't call nf_confirm here, it would create a dependency
625 * on nf_conntrack module.
627 ct_hook = rcu_dereference(nf_ct_hook);
630 nf_conntrack_put(nfct);
634 nf_bridge_pull_encap_header(skb);
635 ret = ct_hook->confirm(skb);
636 switch (ret & NF_VERDICT_MASK) {
640 nf_bridge_push_encap_header(skb);
644 ct = container_of(nfct, struct nf_conn, ct_general);
645 WARN_ON_ONCE(!nf_ct_is_confirmed(ct));
651 /* PF_BRIDGE/FORWARD *************************************************/
652 static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
654 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
655 struct net_device *in;
657 if (!IS_ARP(skb) && !is_vlan_arp(skb, net)) {
659 if (skb->protocol == htons(ETH_P_IP))
660 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
662 if (skb->protocol == htons(ETH_P_IPV6))
663 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
665 in = nf_bridge_get_physindev(skb, net);
670 if (nf_bridge->pkt_otherhost) {
671 skb->pkt_type = PACKET_OTHERHOST;
672 nf_bridge->pkt_otherhost = false;
674 nf_bridge_update_protocol(skb);
676 in = *((struct net_device **)(skb->cb));
678 nf_bridge_push_encap_header(skb);
680 br_nf_hook_thresh(NF_BR_FORWARD, net, sk, skb, in, skb->dev,
686 static unsigned int br_nf_forward_ip(struct sk_buff *skb,
687 const struct nf_hook_state *state,
690 struct nf_bridge_info *nf_bridge;
691 struct net_device *parent;
693 nf_bridge = nf_bridge_info_get(skb);
697 /* Need exclusive nf_bridge_info since we might have multiple
698 * different physoutdevs. */
699 if (!nf_bridge_unshare(skb))
700 return NF_DROP_REASON(skb, SKB_DROP_REASON_NOMEM, 0);
702 nf_bridge = nf_bridge_info_get(skb);
704 return NF_DROP_REASON(skb, SKB_DROP_REASON_NOMEM, 0);
706 parent = bridge_parent(state->out);
708 return NF_DROP_REASON(skb, SKB_DROP_REASON_DEV_READY, 0);
710 nf_bridge_pull_encap_header(skb);
712 if (skb->pkt_type == PACKET_OTHERHOST) {
713 skb->pkt_type = PACKET_HOST;
714 nf_bridge->pkt_otherhost = true;
717 if (pf == NFPROTO_IPV4) {
718 if (br_validate_ipv4(state->net, skb))
719 return NF_DROP_REASON(skb, SKB_DROP_REASON_IP_INHDR, 0);
720 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
721 skb->protocol = htons(ETH_P_IP);
722 } else if (pf == NFPROTO_IPV6) {
723 if (br_validate_ipv6(state->net, skb))
724 return NF_DROP_REASON(skb, SKB_DROP_REASON_IP_INHDR, 0);
725 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
726 skb->protocol = htons(ETH_P_IPV6);
732 nf_bridge->physoutdev = skb->dev;
734 NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb,
735 brnf_get_logical_dev(skb, state->in, state->net),
736 parent, br_nf_forward_finish);
741 static unsigned int br_nf_forward_arp(struct sk_buff *skb,
742 const struct nf_hook_state *state)
744 struct net_bridge_port *p;
745 struct net_bridge *br;
746 struct net_device **d = (struct net_device **)(skb->cb);
747 struct brnf_net *brnet;
749 p = br_port_get_rcu(state->out);
754 brnet = net_generic(state->net, brnf_net_id);
755 if (!brnet->call_arptables && !br_opt_get(br, BROPT_NF_CALL_ARPTABLES))
758 if (is_vlan_arp(skb, state->net))
759 nf_bridge_pull_encap_header(skb);
761 if (unlikely(!pskb_may_pull(skb, sizeof(struct arphdr))))
762 return NF_DROP_REASON(skb, SKB_DROP_REASON_PKT_TOO_SMALL, 0);
764 if (arp_hdr(skb)->ar_pln != 4) {
765 if (is_vlan_arp(skb, state->net))
766 nf_bridge_push_encap_header(skb);
770 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb,
771 state->in, state->out, br_nf_forward_finish);
776 /* This is the 'purely bridged' case. For IP, we pass the packet to
777 * netfilter with indev and outdev set to the bridge device,
778 * but we are still able to filter on the 'real' indev/outdev
779 * because of the physdev module. For ARP, indev and outdev are the
782 static unsigned int br_nf_forward(void *priv,
784 const struct nf_hook_state *state)
786 if (IS_IP(skb) || is_vlan_ip(skb, state->net) ||
787 is_pppoe_ip(skb, state->net))
788 return br_nf_forward_ip(skb, state, NFPROTO_IPV4);
789 if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
790 is_pppoe_ipv6(skb, state->net))
791 return br_nf_forward_ip(skb, state, NFPROTO_IPV6);
792 if (IS_ARP(skb) || is_vlan_arp(skb, state->net))
793 return br_nf_forward_arp(skb, state);
798 static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
800 struct brnf_frag_data *data;
803 data = this_cpu_ptr(&brnf_frag_data_storage);
804 err = skb_cow_head(skb, data->size);
811 if (data->vlan_proto)
812 __vlan_hwaccel_put_tag(skb, data->vlan_proto, data->vlan_tci);
814 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
815 __skb_push(skb, data->encap_size);
817 nf_bridge_info_free(skb);
818 return br_dev_queue_push_xmit(net, sk, skb);
822 br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
823 int (*output)(struct net *, struct sock *, struct sk_buff *))
825 unsigned int mtu = ip_skb_dst_mtu(sk, skb);
826 struct iphdr *iph = ip_hdr(skb);
828 if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
829 (IPCB(skb)->frag_max_size &&
830 IPCB(skb)->frag_max_size > mtu))) {
831 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
836 return ip_do_fragment(net, sk, skb, output);
839 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
841 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
843 if (nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
844 return PPPOE_SES_HLEN;
848 static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
850 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
851 unsigned int mtu, mtu_reserved;
853 mtu_reserved = nf_bridge_mtu_reduction(skb);
856 if (nf_bridge->pkt_otherhost) {
857 skb->pkt_type = PACKET_OTHERHOST;
858 nf_bridge->pkt_otherhost = false;
861 if (nf_bridge->frag_max_size && nf_bridge->frag_max_size < mtu)
862 mtu = nf_bridge->frag_max_size;
864 nf_bridge_update_protocol(skb);
865 nf_bridge_push_encap_header(skb);
867 if (skb_is_gso(skb) || skb->len + mtu_reserved <= mtu) {
868 nf_bridge_info_free(skb);
869 return br_dev_queue_push_xmit(net, sk, skb);
872 /* This is wrong! We should preserve the original fragment
873 * boundaries by preserving frag_list rather than refragmenting.
875 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) &&
876 skb->protocol == htons(ETH_P_IP)) {
877 struct brnf_frag_data *data;
879 if (br_validate_ipv4(net, skb))
882 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
884 data = this_cpu_ptr(&brnf_frag_data_storage);
886 if (skb_vlan_tag_present(skb)) {
887 data->vlan_tci = skb->vlan_tci;
888 data->vlan_proto = skb->vlan_proto;
890 data->vlan_proto = 0;
893 data->encap_size = nf_bridge_encap_header_len(skb);
894 data->size = ETH_HLEN + data->encap_size;
896 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
899 return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit);
901 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) &&
902 skb->protocol == htons(ETH_P_IPV6)) {
903 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
904 struct brnf_frag_data *data;
906 if (br_validate_ipv6(net, skb))
909 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
911 data = this_cpu_ptr(&brnf_frag_data_storage);
912 data->encap_size = nf_bridge_encap_header_len(skb);
913 data->size = ETH_HLEN + data->encap_size;
915 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
919 return v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit);
924 nf_bridge_info_free(skb);
925 return br_dev_queue_push_xmit(net, sk, skb);
931 /* PF_BRIDGE/POST_ROUTING ********************************************/
932 static unsigned int br_nf_post_routing(void *priv,
934 const struct nf_hook_state *state)
936 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
937 struct net_device *realoutdev = bridge_parent(skb->dev);
940 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
941 * on a bridge, but was delivered locally and is now being routed:
943 * POST_ROUTING was already invoked from the ip stack.
945 if (!nf_bridge || !nf_bridge->physoutdev)
949 return NF_DROP_REASON(skb, SKB_DROP_REASON_DEV_READY, 0);
951 if (IS_IP(skb) || is_vlan_ip(skb, state->net) ||
952 is_pppoe_ip(skb, state->net))
954 else if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
955 is_pppoe_ipv6(skb, state->net))
960 if (skb->pkt_type == PACKET_OTHERHOST) {
961 skb->pkt_type = PACKET_HOST;
962 nf_bridge->pkt_otherhost = true;
965 nf_bridge_pull_encap_header(skb);
966 if (pf == NFPROTO_IPV4)
967 skb->protocol = htons(ETH_P_IP);
969 skb->protocol = htons(ETH_P_IPV6);
971 NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb,
973 br_nf_dev_queue_xmit);
978 /* IP/SABOTAGE *****************************************************/
979 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
980 * for the second time. */
981 static unsigned int ip_sabotage_in(void *priv,
983 const struct nf_hook_state *state)
985 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
988 if (nf_bridge->sabotage_in_done)
991 if (!nf_bridge->in_prerouting &&
992 !netif_is_l3_master(skb->dev) &&
993 !netif_is_l3_slave(skb->dev)) {
994 nf_bridge->sabotage_in_done = 1;
995 state->okfn(state->net, state->sk, skb);
1003 /* This is called when br_netfilter has called into iptables/netfilter,
1004 * and DNAT has taken place on a bridge-forwarded packet.
1006 * neigh->output has created a new MAC header, with local br0 MAC
1009 * This restores the original MAC saddr of the bridged packet
1010 * before invoking bridge forward logic to transmit the packet.
1012 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
1014 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
1015 struct net_device *br_indev;
1017 br_indev = nf_bridge_get_physindev(skb, dev_net(skb->dev));
1023 skb_pull(skb, ETH_HLEN);
1024 nf_bridge->bridged_dnat = 0;
1026 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
1028 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
1029 nf_bridge->neigh_header,
1030 ETH_HLEN - ETH_ALEN);
1031 skb->dev = br_indev;
1033 nf_bridge->physoutdev = NULL;
1034 br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
1037 static int br_nf_dev_xmit(struct sk_buff *skb)
1039 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
1041 if (nf_bridge && nf_bridge->bridged_dnat) {
1042 br_nf_pre_routing_finish_bridge_slow(skb);
1048 static const struct nf_br_ops br_ops = {
1049 .br_dev_xmit_hook = br_nf_dev_xmit,
1052 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
1053 * br_dev_queue_push_xmit is called afterwards */
1054 static const struct nf_hook_ops br_nf_ops[] = {
1056 .hook = br_nf_pre_routing,
1057 .pf = NFPROTO_BRIDGE,
1058 .hooknum = NF_BR_PRE_ROUTING,
1059 .priority = NF_BR_PRI_BRNF,
1061 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1063 .hook = br_nf_local_in,
1064 .pf = NFPROTO_BRIDGE,
1065 .hooknum = NF_BR_LOCAL_IN,
1066 .priority = NF_BR_PRI_LAST,
1070 .hook = br_nf_forward,
1071 .pf = NFPROTO_BRIDGE,
1072 .hooknum = NF_BR_FORWARD,
1073 .priority = NF_BR_PRI_BRNF,
1076 .hook = br_nf_post_routing,
1077 .pf = NFPROTO_BRIDGE,
1078 .hooknum = NF_BR_POST_ROUTING,
1079 .priority = NF_BR_PRI_LAST,
1082 .hook = ip_sabotage_in,
1084 .hooknum = NF_INET_PRE_ROUTING,
1085 .priority = NF_IP_PRI_FIRST,
1088 .hook = ip_sabotage_in,
1090 .hooknum = NF_INET_PRE_ROUTING,
1091 .priority = NF_IP6_PRI_FIRST,
1095 static int brnf_device_event(struct notifier_block *unused, unsigned long event,
1098 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1099 struct brnf_net *brnet;
1103 if (event != NETDEV_REGISTER || !netif_is_bridge_master(dev))
1109 brnet = net_generic(net, brnf_net_id);
1113 ret = nf_register_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
1117 brnet->enabled = true;
1121 static struct notifier_block brnf_notifier __read_mostly = {
1122 .notifier_call = brnf_device_event,
1125 /* recursively invokes nf_hook_slow (again), skipping already-called
1126 * hooks (< NF_BR_PRI_BRNF).
1128 * Called with rcu read lock held.
1130 int br_nf_hook_thresh(unsigned int hook, struct net *net,
1131 struct sock *sk, struct sk_buff *skb,
1132 struct net_device *indev,
1133 struct net_device *outdev,
1134 int (*okfn)(struct net *, struct sock *,
1137 const struct nf_hook_entries *e;
1138 struct nf_hook_state state;
1139 struct nf_hook_ops **ops;
1143 e = rcu_dereference(net->nf.hooks_bridge[hook]);
1145 return okfn(net, sk, skb);
1147 ops = nf_hook_entries_get_hook_ops(e);
1148 for (i = 0; i < e->num_hook_entries; i++) {
1149 /* These hooks have already been called */
1150 if (ops[i]->priority < NF_BR_PRI_BRNF)
1153 /* These hooks have not been called yet, run them. */
1154 if (ops[i]->priority > NF_BR_PRI_BRNF)
1157 /* take a closer look at NF_BR_PRI_BRNF. */
1158 if (ops[i]->hook == br_nf_pre_routing) {
1159 /* This hook diverted the skb to this function,
1160 * hooks after this have not been run yet.
1167 nf_hook_state_init(&state, hook, NFPROTO_BRIDGE, indev, outdev,
1170 ret = nf_hook_slow(skb, &state, e, i);
1172 ret = okfn(net, sk, skb);
1177 #ifdef CONFIG_SYSCTL
1179 int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
1180 void *buffer, size_t *lenp, loff_t *ppos)
1184 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1186 if (write && *(int *)(ctl->data))
1187 *(int *)(ctl->data) = 1;
1191 static struct ctl_table brnf_table[] = {
1193 .procname = "bridge-nf-call-arptables",
1194 .maxlen = sizeof(int),
1196 .proc_handler = brnf_sysctl_call_tables,
1199 .procname = "bridge-nf-call-iptables",
1200 .maxlen = sizeof(int),
1202 .proc_handler = brnf_sysctl_call_tables,
1205 .procname = "bridge-nf-call-ip6tables",
1206 .maxlen = sizeof(int),
1208 .proc_handler = brnf_sysctl_call_tables,
1211 .procname = "bridge-nf-filter-vlan-tagged",
1212 .maxlen = sizeof(int),
1214 .proc_handler = brnf_sysctl_call_tables,
1217 .procname = "bridge-nf-filter-pppoe-tagged",
1218 .maxlen = sizeof(int),
1220 .proc_handler = brnf_sysctl_call_tables,
1223 .procname = "bridge-nf-pass-vlan-input-dev",
1224 .maxlen = sizeof(int),
1226 .proc_handler = brnf_sysctl_call_tables,
1231 static inline void br_netfilter_sysctl_default(struct brnf_net *brnf)
1233 brnf->call_iptables = 1;
1234 brnf->call_ip6tables = 1;
1235 brnf->call_arptables = 1;
1236 brnf->filter_vlan_tagged = 0;
1237 brnf->filter_pppoe_tagged = 0;
1238 brnf->pass_vlan_indev = 0;
1241 static int br_netfilter_sysctl_init_net(struct net *net)
1243 struct ctl_table *table = brnf_table;
1244 struct brnf_net *brnet;
1246 if (!net_eq(net, &init_net)) {
1247 table = kmemdup(table, sizeof(brnf_table), GFP_KERNEL);
1252 brnet = net_generic(net, brnf_net_id);
1253 table[0].data = &brnet->call_arptables;
1254 table[1].data = &brnet->call_iptables;
1255 table[2].data = &brnet->call_ip6tables;
1256 table[3].data = &brnet->filter_vlan_tagged;
1257 table[4].data = &brnet->filter_pppoe_tagged;
1258 table[5].data = &brnet->pass_vlan_indev;
1260 br_netfilter_sysctl_default(brnet);
1262 brnet->ctl_hdr = register_net_sysctl_sz(net, "net/bridge", table,
1263 ARRAY_SIZE(brnf_table));
1264 if (!brnet->ctl_hdr) {
1265 if (!net_eq(net, &init_net))
1274 static void br_netfilter_sysctl_exit_net(struct net *net,
1275 struct brnf_net *brnet)
1277 struct ctl_table *table = brnet->ctl_hdr->ctl_table_arg;
1279 unregister_net_sysctl_table(brnet->ctl_hdr);
1280 if (!net_eq(net, &init_net))
1284 static int __net_init brnf_init_net(struct net *net)
1286 return br_netfilter_sysctl_init_net(net);
1290 static void __net_exit brnf_exit_net(struct net *net)
1292 struct brnf_net *brnet;
1294 brnet = net_generic(net, brnf_net_id);
1295 if (brnet->enabled) {
1296 nf_unregister_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
1297 brnet->enabled = false;
1300 #ifdef CONFIG_SYSCTL
1301 br_netfilter_sysctl_exit_net(net, brnet);
1305 static struct pernet_operations brnf_net_ops __read_mostly = {
1306 #ifdef CONFIG_SYSCTL
1307 .init = brnf_init_net,
1309 .exit = brnf_exit_net,
1311 .size = sizeof(struct brnf_net),
1314 static int __init br_netfilter_init(void)
1318 ret = register_pernet_subsys(&brnf_net_ops);
1322 ret = register_netdevice_notifier(&brnf_notifier);
1324 unregister_pernet_subsys(&brnf_net_ops);
1328 RCU_INIT_POINTER(nf_br_ops, &br_ops);
1329 printk(KERN_NOTICE "Bridge firewalling registered\n");
1333 static void __exit br_netfilter_fini(void)
1335 RCU_INIT_POINTER(nf_br_ops, NULL);
1336 unregister_netdevice_notifier(&brnf_notifier);
1337 unregister_pernet_subsys(&brnf_net_ops);
1340 module_init(br_netfilter_init);
1341 module_exit(br_netfilter_fini);
1343 MODULE_LICENSE("GPL");
1344 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1345 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1346 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");