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 = 0;
53 static int brnf_filter_pppoe_tagged __read_mostly = 0;
54 static int brnf_pass_vlan_indev __read_mostly = 0;
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 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4)
115 struct brnf_frag_data {
116 char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH];
121 static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage);
124 static struct nf_bridge_info *nf_bridge_info_get(const struct sk_buff *skb)
126 return skb->nf_bridge;
129 static void nf_bridge_info_free(struct sk_buff *skb)
131 if (skb->nf_bridge) {
132 nf_bridge_put(skb->nf_bridge);
133 skb->nf_bridge = NULL;
137 static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
139 struct net_bridge_port *port;
141 port = br_port_get_rcu(dev);
142 return port ? &port->br->fake_rtable : NULL;
145 static inline struct net_device *bridge_parent(const struct net_device *dev)
147 struct net_bridge_port *port;
149 port = br_port_get_rcu(dev);
150 return port ? port->br->dev : NULL;
153 static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
155 skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
156 if (likely(skb->nf_bridge))
157 atomic_set(&(skb->nf_bridge->use), 1);
159 return skb->nf_bridge;
162 static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
164 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
166 if (atomic_read(&nf_bridge->use) > 1) {
167 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
170 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
171 atomic_set(&tmp->use, 1);
173 nf_bridge_put(nf_bridge);
179 static unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
181 switch (skb->protocol) {
182 case __cpu_to_be16(ETH_P_8021Q):
184 case __cpu_to_be16(ETH_P_PPP_SES):
185 return PPPOE_SES_HLEN;
191 static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
193 unsigned int len = nf_bridge_encap_header_len(skb);
196 skb->network_header -= len;
199 static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
201 unsigned int len = nf_bridge_encap_header_len(skb);
204 skb->network_header += len;
207 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
209 unsigned int len = nf_bridge_encap_header_len(skb);
211 skb_pull_rcsum(skb, len);
212 skb->network_header += len;
215 /* When handing a packet over to the IP layer
216 * check whether we have a skb that is in the
220 static int br_validate_ipv4(struct sk_buff *skb)
222 const struct iphdr *iph;
223 struct net_device *dev = skb->dev;
226 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
231 /* Basic sanity checks */
232 if (iph->ihl < 5 || iph->version != 4)
235 if (!pskb_may_pull(skb, iph->ihl*4))
239 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
242 len = ntohs(iph->tot_len);
243 if (skb->len < len) {
244 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INTRUNCATEDPKTS);
246 } else if (len < (iph->ihl*4))
249 if (pskb_trim_rcsum(skb, len)) {
250 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
254 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
255 /* We should really parse IP options here but until
256 * somebody who actually uses IP options complains to
257 * us we'll just silently ignore the options because
263 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
268 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff
271 static int check_hbh_len(struct sk_buff *skb)
273 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
275 const unsigned char *nh = skb_network_header(skb);
277 int len = (raw[1] + 1) << 3;
279 if ((raw + len) - skb->data > skb_headlen(skb))
286 int optlen = nh[off + 1] + 2;
297 if (nh[off + 1] != 4 || (off & 3) != 2)
299 pkt_len = ntohl(*(__be32 *)(nh + off + 2));
300 if (pkt_len <= IPV6_MAXPLEN ||
301 ipv6_hdr(skb)->payload_len)
303 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
305 if (pskb_trim_rcsum(skb,
306 pkt_len + sizeof(struct ipv6hdr)))
308 nh = skb_network_header(skb);
324 /* Equivalent to br_validate_ipv4 for IPv6 */
325 static int br_validate_ipv6(struct sk_buff *skb)
327 const struct ipv6hdr *hdr;
328 struct net_device *dev = skb->dev;
329 struct inet6_dev *idev = in6_dev_get(skb->dev);
331 u8 ip6h_len = sizeof(struct ipv6hdr);
333 if (!pskb_may_pull(skb, ip6h_len))
336 if (skb->len < ip6h_len)
341 if (hdr->version != 6)
344 pkt_len = ntohs(hdr->payload_len);
346 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
347 if (pkt_len + ip6h_len > skb->len) {
348 IP6_INC_STATS_BH(dev_net(dev), idev,
349 IPSTATS_MIB_INTRUNCATEDPKTS);
352 if (pskb_trim_rcsum(skb, pkt_len + ip6h_len)) {
353 IP6_INC_STATS_BH(dev_net(dev), idev,
354 IPSTATS_MIB_INDISCARDS);
358 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
361 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
362 /* No IP options in IPv6 header; however it should be
363 * checked if some next headers need special treatment
368 IP6_INC_STATS_BH(dev_net(dev), idev, IPSTATS_MIB_INHDRERRORS);
373 static void nf_bridge_update_protocol(struct sk_buff *skb)
375 switch (skb->nf_bridge->orig_proto) {
376 case BRNF_PROTO_8021Q:
377 skb->protocol = htons(ETH_P_8021Q);
379 case BRNF_PROTO_PPPOE:
380 skb->protocol = htons(ETH_P_PPP_SES);
382 case BRNF_PROTO_UNCHANGED:
387 /* Obtain the correct destination MAC address, while preserving the original
388 * source MAC address. If we already know this address, we just copy it. If we
389 * don't, we use the neighbour framework to find out. In both cases, we make
390 * sure that br_handle_frame_finish() is called afterwards.
392 static int br_nf_pre_routing_finish_bridge(struct sock *sk, struct sk_buff *skb)
394 struct neighbour *neigh;
395 struct dst_entry *dst;
397 skb->dev = bridge_parent(skb->dev);
401 neigh = dst_neigh_lookup_skb(dst, skb);
403 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
406 if (neigh->hh.hh_len) {
407 neigh_hh_bridge(&neigh->hh, skb);
408 skb->dev = nf_bridge->physindev;
409 ret = br_handle_frame_finish(sk, skb);
411 /* the neighbour function below overwrites the complete
412 * MAC header, so we save the Ethernet source address and
415 skb_copy_from_linear_data_offset(skb,
416 -(ETH_HLEN-ETH_ALEN),
417 nf_bridge->neigh_header,
419 /* tell br_dev_xmit to continue with forwarding */
420 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
421 /* FIXME Need to refragment */
422 ret = neigh->output(neigh, skb);
424 neigh_release(neigh);
432 static bool daddr_was_changed(const struct sk_buff *skb,
433 const struct nf_bridge_info *nf_bridge)
435 switch (skb->protocol) {
436 case htons(ETH_P_IP):
437 return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
438 case htons(ETH_P_IPV6):
439 return memcmp(&nf_bridge->ipv6_daddr, &ipv6_hdr(skb)->daddr,
440 sizeof(ipv6_hdr(skb)->daddr)) != 0;
446 /* PF_BRIDGE/PRE_ROUTING: Undo the changes made for ip6tables
447 * PREROUTING and continue the bridge PRE_ROUTING hook. See comment
448 * for br_nf_pre_routing_finish(), same logic is used here but
449 * equivalent IPv6 function ip6_route_input() called indirectly.
451 static int br_nf_pre_routing_finish_ipv6(struct sock *sk, struct sk_buff *skb)
453 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
455 struct net_device *dev = skb->dev;
456 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
458 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
460 if (nf_bridge->pkt_otherhost) {
461 skb->pkt_type = PACKET_OTHERHOST;
462 nf_bridge->pkt_otherhost = false;
464 nf_bridge->mask &= ~BRNF_NF_BRIDGE_PREROUTING;
465 if (daddr_was_changed(skb, nf_bridge)) {
467 v6ops->route_input(skb);
469 if (skb_dst(skb)->error) {
474 if (skb_dst(skb)->dev == dev) {
475 skb->dev = nf_bridge->physindev;
476 nf_bridge_update_protocol(skb);
477 nf_bridge_push_encap_header(skb);
478 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING,
479 sk, skb, skb->dev, NULL,
480 br_nf_pre_routing_finish_bridge,
484 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
485 skb->pkt_type = PACKET_HOST;
487 rt = bridge_parent_rtable(nf_bridge->physindev);
492 skb_dst_set_noref(skb, &rt->dst);
495 skb->dev = nf_bridge->physindev;
496 nf_bridge_update_protocol(skb);
497 nf_bridge_push_encap_header(skb);
498 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, sk, skb,
500 br_handle_frame_finish, 1);
505 /* This requires some explaining. If DNAT has taken place,
506 * we will need to fix up the destination Ethernet address.
507 * This is also true when SNAT takes place (for the reply direction).
509 * There are two cases to consider:
510 * 1. The packet was DNAT'ed to a device in the same bridge
511 * port group as it was received on. We can still bridge
513 * 2. The packet was DNAT'ed to a different device, either
514 * a non-bridged device or another bridge port group.
515 * The packet will need to be routed.
517 * The correct way of distinguishing between these two cases is to
518 * call ip_route_input() and to look at skb->dst->dev, which is
519 * changed to the destination device if ip_route_input() succeeds.
521 * Let's first consider the case that ip_route_input() succeeds:
523 * If the output device equals the logical bridge device the packet
524 * came in on, we can consider this bridging. The corresponding MAC
525 * address will be obtained in br_nf_pre_routing_finish_bridge.
526 * Otherwise, the packet is considered to be routed and we just
527 * change the destination MAC address so that the packet will
528 * later be passed up to the IP stack to be routed. For a redirected
529 * packet, ip_route_input() will give back the localhost as output device,
530 * which differs from the bridge device.
532 * Let's now consider the case that ip_route_input() fails:
534 * This can be because the destination address is martian, in which case
535 * the packet will be dropped.
536 * If IP forwarding is disabled, ip_route_input() will fail, while
537 * ip_route_output_key() can return success. The source
538 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
539 * thinks we're handling a locally generated packet and won't care
540 * if IP forwarding is enabled. If the output device equals the logical bridge
541 * device, we proceed as if ip_route_input() succeeded. If it differs from the
542 * logical bridge port or if ip_route_output_key() fails we drop the packet.
544 static int br_nf_pre_routing_finish(struct sock *sk, struct sk_buff *skb)
546 struct net_device *dev = skb->dev;
547 struct iphdr *iph = ip_hdr(skb);
548 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
552 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
554 if (nf_bridge->pkt_otherhost) {
555 skb->pkt_type = PACKET_OTHERHOST;
556 nf_bridge->pkt_otherhost = false;
558 nf_bridge->mask &= ~BRNF_NF_BRIDGE_PREROUTING;
559 if (daddr_was_changed(skb, nf_bridge)) {
560 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
561 struct in_device *in_dev = __in_dev_get_rcu(dev);
563 /* If err equals -EHOSTUNREACH the error is due to a
564 * martian destination or due to the fact that
565 * forwarding is disabled. For most martian packets,
566 * ip_route_output_key() will fail. It won't fail for 2 types of
567 * martian destinations: loopback destinations and destination
568 * 0.0.0.0. In both cases the packet will be dropped because the
569 * destination is the loopback device and not the bridge. */
570 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
573 rt = ip_route_output(dev_net(dev), iph->daddr, 0,
574 RT_TOS(iph->tos), 0);
576 /* - Bridged-and-DNAT'ed traffic doesn't
577 * require ip_forwarding. */
578 if (rt->dst.dev == dev) {
579 skb_dst_set(skb, &rt->dst);
588 if (skb_dst(skb)->dev == dev) {
590 skb->dev = nf_bridge->physindev;
591 nf_bridge_update_protocol(skb);
592 nf_bridge_push_encap_header(skb);
593 NF_HOOK_THRESH(NFPROTO_BRIDGE,
595 sk, skb, skb->dev, NULL,
596 br_nf_pre_routing_finish_bridge,
600 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
601 skb->pkt_type = PACKET_HOST;
604 rt = bridge_parent_rtable(nf_bridge->physindev);
609 skb_dst_set_noref(skb, &rt->dst);
612 skb->dev = nf_bridge->physindev;
613 nf_bridge_update_protocol(skb);
614 nf_bridge_push_encap_header(skb);
615 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, sk, skb,
617 br_handle_frame_finish, 1);
622 static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
624 struct net_device *vlan, *br;
626 br = bridge_parent(dev);
627 if (brnf_pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
630 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
631 skb_vlan_tag_get(skb) & VLAN_VID_MASK);
633 return vlan ? vlan : br;
636 /* Some common code for IPv4/IPv6 */
637 static struct net_device *setup_pre_routing(struct sk_buff *skb)
639 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
641 if (skb->pkt_type == PACKET_OTHERHOST) {
642 skb->pkt_type = PACKET_HOST;
643 nf_bridge->pkt_otherhost = true;
646 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
647 nf_bridge->physindev = skb->dev;
648 skb->dev = brnf_get_logical_dev(skb, skb->dev);
650 if (skb->protocol == htons(ETH_P_8021Q))
651 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
652 else if (skb->protocol == htons(ETH_P_PPP_SES))
653 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
655 /* Must drop socket now because of tproxy. */
660 /* Replicate the checks that IPv6 does on packet reception and pass the packet
663 static unsigned int br_nf_pre_routing_ipv6(const struct nf_hook_ops *ops,
665 const struct nf_hook_state *state)
667 struct nf_bridge_info *nf_bridge;
669 if (br_validate_ipv6(skb))
672 nf_bridge_put(skb->nf_bridge);
673 if (!nf_bridge_alloc(skb))
675 if (!setup_pre_routing(skb))
678 nf_bridge = nf_bridge_info_get(skb);
679 nf_bridge->ipv6_daddr = ipv6_hdr(skb)->daddr;
681 skb->protocol = htons(ETH_P_IPV6);
682 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, state->sk, skb,
684 br_nf_pre_routing_finish_ipv6);
689 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
690 * Replicate the checks that IPv4 does on packet reception.
691 * Set skb->dev to the bridge device (i.e. parent of the
692 * receiving device) to make netfilter happy, the REDIRECT
693 * target in particular. Save the original destination IP
694 * address to be able to detect DNAT afterwards. */
695 static unsigned int br_nf_pre_routing(const struct nf_hook_ops *ops,
697 const struct nf_hook_state *state)
699 struct nf_bridge_info *nf_bridge;
700 struct net_bridge_port *p;
701 struct net_bridge *br;
702 __u32 len = nf_bridge_encap_header_len(skb);
704 if (unlikely(!pskb_may_pull(skb, len)))
707 p = br_port_get_rcu(state->in);
712 if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
713 if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
716 nf_bridge_pull_encap_header_rcsum(skb);
717 return br_nf_pre_routing_ipv6(ops, skb, state);
720 if (!brnf_call_iptables && !br->nf_call_iptables)
723 if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
726 nf_bridge_pull_encap_header_rcsum(skb);
728 if (br_validate_ipv4(skb))
731 nf_bridge_put(skb->nf_bridge);
732 if (!nf_bridge_alloc(skb))
734 if (!setup_pre_routing(skb))
737 nf_bridge = nf_bridge_info_get(skb);
738 nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
740 skb->protocol = htons(ETH_P_IP);
742 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->sk, skb,
744 br_nf_pre_routing_finish);
750 /* PF_BRIDGE/LOCAL_IN ************************************************/
751 /* The packet is locally destined, which requires a real
752 * dst_entry, so detach the fake one. On the way up, the
753 * packet would pass through PRE_ROUTING again (which already
754 * took place when the packet entered the bridge), but we
755 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
756 * prevent this from happening. */
757 static unsigned int br_nf_local_in(const struct nf_hook_ops *ops,
759 const struct nf_hook_state *state)
761 br_drop_fake_rtable(skb);
765 /* PF_BRIDGE/FORWARD *************************************************/
766 static int br_nf_forward_finish(struct sock *sk, struct sk_buff *skb)
768 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
769 struct net_device *in;
771 if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
773 if (skb->protocol == htons(ETH_P_IP))
774 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
776 if (skb->protocol == htons(ETH_P_IPV6))
777 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
779 in = nf_bridge->physindev;
780 if (nf_bridge->pkt_otherhost) {
781 skb->pkt_type = PACKET_OTHERHOST;
782 nf_bridge->pkt_otherhost = false;
784 nf_bridge_update_protocol(skb);
786 in = *((struct net_device **)(skb->cb));
788 nf_bridge_push_encap_header(skb);
790 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, sk, skb,
791 in, skb->dev, br_forward_finish, 1);
796 /* This is the 'purely bridged' case. For IP, we pass the packet to
797 * netfilter with indev and outdev set to the bridge device,
798 * but we are still able to filter on the 'real' indev/outdev
799 * because of the physdev module. For ARP, indev and outdev are the
801 static unsigned int br_nf_forward_ip(const struct nf_hook_ops *ops,
803 const struct nf_hook_state *state)
805 struct nf_bridge_info *nf_bridge;
806 struct net_device *parent;
812 /* Need exclusive nf_bridge_info since we might have multiple
813 * different physoutdevs. */
814 if (!nf_bridge_unshare(skb))
817 nf_bridge = nf_bridge_info_get(skb);
821 parent = bridge_parent(state->out);
825 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
827 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
832 nf_bridge_pull_encap_header(skb);
834 if (skb->pkt_type == PACKET_OTHERHOST) {
835 skb->pkt_type = PACKET_HOST;
836 nf_bridge->pkt_otherhost = true;
839 if (pf == NFPROTO_IPV4) {
840 if (br_validate_ipv4(skb))
842 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
845 if (pf == NFPROTO_IPV6) {
846 if (br_validate_ipv6(skb))
848 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
851 nf_bridge->physoutdev = skb->dev;
852 if (pf == NFPROTO_IPV4)
853 skb->protocol = htons(ETH_P_IP);
855 skb->protocol = htons(ETH_P_IPV6);
857 NF_HOOK(pf, NF_INET_FORWARD, NULL, skb,
858 brnf_get_logical_dev(skb, state->in),
859 parent, br_nf_forward_finish);
864 static unsigned int br_nf_forward_arp(const struct nf_hook_ops *ops,
866 const struct nf_hook_state *state)
868 struct net_bridge_port *p;
869 struct net_bridge *br;
870 struct net_device **d = (struct net_device **)(skb->cb);
872 p = br_port_get_rcu(state->out);
877 if (!brnf_call_arptables && !br->nf_call_arptables)
881 if (!IS_VLAN_ARP(skb))
883 nf_bridge_pull_encap_header(skb);
886 if (arp_hdr(skb)->ar_pln != 4) {
887 if (IS_VLAN_ARP(skb))
888 nf_bridge_push_encap_header(skb);
892 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->sk, skb,
893 state->in, state->out, br_nf_forward_finish);
898 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) || IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
899 static int br_nf_push_frag_xmit(struct sock *sk, struct sk_buff *skb)
901 struct brnf_frag_data *data;
904 data = this_cpu_ptr(&brnf_frag_data_storage);
905 err = skb_cow_head(skb, data->size);
912 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
913 __skb_push(skb, data->encap_size);
915 nf_bridge_info_free(skb);
916 return br_dev_queue_push_xmit(sk, skb);
920 static int br_nf_ip_fragment(struct sock *sk, struct sk_buff *skb,
921 int (*output)(struct sock *, struct sk_buff *))
923 unsigned int mtu = ip_skb_dst_mtu(skb);
924 struct iphdr *iph = ip_hdr(skb);
925 struct rtable *rt = skb_rtable(skb);
926 struct net_device *dev = rt->dst.dev;
928 if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
929 (IPCB(skb)->frag_max_size &&
930 IPCB(skb)->frag_max_size > mtu))) {
931 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
936 return ip_do_fragment(sk, skb, output);
939 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
941 if (skb->nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
942 return PPPOE_SES_HLEN;
946 static int br_nf_dev_queue_xmit(struct sock *sk, struct sk_buff *skb)
948 struct nf_bridge_info *nf_bridge;
949 unsigned int mtu_reserved;
951 mtu_reserved = nf_bridge_mtu_reduction(skb);
953 if (skb_is_gso(skb) || skb->len + mtu_reserved <= skb->dev->mtu) {
954 nf_bridge_info_free(skb);
955 return br_dev_queue_push_xmit(sk, skb);
958 nf_bridge = nf_bridge_info_get(skb);
960 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4)
961 /* This is wrong! We should preserve the original fragment
962 * boundaries by preserving frag_list rather than refragmenting.
964 if (skb->protocol == htons(ETH_P_IP)) {
965 struct brnf_frag_data *data;
967 if (br_validate_ipv4(skb))
970 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
972 nf_bridge_update_protocol(skb);
974 data = this_cpu_ptr(&brnf_frag_data_storage);
975 data->encap_size = nf_bridge_encap_header_len(skb);
976 data->size = ETH_HLEN + data->encap_size;
978 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
981 return br_nf_ip_fragment(sk, skb, br_nf_push_frag_xmit);
984 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
985 if (skb->protocol == htons(ETH_P_IPV6)) {
986 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
987 struct brnf_frag_data *data;
989 if (br_validate_ipv6(skb))
992 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
994 nf_bridge_update_protocol(skb);
996 data = this_cpu_ptr(&brnf_frag_data_storage);
997 data->encap_size = nf_bridge_encap_header_len(skb);
998 data->size = ETH_HLEN + data->encap_size;
1000 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
1004 return v6ops->fragment(sk, skb, br_nf_push_frag_xmit);
1009 nf_bridge_info_free(skb);
1010 return br_dev_queue_push_xmit(sk, skb);
1013 /* PF_BRIDGE/POST_ROUTING ********************************************/
1014 static unsigned int br_nf_post_routing(const struct nf_hook_ops *ops,
1015 struct sk_buff *skb,
1016 const struct nf_hook_state *state)
1018 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
1019 struct net_device *realoutdev = bridge_parent(skb->dev);
1022 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
1023 * on a bridge, but was delivered locally and is now being routed:
1025 * POST_ROUTING was already invoked from the ip stack.
1027 if (!nf_bridge || !nf_bridge->physoutdev)
1033 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
1035 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
1040 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
1041 * about the value of skb->pkt_type. */
1042 if (skb->pkt_type == PACKET_OTHERHOST) {
1043 skb->pkt_type = PACKET_HOST;
1044 nf_bridge->pkt_otherhost = true;
1047 nf_bridge_pull_encap_header(skb);
1048 if (pf == NFPROTO_IPV4)
1049 skb->protocol = htons(ETH_P_IP);
1051 skb->protocol = htons(ETH_P_IPV6);
1053 NF_HOOK(pf, NF_INET_POST_ROUTING, state->sk, skb,
1055 br_nf_dev_queue_xmit);
1060 /* IP/SABOTAGE *****************************************************/
1061 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
1062 * for the second time. */
1063 static unsigned int ip_sabotage_in(const struct nf_hook_ops *ops,
1064 struct sk_buff *skb,
1065 const struct nf_hook_state *state)
1067 if (skb->nf_bridge &&
1068 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
1075 /* This is called when br_netfilter has called into iptables/netfilter,
1076 * and DNAT has taken place on a bridge-forwarded packet.
1078 * neigh->output has created a new MAC header, with local br0 MAC
1081 * This restores the original MAC saddr of the bridged packet
1082 * before invoking bridge forward logic to transmit the packet.
1084 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
1086 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
1088 skb_pull(skb, ETH_HLEN);
1089 nf_bridge->mask &= ~BRNF_BRIDGED_DNAT;
1091 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
1093 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
1094 nf_bridge->neigh_header,
1095 ETH_HLEN - ETH_ALEN);
1096 skb->dev = nf_bridge->physindev;
1098 nf_bridge->physoutdev = NULL;
1099 br_handle_frame_finish(NULL, skb);
1102 static int br_nf_dev_xmit(struct sk_buff *skb)
1104 if (skb->nf_bridge && (skb->nf_bridge->mask & BRNF_BRIDGED_DNAT)) {
1105 br_nf_pre_routing_finish_bridge_slow(skb);
1111 static const struct nf_br_ops br_ops = {
1112 .br_dev_xmit_hook = br_nf_dev_xmit,
1115 void br_netfilter_enable(void)
1118 EXPORT_SYMBOL_GPL(br_netfilter_enable);
1120 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
1121 * br_dev_queue_push_xmit is called afterwards */
1122 static struct nf_hook_ops br_nf_ops[] __read_mostly = {
1124 .hook = br_nf_pre_routing,
1125 .owner = THIS_MODULE,
1126 .pf = NFPROTO_BRIDGE,
1127 .hooknum = NF_BR_PRE_ROUTING,
1128 .priority = NF_BR_PRI_BRNF,
1131 .hook = br_nf_local_in,
1132 .owner = THIS_MODULE,
1133 .pf = NFPROTO_BRIDGE,
1134 .hooknum = NF_BR_LOCAL_IN,
1135 .priority = NF_BR_PRI_BRNF,
1138 .hook = br_nf_forward_ip,
1139 .owner = THIS_MODULE,
1140 .pf = NFPROTO_BRIDGE,
1141 .hooknum = NF_BR_FORWARD,
1142 .priority = NF_BR_PRI_BRNF - 1,
1145 .hook = br_nf_forward_arp,
1146 .owner = THIS_MODULE,
1147 .pf = NFPROTO_BRIDGE,
1148 .hooknum = NF_BR_FORWARD,
1149 .priority = NF_BR_PRI_BRNF,
1152 .hook = br_nf_post_routing,
1153 .owner = THIS_MODULE,
1154 .pf = NFPROTO_BRIDGE,
1155 .hooknum = NF_BR_POST_ROUTING,
1156 .priority = NF_BR_PRI_LAST,
1159 .hook = ip_sabotage_in,
1160 .owner = THIS_MODULE,
1162 .hooknum = NF_INET_PRE_ROUTING,
1163 .priority = NF_IP_PRI_FIRST,
1166 .hook = ip_sabotage_in,
1167 .owner = THIS_MODULE,
1169 .hooknum = NF_INET_PRE_ROUTING,
1170 .priority = NF_IP6_PRI_FIRST,
1174 #ifdef CONFIG_SYSCTL
1176 int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
1177 void __user *buffer, size_t *lenp, loff_t *ppos)
1181 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1183 if (write && *(int *)(ctl->data))
1184 *(int *)(ctl->data) = 1;
1188 static struct ctl_table brnf_table[] = {
1190 .procname = "bridge-nf-call-arptables",
1191 .data = &brnf_call_arptables,
1192 .maxlen = sizeof(int),
1194 .proc_handler = brnf_sysctl_call_tables,
1197 .procname = "bridge-nf-call-iptables",
1198 .data = &brnf_call_iptables,
1199 .maxlen = sizeof(int),
1201 .proc_handler = brnf_sysctl_call_tables,
1204 .procname = "bridge-nf-call-ip6tables",
1205 .data = &brnf_call_ip6tables,
1206 .maxlen = sizeof(int),
1208 .proc_handler = brnf_sysctl_call_tables,
1211 .procname = "bridge-nf-filter-vlan-tagged",
1212 .data = &brnf_filter_vlan_tagged,
1213 .maxlen = sizeof(int),
1215 .proc_handler = brnf_sysctl_call_tables,
1218 .procname = "bridge-nf-filter-pppoe-tagged",
1219 .data = &brnf_filter_pppoe_tagged,
1220 .maxlen = sizeof(int),
1222 .proc_handler = brnf_sysctl_call_tables,
1225 .procname = "bridge-nf-pass-vlan-input-dev",
1226 .data = &brnf_pass_vlan_indev,
1227 .maxlen = sizeof(int),
1229 .proc_handler = brnf_sysctl_call_tables,
1235 static int __init br_netfilter_init(void)
1239 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1243 #ifdef CONFIG_SYSCTL
1244 brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
1245 if (brnf_sysctl_header == NULL) {
1247 "br_netfilter: can't register to sysctl.\n");
1248 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1252 RCU_INIT_POINTER(nf_br_ops, &br_ops);
1253 printk(KERN_NOTICE "Bridge firewalling registered\n");
1257 static void __exit br_netfilter_fini(void)
1259 RCU_INIT_POINTER(nf_br_ops, NULL);
1260 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1261 #ifdef CONFIG_SYSCTL
1262 unregister_net_sysctl_table(brnf_sysctl_header);
1266 module_init(br_netfilter_init);
1267 module_exit(br_netfilter_fini);
1269 MODULE_LICENSE("GPL");
1270 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1271 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1272 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");