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;
52 struct ctl_table_header *ctl_hdr;
55 /* default value is 1 */
60 /* default value is 0 */
61 int filter_vlan_tagged;
62 int filter_pppoe_tagged;
67 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
69 #define IS_IPV6(skb) \
70 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
73 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
75 static inline __be16 vlan_proto(const struct sk_buff *skb)
77 if (skb_vlan_tag_present(skb))
79 else if (skb->protocol == htons(ETH_P_8021Q))
80 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
85 static inline bool is_vlan_ip(const struct sk_buff *skb, const struct net *net)
87 struct brnf_net *brnet = net_generic(net, brnf_net_id);
89 return vlan_proto(skb) == htons(ETH_P_IP) && brnet->filter_vlan_tagged;
92 static inline bool is_vlan_ipv6(const struct sk_buff *skb,
93 const struct net *net)
95 struct brnf_net *brnet = net_generic(net, brnf_net_id);
97 return vlan_proto(skb) == htons(ETH_P_IPV6) &&
98 brnet->filter_vlan_tagged;
101 static inline bool is_vlan_arp(const struct sk_buff *skb, const struct net *net)
103 struct brnf_net *brnet = net_generic(net, brnf_net_id);
105 return vlan_proto(skb) == htons(ETH_P_ARP) && brnet->filter_vlan_tagged;
108 static inline __be16 pppoe_proto(const struct sk_buff *skb)
110 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
111 sizeof(struct pppoe_hdr)));
114 static inline bool is_pppoe_ip(const struct sk_buff *skb, const struct net *net)
116 struct brnf_net *brnet = net_generic(net, brnf_net_id);
118 return skb->protocol == htons(ETH_P_PPP_SES) &&
119 pppoe_proto(skb) == htons(PPP_IP) && brnet->filter_pppoe_tagged;
122 static inline bool is_pppoe_ipv6(const struct sk_buff *skb,
123 const struct net *net)
125 struct brnf_net *brnet = net_generic(net, brnf_net_id);
127 return skb->protocol == htons(ETH_P_PPP_SES) &&
128 pppoe_proto(skb) == htons(PPP_IPV6) &&
129 brnet->filter_pppoe_tagged;
132 /* largest possible L2 header, see br_nf_dev_queue_xmit() */
133 #define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN)
135 struct brnf_frag_data {
136 char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH];
143 static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage);
145 static void nf_bridge_info_free(struct sk_buff *skb)
147 skb_ext_del(skb, SKB_EXT_BRIDGE_NF);
150 static inline struct net_device *bridge_parent(const struct net_device *dev)
152 struct net_bridge_port *port;
154 port = br_port_get_rcu(dev);
155 return port ? port->br->dev : NULL;
158 static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
160 return skb_ext_add(skb, SKB_EXT_BRIDGE_NF);
163 unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
165 switch (skb->protocol) {
166 case __cpu_to_be16(ETH_P_8021Q):
168 case __cpu_to_be16(ETH_P_PPP_SES):
169 return PPPOE_SES_HLEN;
175 static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
177 unsigned int len = nf_bridge_encap_header_len(skb);
180 skb->network_header += len;
183 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
185 unsigned int len = nf_bridge_encap_header_len(skb);
187 skb_pull_rcsum(skb, len);
188 skb->network_header += len;
191 /* When handing a packet over to the IP layer
192 * check whether we have a skb that is in the
196 static int br_validate_ipv4(struct net *net, struct sk_buff *skb)
198 const struct iphdr *iph;
201 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
206 /* Basic sanity checks */
207 if (iph->ihl < 5 || iph->version != 4)
210 if (!pskb_may_pull(skb, iph->ihl*4))
214 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
217 len = skb_ip_totlen(skb);
218 if (skb->len < len) {
219 __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS);
221 } else if (len < (iph->ihl*4))
224 if (pskb_trim_rcsum(skb, len)) {
225 __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
229 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
230 /* We should really parse IP options here but until
231 * somebody who actually uses IP options complains to
232 * us we'll just silently ignore the options because
238 __IP_INC_STATS(net, IPSTATS_MIB_CSUMERRORS);
240 __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
245 void nf_bridge_update_protocol(struct sk_buff *skb)
247 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
249 switch (nf_bridge->orig_proto) {
250 case BRNF_PROTO_8021Q:
251 skb->protocol = htons(ETH_P_8021Q);
253 case BRNF_PROTO_PPPOE:
254 skb->protocol = htons(ETH_P_PPP_SES);
256 case BRNF_PROTO_UNCHANGED:
261 /* Obtain the correct destination MAC address, while preserving the original
262 * source MAC address. If we already know this address, we just copy it. If we
263 * don't, we use the neighbour framework to find out. In both cases, we make
264 * sure that br_handle_frame_finish() is called afterwards.
266 int br_nf_pre_routing_finish_bridge(struct net *net, struct sock *sk, struct sk_buff *skb)
268 struct neighbour *neigh;
269 struct dst_entry *dst;
271 skb->dev = bridge_parent(skb->dev);
275 neigh = dst_neigh_lookup_skb(dst, skb);
277 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
280 if ((READ_ONCE(neigh->nud_state) & NUD_CONNECTED) &&
281 READ_ONCE(neigh->hh.hh_len)) {
282 struct net_device *br_indev;
284 br_indev = nf_bridge_get_physindev(skb, net);
286 neigh_release(neigh);
290 neigh_hh_bridge(&neigh->hh, skb);
293 ret = br_handle_frame_finish(net, sk, skb);
295 /* the neighbour function below overwrites the complete
296 * MAC header, so we save the Ethernet source address and
299 skb_copy_from_linear_data_offset(skb,
300 -(ETH_HLEN-ETH_ALEN),
301 nf_bridge->neigh_header,
303 /* tell br_dev_xmit to continue with forwarding */
304 nf_bridge->bridged_dnat = 1;
305 /* FIXME Need to refragment */
306 ret = READ_ONCE(neigh->output)(neigh, skb);
308 neigh_release(neigh);
317 br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb,
318 const struct nf_bridge_info *nf_bridge)
320 return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
323 /* This requires some explaining. If DNAT has taken place,
324 * we will need to fix up the destination Ethernet address.
325 * This is also true when SNAT takes place (for the reply direction).
327 * There are two cases to consider:
328 * 1. The packet was DNAT'ed to a device in the same bridge
329 * port group as it was received on. We can still bridge
331 * 2. The packet was DNAT'ed to a different device, either
332 * a non-bridged device or another bridge port group.
333 * The packet will need to be routed.
335 * The correct way of distinguishing between these two cases is to
336 * call ip_route_input() and to look at skb->dst->dev, which is
337 * changed to the destination device if ip_route_input() succeeds.
339 * Let's first consider the case that ip_route_input() succeeds:
341 * If the output device equals the logical bridge device the packet
342 * came in on, we can consider this bridging. The corresponding MAC
343 * address will be obtained in br_nf_pre_routing_finish_bridge.
344 * Otherwise, the packet is considered to be routed and we just
345 * change the destination MAC address so that the packet will
346 * later be passed up to the IP stack to be routed. For a redirected
347 * packet, ip_route_input() will give back the localhost as output device,
348 * which differs from the bridge device.
350 * Let's now consider the case that ip_route_input() fails:
352 * This can be because the destination address is martian, in which case
353 * the packet will be dropped.
354 * If IP forwarding is disabled, ip_route_input() will fail, while
355 * ip_route_output_key() can return success. The source
356 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
357 * thinks we're handling a locally generated packet and won't care
358 * if IP forwarding is enabled. If the output device equals the logical bridge
359 * device, we proceed as if ip_route_input() succeeded. If it differs from the
360 * logical bridge port or if ip_route_output_key() fails we drop the packet.
362 static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
364 struct net_device *dev = skb->dev, *br_indev;
365 struct iphdr *iph = ip_hdr(skb);
366 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
370 br_indev = nf_bridge_get_physindev(skb, net);
376 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
378 if (nf_bridge->pkt_otherhost) {
379 skb->pkt_type = PACKET_OTHERHOST;
380 nf_bridge->pkt_otherhost = false;
382 nf_bridge->in_prerouting = 0;
383 if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) {
384 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
385 struct in_device *in_dev = __in_dev_get_rcu(dev);
387 /* If err equals -EHOSTUNREACH the error is due to a
388 * martian destination or due to the fact that
389 * forwarding is disabled. For most martian packets,
390 * ip_route_output_key() will fail. It won't fail for 2 types of
391 * martian destinations: loopback destinations and destination
392 * 0.0.0.0. In both cases the packet will be dropped because the
393 * destination is the loopback device and not the bridge. */
394 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
397 rt = ip_route_output(net, iph->daddr, 0,
398 RT_TOS(iph->tos), 0);
400 /* - Bridged-and-DNAT'ed traffic doesn't
401 * require ip_forwarding. */
402 if (rt->dst.dev == dev) {
404 skb_dst_set(skb, &rt->dst);
413 if (skb_dst(skb)->dev == dev) {
416 nf_bridge_update_protocol(skb);
417 nf_bridge_push_encap_header(skb);
418 br_nf_hook_thresh(NF_BR_PRE_ROUTING,
419 net, sk, skb, skb->dev,
421 br_nf_pre_routing_finish_bridge);
424 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
425 skb->pkt_type = PACKET_HOST;
428 rt = bridge_parent_rtable(br_indev);
434 skb_dst_set_noref(skb, &rt->dst);
438 nf_bridge_update_protocol(skb);
439 nf_bridge_push_encap_header(skb);
440 br_nf_hook_thresh(NF_BR_PRE_ROUTING, net, sk, skb, skb->dev, NULL,
441 br_handle_frame_finish);
445 static struct net_device *brnf_get_logical_dev(struct sk_buff *skb,
446 const struct net_device *dev,
447 const struct net *net)
449 struct net_device *vlan, *br;
450 struct brnf_net *brnet = net_generic(net, brnf_net_id);
452 br = bridge_parent(dev);
454 if (brnet->pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
457 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
458 skb_vlan_tag_get(skb) & VLAN_VID_MASK);
460 return vlan ? vlan : br;
463 /* Some common code for IPv4/IPv6 */
464 struct net_device *setup_pre_routing(struct sk_buff *skb, const struct net *net)
466 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
468 if (skb->pkt_type == PACKET_OTHERHOST) {
469 skb->pkt_type = PACKET_HOST;
470 nf_bridge->pkt_otherhost = true;
473 nf_bridge->in_prerouting = 1;
474 nf_bridge->physinif = skb->dev->ifindex;
475 skb->dev = brnf_get_logical_dev(skb, skb->dev, net);
477 if (skb->protocol == htons(ETH_P_8021Q))
478 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
479 else if (skb->protocol == htons(ETH_P_PPP_SES))
480 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
482 /* Must drop socket now because of tproxy. */
487 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
488 * Replicate the checks that IPv4 does on packet reception.
489 * Set skb->dev to the bridge device (i.e. parent of the
490 * receiving device) to make netfilter happy, the REDIRECT
491 * target in particular. Save the original destination IP
492 * address to be able to detect DNAT afterwards. */
493 static unsigned int br_nf_pre_routing(void *priv,
495 const struct nf_hook_state *state)
497 struct nf_bridge_info *nf_bridge;
498 struct net_bridge_port *p;
499 struct net_bridge *br;
500 __u32 len = nf_bridge_encap_header_len(skb);
501 struct brnf_net *brnet;
503 if (unlikely(!pskb_may_pull(skb, len)))
506 p = br_port_get_rcu(state->in);
511 brnet = net_generic(state->net, brnf_net_id);
512 if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
513 is_pppoe_ipv6(skb, state->net)) {
514 if (!brnet->call_ip6tables &&
515 !br_opt_get(br, BROPT_NF_CALL_IP6TABLES))
517 if (!ipv6_mod_enabled()) {
518 pr_warn_once("Module ipv6 is disabled, so call_ip6tables is not supported.");
522 nf_bridge_pull_encap_header_rcsum(skb);
523 return br_nf_pre_routing_ipv6(priv, skb, state);
526 if (!brnet->call_iptables && !br_opt_get(br, BROPT_NF_CALL_IPTABLES))
529 if (!IS_IP(skb) && !is_vlan_ip(skb, state->net) &&
530 !is_pppoe_ip(skb, state->net))
533 nf_bridge_pull_encap_header_rcsum(skb);
535 if (br_validate_ipv4(state->net, skb))
538 if (!nf_bridge_alloc(skb))
540 if (!setup_pre_routing(skb, state->net))
543 nf_bridge = nf_bridge_info_get(skb);
544 nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
546 skb->protocol = htons(ETH_P_IP);
547 skb->transport_header = skb->network_header + ip_hdr(skb)->ihl * 4;
549 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->net, state->sk, skb,
551 br_nf_pre_routing_finish);
557 /* PF_BRIDGE/FORWARD *************************************************/
558 static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
560 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
561 struct net_device *in;
563 if (!IS_ARP(skb) && !is_vlan_arp(skb, net)) {
565 if (skb->protocol == htons(ETH_P_IP))
566 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
568 if (skb->protocol == htons(ETH_P_IPV6))
569 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
571 in = nf_bridge_get_physindev(skb, net);
576 if (nf_bridge->pkt_otherhost) {
577 skb->pkt_type = PACKET_OTHERHOST;
578 nf_bridge->pkt_otherhost = false;
580 nf_bridge_update_protocol(skb);
582 in = *((struct net_device **)(skb->cb));
584 nf_bridge_push_encap_header(skb);
586 br_nf_hook_thresh(NF_BR_FORWARD, net, sk, skb, in, skb->dev,
592 /* This is the 'purely bridged' case. For IP, we pass the packet to
593 * netfilter with indev and outdev set to the bridge device,
594 * but we are still able to filter on the 'real' indev/outdev
595 * because of the physdev module. For ARP, indev and outdev are the
597 static unsigned int br_nf_forward_ip(void *priv,
599 const struct nf_hook_state *state)
601 struct nf_bridge_info *nf_bridge;
602 struct net_device *parent;
605 nf_bridge = nf_bridge_info_get(skb);
609 /* Need exclusive nf_bridge_info since we might have multiple
610 * different physoutdevs. */
611 if (!nf_bridge_unshare(skb))
614 nf_bridge = nf_bridge_info_get(skb);
618 parent = bridge_parent(state->out);
622 if (IS_IP(skb) || is_vlan_ip(skb, state->net) ||
623 is_pppoe_ip(skb, state->net))
625 else if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
626 is_pppoe_ipv6(skb, state->net))
631 nf_bridge_pull_encap_header(skb);
633 if (skb->pkt_type == PACKET_OTHERHOST) {
634 skb->pkt_type = PACKET_HOST;
635 nf_bridge->pkt_otherhost = true;
638 if (pf == NFPROTO_IPV4) {
639 if (br_validate_ipv4(state->net, skb))
641 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
644 if (pf == NFPROTO_IPV6) {
645 if (br_validate_ipv6(state->net, skb))
647 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
650 nf_bridge->physoutdev = skb->dev;
651 if (pf == NFPROTO_IPV4)
652 skb->protocol = htons(ETH_P_IP);
654 skb->protocol = htons(ETH_P_IPV6);
656 NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb,
657 brnf_get_logical_dev(skb, state->in, state->net),
658 parent, br_nf_forward_finish);
663 static unsigned int br_nf_forward_arp(void *priv,
665 const struct nf_hook_state *state)
667 struct net_bridge_port *p;
668 struct net_bridge *br;
669 struct net_device **d = (struct net_device **)(skb->cb);
670 struct brnf_net *brnet;
672 p = br_port_get_rcu(state->out);
677 brnet = net_generic(state->net, brnf_net_id);
678 if (!brnet->call_arptables && !br_opt_get(br, BROPT_NF_CALL_ARPTABLES))
682 if (!is_vlan_arp(skb, state->net))
684 nf_bridge_pull_encap_header(skb);
687 if (unlikely(!pskb_may_pull(skb, sizeof(struct arphdr))))
690 if (arp_hdr(skb)->ar_pln != 4) {
691 if (is_vlan_arp(skb, state->net))
692 nf_bridge_push_encap_header(skb);
696 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb,
697 state->in, state->out, br_nf_forward_finish);
702 static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
704 struct brnf_frag_data *data;
707 data = this_cpu_ptr(&brnf_frag_data_storage);
708 err = skb_cow_head(skb, data->size);
715 if (data->vlan_proto)
716 __vlan_hwaccel_put_tag(skb, data->vlan_proto, data->vlan_tci);
718 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
719 __skb_push(skb, data->encap_size);
721 nf_bridge_info_free(skb);
722 return br_dev_queue_push_xmit(net, sk, skb);
726 br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
727 int (*output)(struct net *, struct sock *, struct sk_buff *))
729 unsigned int mtu = ip_skb_dst_mtu(sk, skb);
730 struct iphdr *iph = ip_hdr(skb);
732 if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
733 (IPCB(skb)->frag_max_size &&
734 IPCB(skb)->frag_max_size > mtu))) {
735 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
740 return ip_do_fragment(net, sk, skb, output);
743 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
745 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
747 if (nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
748 return PPPOE_SES_HLEN;
752 static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
754 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
755 unsigned int mtu, mtu_reserved;
757 mtu_reserved = nf_bridge_mtu_reduction(skb);
760 if (nf_bridge->pkt_otherhost) {
761 skb->pkt_type = PACKET_OTHERHOST;
762 nf_bridge->pkt_otherhost = false;
765 if (nf_bridge->frag_max_size && nf_bridge->frag_max_size < mtu)
766 mtu = nf_bridge->frag_max_size;
768 nf_bridge_update_protocol(skb);
769 nf_bridge_push_encap_header(skb);
771 if (skb_is_gso(skb) || skb->len + mtu_reserved <= mtu) {
772 nf_bridge_info_free(skb);
773 return br_dev_queue_push_xmit(net, sk, skb);
776 /* This is wrong! We should preserve the original fragment
777 * boundaries by preserving frag_list rather than refragmenting.
779 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) &&
780 skb->protocol == htons(ETH_P_IP)) {
781 struct brnf_frag_data *data;
783 if (br_validate_ipv4(net, skb))
786 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
788 data = this_cpu_ptr(&brnf_frag_data_storage);
790 if (skb_vlan_tag_present(skb)) {
791 data->vlan_tci = skb->vlan_tci;
792 data->vlan_proto = skb->vlan_proto;
794 data->vlan_proto = 0;
797 data->encap_size = nf_bridge_encap_header_len(skb);
798 data->size = ETH_HLEN + data->encap_size;
800 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
803 return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit);
805 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) &&
806 skb->protocol == htons(ETH_P_IPV6)) {
807 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
808 struct brnf_frag_data *data;
810 if (br_validate_ipv6(net, skb))
813 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
815 data = this_cpu_ptr(&brnf_frag_data_storage);
816 data->encap_size = nf_bridge_encap_header_len(skb);
817 data->size = ETH_HLEN + data->encap_size;
819 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
823 return v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit);
828 nf_bridge_info_free(skb);
829 return br_dev_queue_push_xmit(net, sk, skb);
835 /* PF_BRIDGE/POST_ROUTING ********************************************/
836 static unsigned int br_nf_post_routing(void *priv,
838 const struct nf_hook_state *state)
840 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
841 struct net_device *realoutdev = bridge_parent(skb->dev);
844 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
845 * on a bridge, but was delivered locally and is now being routed:
847 * POST_ROUTING was already invoked from the ip stack.
849 if (!nf_bridge || !nf_bridge->physoutdev)
855 if (IS_IP(skb) || is_vlan_ip(skb, state->net) ||
856 is_pppoe_ip(skb, state->net))
858 else if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
859 is_pppoe_ipv6(skb, state->net))
864 if (skb->pkt_type == PACKET_OTHERHOST) {
865 skb->pkt_type = PACKET_HOST;
866 nf_bridge->pkt_otherhost = true;
869 nf_bridge_pull_encap_header(skb);
870 if (pf == NFPROTO_IPV4)
871 skb->protocol = htons(ETH_P_IP);
873 skb->protocol = htons(ETH_P_IPV6);
875 NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb,
877 br_nf_dev_queue_xmit);
882 /* IP/SABOTAGE *****************************************************/
883 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
884 * for the second time. */
885 static unsigned int ip_sabotage_in(void *priv,
887 const struct nf_hook_state *state)
889 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
892 if (nf_bridge->sabotage_in_done)
895 if (!nf_bridge->in_prerouting &&
896 !netif_is_l3_master(skb->dev) &&
897 !netif_is_l3_slave(skb->dev)) {
898 nf_bridge->sabotage_in_done = 1;
899 state->okfn(state->net, state->sk, skb);
907 /* This is called when br_netfilter has called into iptables/netfilter,
908 * and DNAT has taken place on a bridge-forwarded packet.
910 * neigh->output has created a new MAC header, with local br0 MAC
913 * This restores the original MAC saddr of the bridged packet
914 * before invoking bridge forward logic to transmit the packet.
916 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
918 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
919 struct net_device *br_indev;
921 br_indev = nf_bridge_get_physindev(skb, dev_net(skb->dev));
927 skb_pull(skb, ETH_HLEN);
928 nf_bridge->bridged_dnat = 0;
930 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
932 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
933 nf_bridge->neigh_header,
934 ETH_HLEN - ETH_ALEN);
937 nf_bridge->physoutdev = NULL;
938 br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
941 static int br_nf_dev_xmit(struct sk_buff *skb)
943 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
945 if (nf_bridge && nf_bridge->bridged_dnat) {
946 br_nf_pre_routing_finish_bridge_slow(skb);
952 static const struct nf_br_ops br_ops = {
953 .br_dev_xmit_hook = br_nf_dev_xmit,
956 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
957 * br_dev_queue_push_xmit is called afterwards */
958 static const struct nf_hook_ops br_nf_ops[] = {
960 .hook = br_nf_pre_routing,
961 .pf = NFPROTO_BRIDGE,
962 .hooknum = NF_BR_PRE_ROUTING,
963 .priority = NF_BR_PRI_BRNF,
966 .hook = br_nf_forward_ip,
967 .pf = NFPROTO_BRIDGE,
968 .hooknum = NF_BR_FORWARD,
969 .priority = NF_BR_PRI_BRNF - 1,
972 .hook = br_nf_forward_arp,
973 .pf = NFPROTO_BRIDGE,
974 .hooknum = NF_BR_FORWARD,
975 .priority = NF_BR_PRI_BRNF,
978 .hook = br_nf_post_routing,
979 .pf = NFPROTO_BRIDGE,
980 .hooknum = NF_BR_POST_ROUTING,
981 .priority = NF_BR_PRI_LAST,
984 .hook = ip_sabotage_in,
986 .hooknum = NF_INET_PRE_ROUTING,
987 .priority = NF_IP_PRI_FIRST,
990 .hook = ip_sabotage_in,
992 .hooknum = NF_INET_PRE_ROUTING,
993 .priority = NF_IP6_PRI_FIRST,
997 static int brnf_device_event(struct notifier_block *unused, unsigned long event,
1000 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1001 struct brnf_net *brnet;
1005 if (event != NETDEV_REGISTER || !netif_is_bridge_master(dev))
1011 brnet = net_generic(net, brnf_net_id);
1015 ret = nf_register_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
1019 brnet->enabled = true;
1023 static struct notifier_block brnf_notifier __read_mostly = {
1024 .notifier_call = brnf_device_event,
1027 /* recursively invokes nf_hook_slow (again), skipping already-called
1028 * hooks (< NF_BR_PRI_BRNF).
1030 * Called with rcu read lock held.
1032 int br_nf_hook_thresh(unsigned int hook, struct net *net,
1033 struct sock *sk, struct sk_buff *skb,
1034 struct net_device *indev,
1035 struct net_device *outdev,
1036 int (*okfn)(struct net *, struct sock *,
1039 const struct nf_hook_entries *e;
1040 struct nf_hook_state state;
1041 struct nf_hook_ops **ops;
1045 e = rcu_dereference(net->nf.hooks_bridge[hook]);
1047 return okfn(net, sk, skb);
1049 ops = nf_hook_entries_get_hook_ops(e);
1050 for (i = 0; i < e->num_hook_entries; i++) {
1051 /* These hooks have already been called */
1052 if (ops[i]->priority < NF_BR_PRI_BRNF)
1055 /* These hooks have not been called yet, run them. */
1056 if (ops[i]->priority > NF_BR_PRI_BRNF)
1059 /* take a closer look at NF_BR_PRI_BRNF. */
1060 if (ops[i]->hook == br_nf_pre_routing) {
1061 /* This hook diverted the skb to this function,
1062 * hooks after this have not been run yet.
1069 nf_hook_state_init(&state, hook, NFPROTO_BRIDGE, indev, outdev,
1072 ret = nf_hook_slow(skb, &state, e, i);
1074 ret = okfn(net, sk, skb);
1079 #ifdef CONFIG_SYSCTL
1081 int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
1082 void *buffer, size_t *lenp, loff_t *ppos)
1086 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1088 if (write && *(int *)(ctl->data))
1089 *(int *)(ctl->data) = 1;
1093 static struct ctl_table brnf_table[] = {
1095 .procname = "bridge-nf-call-arptables",
1096 .maxlen = sizeof(int),
1098 .proc_handler = brnf_sysctl_call_tables,
1101 .procname = "bridge-nf-call-iptables",
1102 .maxlen = sizeof(int),
1104 .proc_handler = brnf_sysctl_call_tables,
1107 .procname = "bridge-nf-call-ip6tables",
1108 .maxlen = sizeof(int),
1110 .proc_handler = brnf_sysctl_call_tables,
1113 .procname = "bridge-nf-filter-vlan-tagged",
1114 .maxlen = sizeof(int),
1116 .proc_handler = brnf_sysctl_call_tables,
1119 .procname = "bridge-nf-filter-pppoe-tagged",
1120 .maxlen = sizeof(int),
1122 .proc_handler = brnf_sysctl_call_tables,
1125 .procname = "bridge-nf-pass-vlan-input-dev",
1126 .maxlen = sizeof(int),
1128 .proc_handler = brnf_sysctl_call_tables,
1133 static inline void br_netfilter_sysctl_default(struct brnf_net *brnf)
1135 brnf->call_iptables = 1;
1136 brnf->call_ip6tables = 1;
1137 brnf->call_arptables = 1;
1138 brnf->filter_vlan_tagged = 0;
1139 brnf->filter_pppoe_tagged = 0;
1140 brnf->pass_vlan_indev = 0;
1143 static int br_netfilter_sysctl_init_net(struct net *net)
1145 struct ctl_table *table = brnf_table;
1146 struct brnf_net *brnet;
1148 if (!net_eq(net, &init_net)) {
1149 table = kmemdup(table, sizeof(brnf_table), GFP_KERNEL);
1154 brnet = net_generic(net, brnf_net_id);
1155 table[0].data = &brnet->call_arptables;
1156 table[1].data = &brnet->call_iptables;
1157 table[2].data = &brnet->call_ip6tables;
1158 table[3].data = &brnet->filter_vlan_tagged;
1159 table[4].data = &brnet->filter_pppoe_tagged;
1160 table[5].data = &brnet->pass_vlan_indev;
1162 br_netfilter_sysctl_default(brnet);
1164 brnet->ctl_hdr = register_net_sysctl_sz(net, "net/bridge", table,
1165 ARRAY_SIZE(brnf_table));
1166 if (!brnet->ctl_hdr) {
1167 if (!net_eq(net, &init_net))
1176 static void br_netfilter_sysctl_exit_net(struct net *net,
1177 struct brnf_net *brnet)
1179 struct ctl_table *table = brnet->ctl_hdr->ctl_table_arg;
1181 unregister_net_sysctl_table(brnet->ctl_hdr);
1182 if (!net_eq(net, &init_net))
1186 static int __net_init brnf_init_net(struct net *net)
1188 return br_netfilter_sysctl_init_net(net);
1192 static void __net_exit brnf_exit_net(struct net *net)
1194 struct brnf_net *brnet;
1196 brnet = net_generic(net, brnf_net_id);
1197 if (brnet->enabled) {
1198 nf_unregister_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
1199 brnet->enabled = false;
1202 #ifdef CONFIG_SYSCTL
1203 br_netfilter_sysctl_exit_net(net, brnet);
1207 static struct pernet_operations brnf_net_ops __read_mostly = {
1208 #ifdef CONFIG_SYSCTL
1209 .init = brnf_init_net,
1211 .exit = brnf_exit_net,
1213 .size = sizeof(struct brnf_net),
1216 static int __init br_netfilter_init(void)
1220 ret = register_pernet_subsys(&brnf_net_ops);
1224 ret = register_netdevice_notifier(&brnf_notifier);
1226 unregister_pernet_subsys(&brnf_net_ops);
1230 RCU_INIT_POINTER(nf_br_ops, &br_ops);
1231 printk(KERN_NOTICE "Bridge firewalling registered\n");
1235 static void __exit br_netfilter_fini(void)
1237 RCU_INIT_POINTER(nf_br_ops, NULL);
1238 unregister_netdevice_notifier(&brnf_notifier);
1239 unregister_pernet_subsys(&brnf_net_ops);
1242 module_init(br_netfilter_init);
1243 module_exit(br_netfilter_fini);
1245 MODULE_LICENSE("GPL");
1246 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1247 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1248 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");