3 * Linux ethernet bridge
6 * Lennert Buytenhek <buytenh@gnu.org>
7 * Bart De Schuymer <bdschuym@pandora.be>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 * Lennert dedicates this file to Kerstin Wurdinger.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <linux/if_pppox.h>
27 #include <linux/ppp_defs.h>
28 #include <linux/netfilter_bridge.h>
29 #include <uapi/linux/netfilter_bridge.h>
30 #include <linux/netfilter_ipv4.h>
31 #include <linux/netfilter_ipv6.h>
32 #include <linux/netfilter_arp.h>
33 #include <linux/in_route.h>
34 #include <linux/rculist.h>
35 #include <linux/inetdevice.h>
39 #include <net/addrconf.h>
40 #include <net/route.h>
41 #include <net/netfilter/br_netfilter.h>
42 #include <net/netns/generic.h>
44 #include <linux/uaccess.h>
45 #include "br_private.h"
47 #include <linux/sysctl.h>
50 static unsigned int brnf_net_id __read_mostly;
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 = ntohs(iph->tot_len);
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 ((neigh->nud_state & NUD_CONNECTED) && neigh->hh.hh_len) {
285 neigh_hh_bridge(&neigh->hh, skb);
286 skb->dev = nf_bridge->physindev;
287 ret = br_handle_frame_finish(net, sk, skb);
289 /* the neighbour function below overwrites the complete
290 * MAC header, so we save the Ethernet source address and
293 skb_copy_from_linear_data_offset(skb,
294 -(ETH_HLEN-ETH_ALEN),
295 nf_bridge->neigh_header,
297 /* tell br_dev_xmit to continue with forwarding */
298 nf_bridge->bridged_dnat = 1;
299 /* FIXME Need to refragment */
300 ret = neigh->output(neigh, skb);
302 neigh_release(neigh);
311 br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb,
312 const struct nf_bridge_info *nf_bridge)
314 return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
317 /* This requires some explaining. If DNAT has taken place,
318 * we will need to fix up the destination Ethernet address.
319 * This is also true when SNAT takes place (for the reply direction).
321 * There are two cases to consider:
322 * 1. The packet was DNAT'ed to a device in the same bridge
323 * port group as it was received on. We can still bridge
325 * 2. The packet was DNAT'ed to a different device, either
326 * a non-bridged device or another bridge port group.
327 * The packet will need to be routed.
329 * The correct way of distinguishing between these two cases is to
330 * call ip_route_input() and to look at skb->dst->dev, which is
331 * changed to the destination device if ip_route_input() succeeds.
333 * Let's first consider the case that ip_route_input() succeeds:
335 * If the output device equals the logical bridge device the packet
336 * came in on, we can consider this bridging. The corresponding MAC
337 * address will be obtained in br_nf_pre_routing_finish_bridge.
338 * Otherwise, the packet is considered to be routed and we just
339 * change the destination MAC address so that the packet will
340 * later be passed up to the IP stack to be routed. For a redirected
341 * packet, ip_route_input() will give back the localhost as output device,
342 * which differs from the bridge device.
344 * Let's now consider the case that ip_route_input() fails:
346 * This can be because the destination address is martian, in which case
347 * the packet will be dropped.
348 * If IP forwarding is disabled, ip_route_input() will fail, while
349 * ip_route_output_key() can return success. The source
350 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
351 * thinks we're handling a locally generated packet and won't care
352 * if IP forwarding is enabled. If the output device equals the logical bridge
353 * device, we proceed as if ip_route_input() succeeded. If it differs from the
354 * logical bridge port or if ip_route_output_key() fails we drop the packet.
356 static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
358 struct net_device *dev = skb->dev;
359 struct iphdr *iph = ip_hdr(skb);
360 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
364 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
366 if (nf_bridge->pkt_otherhost) {
367 skb->pkt_type = PACKET_OTHERHOST;
368 nf_bridge->pkt_otherhost = false;
370 nf_bridge->in_prerouting = 0;
371 if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) {
372 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
373 struct in_device *in_dev = __in_dev_get_rcu(dev);
375 /* If err equals -EHOSTUNREACH the error is due to a
376 * martian destination or due to the fact that
377 * forwarding is disabled. For most martian packets,
378 * ip_route_output_key() will fail. It won't fail for 2 types of
379 * martian destinations: loopback destinations and destination
380 * 0.0.0.0. In both cases the packet will be dropped because the
381 * destination is the loopback device and not the bridge. */
382 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
385 rt = ip_route_output(net, iph->daddr, 0,
386 RT_TOS(iph->tos), 0);
388 /* - Bridged-and-DNAT'ed traffic doesn't
389 * require ip_forwarding. */
390 if (rt->dst.dev == dev) {
391 skb_dst_set(skb, &rt->dst);
400 if (skb_dst(skb)->dev == dev) {
402 skb->dev = nf_bridge->physindev;
403 nf_bridge_update_protocol(skb);
404 nf_bridge_push_encap_header(skb);
405 br_nf_hook_thresh(NF_BR_PRE_ROUTING,
406 net, sk, skb, skb->dev,
408 br_nf_pre_routing_finish_bridge);
411 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
412 skb->pkt_type = PACKET_HOST;
415 rt = bridge_parent_rtable(nf_bridge->physindev);
420 skb_dst_set_noref(skb, &rt->dst);
423 skb->dev = nf_bridge->physindev;
424 nf_bridge_update_protocol(skb);
425 nf_bridge_push_encap_header(skb);
426 br_nf_hook_thresh(NF_BR_PRE_ROUTING, net, sk, skb, skb->dev, NULL,
427 br_handle_frame_finish);
431 static struct net_device *brnf_get_logical_dev(struct sk_buff *skb,
432 const struct net_device *dev,
433 const struct net *net)
435 struct net_device *vlan, *br;
436 struct brnf_net *brnet = net_generic(net, brnf_net_id);
438 br = bridge_parent(dev);
440 if (brnet->pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
443 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
444 skb_vlan_tag_get(skb) & VLAN_VID_MASK);
446 return vlan ? vlan : br;
449 /* Some common code for IPv4/IPv6 */
450 struct net_device *setup_pre_routing(struct sk_buff *skb, const struct net *net)
452 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
454 if (skb->pkt_type == PACKET_OTHERHOST) {
455 skb->pkt_type = PACKET_HOST;
456 nf_bridge->pkt_otherhost = true;
459 nf_bridge->in_prerouting = 1;
460 nf_bridge->physindev = skb->dev;
461 skb->dev = brnf_get_logical_dev(skb, skb->dev, net);
463 if (skb->protocol == htons(ETH_P_8021Q))
464 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
465 else if (skb->protocol == htons(ETH_P_PPP_SES))
466 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
468 /* Must drop socket now because of tproxy. */
473 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
474 * Replicate the checks that IPv4 does on packet reception.
475 * Set skb->dev to the bridge device (i.e. parent of the
476 * receiving device) to make netfilter happy, the REDIRECT
477 * target in particular. Save the original destination IP
478 * address to be able to detect DNAT afterwards. */
479 static unsigned int br_nf_pre_routing(void *priv,
481 const struct nf_hook_state *state)
483 struct nf_bridge_info *nf_bridge;
484 struct net_bridge_port *p;
485 struct net_bridge *br;
486 __u32 len = nf_bridge_encap_header_len(skb);
487 struct brnf_net *brnet;
489 if (unlikely(!pskb_may_pull(skb, len)))
492 p = br_port_get_rcu(state->in);
497 brnet = net_generic(state->net, brnf_net_id);
498 if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
499 is_pppoe_ipv6(skb, state->net)) {
500 if (!brnet->call_ip6tables &&
501 !br_opt_get(br, BROPT_NF_CALL_IP6TABLES))
504 nf_bridge_pull_encap_header_rcsum(skb);
505 return br_nf_pre_routing_ipv6(priv, skb, state);
508 if (!brnet->call_iptables && !br_opt_get(br, BROPT_NF_CALL_IPTABLES))
511 if (!IS_IP(skb) && !is_vlan_ip(skb, state->net) &&
512 !is_pppoe_ip(skb, state->net))
515 nf_bridge_pull_encap_header_rcsum(skb);
517 if (br_validate_ipv4(state->net, skb))
520 if (!nf_bridge_alloc(skb))
522 if (!setup_pre_routing(skb, state->net))
525 nf_bridge = nf_bridge_info_get(skb);
526 nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
528 skb->protocol = htons(ETH_P_IP);
529 skb->transport_header = skb->network_header + ip_hdr(skb)->ihl * 4;
531 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->net, state->sk, skb,
533 br_nf_pre_routing_finish);
539 /* PF_BRIDGE/FORWARD *************************************************/
540 static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
542 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
543 struct net_device *in;
545 if (!IS_ARP(skb) && !is_vlan_arp(skb, net)) {
547 if (skb->protocol == htons(ETH_P_IP))
548 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
550 if (skb->protocol == htons(ETH_P_IPV6))
551 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
553 in = nf_bridge->physindev;
554 if (nf_bridge->pkt_otherhost) {
555 skb->pkt_type = PACKET_OTHERHOST;
556 nf_bridge->pkt_otherhost = false;
558 nf_bridge_update_protocol(skb);
560 in = *((struct net_device **)(skb->cb));
562 nf_bridge_push_encap_header(skb);
564 br_nf_hook_thresh(NF_BR_FORWARD, net, sk, skb, in, skb->dev,
570 /* This is the 'purely bridged' case. For IP, we pass the packet to
571 * netfilter with indev and outdev set to the bridge device,
572 * but we are still able to filter on the 'real' indev/outdev
573 * because of the physdev module. For ARP, indev and outdev are the
575 static unsigned int br_nf_forward_ip(void *priv,
577 const struct nf_hook_state *state)
579 struct nf_bridge_info *nf_bridge;
580 struct net_device *parent;
583 nf_bridge = nf_bridge_info_get(skb);
587 /* Need exclusive nf_bridge_info since we might have multiple
588 * different physoutdevs. */
589 if (!nf_bridge_unshare(skb))
592 nf_bridge = nf_bridge_info_get(skb);
596 parent = bridge_parent(state->out);
600 if (IS_IP(skb) || is_vlan_ip(skb, state->net) ||
601 is_pppoe_ip(skb, state->net))
603 else if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
604 is_pppoe_ipv6(skb, state->net))
609 nf_bridge_pull_encap_header(skb);
611 if (skb->pkt_type == PACKET_OTHERHOST) {
612 skb->pkt_type = PACKET_HOST;
613 nf_bridge->pkt_otherhost = true;
616 if (pf == NFPROTO_IPV4) {
617 if (br_validate_ipv4(state->net, skb))
619 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
622 if (pf == NFPROTO_IPV6) {
623 if (br_validate_ipv6(state->net, skb))
625 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
628 nf_bridge->physoutdev = skb->dev;
629 if (pf == NFPROTO_IPV4)
630 skb->protocol = htons(ETH_P_IP);
632 skb->protocol = htons(ETH_P_IPV6);
634 NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb,
635 brnf_get_logical_dev(skb, state->in, state->net),
636 parent, br_nf_forward_finish);
641 static unsigned int br_nf_forward_arp(void *priv,
643 const struct nf_hook_state *state)
645 struct net_bridge_port *p;
646 struct net_bridge *br;
647 struct net_device **d = (struct net_device **)(skb->cb);
648 struct brnf_net *brnet;
650 p = br_port_get_rcu(state->out);
655 brnet = net_generic(state->net, brnf_net_id);
656 if (!brnet->call_arptables && !br_opt_get(br, BROPT_NF_CALL_ARPTABLES))
660 if (!is_vlan_arp(skb, state->net))
662 nf_bridge_pull_encap_header(skb);
665 if (arp_hdr(skb)->ar_pln != 4) {
666 if (is_vlan_arp(skb, state->net))
667 nf_bridge_push_encap_header(skb);
671 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb,
672 state->in, state->out, br_nf_forward_finish);
677 static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
679 struct brnf_frag_data *data;
682 data = this_cpu_ptr(&brnf_frag_data_storage);
683 err = skb_cow_head(skb, data->size);
690 if (data->vlan_proto)
691 __vlan_hwaccel_put_tag(skb, data->vlan_proto, data->vlan_tci);
693 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
694 __skb_push(skb, data->encap_size);
696 nf_bridge_info_free(skb);
697 return br_dev_queue_push_xmit(net, sk, skb);
701 br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
702 int (*output)(struct net *, struct sock *, struct sk_buff *))
704 unsigned int mtu = ip_skb_dst_mtu(sk, skb);
705 struct iphdr *iph = ip_hdr(skb);
707 if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
708 (IPCB(skb)->frag_max_size &&
709 IPCB(skb)->frag_max_size > mtu))) {
710 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
715 return ip_do_fragment(net, sk, skb, output);
718 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
720 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
722 if (nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
723 return PPPOE_SES_HLEN;
727 static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
729 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
730 unsigned int mtu, mtu_reserved;
732 mtu_reserved = nf_bridge_mtu_reduction(skb);
735 if (nf_bridge->frag_max_size && nf_bridge->frag_max_size < mtu)
736 mtu = nf_bridge->frag_max_size;
738 if (skb_is_gso(skb) || skb->len + mtu_reserved <= mtu) {
739 nf_bridge_info_free(skb);
740 return br_dev_queue_push_xmit(net, sk, skb);
743 /* This is wrong! We should preserve the original fragment
744 * boundaries by preserving frag_list rather than refragmenting.
746 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) &&
747 skb->protocol == htons(ETH_P_IP)) {
748 struct brnf_frag_data *data;
750 if (br_validate_ipv4(net, skb))
753 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
755 nf_bridge_update_protocol(skb);
757 data = this_cpu_ptr(&brnf_frag_data_storage);
759 if (skb_vlan_tag_present(skb)) {
760 data->vlan_tci = skb->vlan_tci;
761 data->vlan_proto = skb->vlan_proto;
763 data->vlan_proto = 0;
766 data->encap_size = nf_bridge_encap_header_len(skb);
767 data->size = ETH_HLEN + data->encap_size;
769 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
772 return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit);
774 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) &&
775 skb->protocol == htons(ETH_P_IPV6)) {
776 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
777 struct brnf_frag_data *data;
779 if (br_validate_ipv6(net, skb))
782 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
784 nf_bridge_update_protocol(skb);
786 data = this_cpu_ptr(&brnf_frag_data_storage);
787 data->encap_size = nf_bridge_encap_header_len(skb);
788 data->size = ETH_HLEN + data->encap_size;
790 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
794 return v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit);
799 nf_bridge_info_free(skb);
800 return br_dev_queue_push_xmit(net, sk, skb);
806 /* PF_BRIDGE/POST_ROUTING ********************************************/
807 static unsigned int br_nf_post_routing(void *priv,
809 const struct nf_hook_state *state)
811 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
812 struct net_device *realoutdev = bridge_parent(skb->dev);
815 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
816 * on a bridge, but was delivered locally and is now being routed:
818 * POST_ROUTING was already invoked from the ip stack.
820 if (!nf_bridge || !nf_bridge->physoutdev)
826 if (IS_IP(skb) || is_vlan_ip(skb, state->net) ||
827 is_pppoe_ip(skb, state->net))
829 else if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
830 is_pppoe_ipv6(skb, state->net))
835 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
836 * about the value of skb->pkt_type. */
837 if (skb->pkt_type == PACKET_OTHERHOST) {
838 skb->pkt_type = PACKET_HOST;
839 nf_bridge->pkt_otherhost = true;
842 nf_bridge_pull_encap_header(skb);
843 if (pf == NFPROTO_IPV4)
844 skb->protocol = htons(ETH_P_IP);
846 skb->protocol = htons(ETH_P_IPV6);
848 NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb,
850 br_nf_dev_queue_xmit);
855 /* IP/SABOTAGE *****************************************************/
856 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
857 * for the second time. */
858 static unsigned int ip_sabotage_in(void *priv,
860 const struct nf_hook_state *state)
862 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
864 if (nf_bridge && !nf_bridge->in_prerouting &&
865 !netif_is_l3_master(skb->dev) &&
866 !netif_is_l3_slave(skb->dev)) {
867 state->okfn(state->net, state->sk, skb);
874 /* This is called when br_netfilter has called into iptables/netfilter,
875 * and DNAT has taken place on a bridge-forwarded packet.
877 * neigh->output has created a new MAC header, with local br0 MAC
880 * This restores the original MAC saddr of the bridged packet
881 * before invoking bridge forward logic to transmit the packet.
883 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
885 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
887 skb_pull(skb, ETH_HLEN);
888 nf_bridge->bridged_dnat = 0;
890 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
892 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
893 nf_bridge->neigh_header,
894 ETH_HLEN - ETH_ALEN);
895 skb->dev = nf_bridge->physindev;
897 nf_bridge->physoutdev = NULL;
898 br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
901 static int br_nf_dev_xmit(struct sk_buff *skb)
903 const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
905 if (nf_bridge && nf_bridge->bridged_dnat) {
906 br_nf_pre_routing_finish_bridge_slow(skb);
912 static const struct nf_br_ops br_ops = {
913 .br_dev_xmit_hook = br_nf_dev_xmit,
916 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
917 * br_dev_queue_push_xmit is called afterwards */
918 static const struct nf_hook_ops br_nf_ops[] = {
920 .hook = br_nf_pre_routing,
921 .pf = NFPROTO_BRIDGE,
922 .hooknum = NF_BR_PRE_ROUTING,
923 .priority = NF_BR_PRI_BRNF,
926 .hook = br_nf_forward_ip,
927 .pf = NFPROTO_BRIDGE,
928 .hooknum = NF_BR_FORWARD,
929 .priority = NF_BR_PRI_BRNF - 1,
932 .hook = br_nf_forward_arp,
933 .pf = NFPROTO_BRIDGE,
934 .hooknum = NF_BR_FORWARD,
935 .priority = NF_BR_PRI_BRNF,
938 .hook = br_nf_post_routing,
939 .pf = NFPROTO_BRIDGE,
940 .hooknum = NF_BR_POST_ROUTING,
941 .priority = NF_BR_PRI_LAST,
944 .hook = ip_sabotage_in,
946 .hooknum = NF_INET_PRE_ROUTING,
947 .priority = NF_IP_PRI_FIRST,
950 .hook = ip_sabotage_in,
952 .hooknum = NF_INET_PRE_ROUTING,
953 .priority = NF_IP6_PRI_FIRST,
957 static int brnf_device_event(struct notifier_block *unused, unsigned long event,
960 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
961 struct brnf_net *brnet;
965 if (event != NETDEV_REGISTER || !(dev->priv_flags & IFF_EBRIDGE))
971 brnet = net_generic(net, brnf_net_id);
975 ret = nf_register_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
979 brnet->enabled = true;
983 static struct notifier_block brnf_notifier __read_mostly = {
984 .notifier_call = brnf_device_event,
987 /* recursively invokes nf_hook_slow (again), skipping already-called
988 * hooks (< NF_BR_PRI_BRNF).
990 * Called with rcu read lock held.
992 int br_nf_hook_thresh(unsigned int hook, struct net *net,
993 struct sock *sk, struct sk_buff *skb,
994 struct net_device *indev,
995 struct net_device *outdev,
996 int (*okfn)(struct net *, struct sock *,
999 const struct nf_hook_entries *e;
1000 struct nf_hook_state state;
1001 struct nf_hook_ops **ops;
1005 e = rcu_dereference(net->nf.hooks_bridge[hook]);
1007 return okfn(net, sk, skb);
1009 ops = nf_hook_entries_get_hook_ops(e);
1010 for (i = 0; i < e->num_hook_entries &&
1011 ops[i]->priority <= NF_BR_PRI_BRNF; i++)
1014 nf_hook_state_init(&state, hook, NFPROTO_BRIDGE, indev, outdev,
1017 ret = nf_hook_slow(skb, &state, e, i);
1019 ret = okfn(net, sk, skb);
1024 #ifdef CONFIG_SYSCTL
1026 int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
1027 void __user *buffer, size_t *lenp, loff_t *ppos)
1031 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1033 if (write && *(int *)(ctl->data))
1034 *(int *)(ctl->data) = 1;
1038 static struct ctl_table brnf_table[] = {
1040 .procname = "bridge-nf-call-arptables",
1041 .maxlen = sizeof(int),
1043 .proc_handler = brnf_sysctl_call_tables,
1046 .procname = "bridge-nf-call-iptables",
1047 .maxlen = sizeof(int),
1049 .proc_handler = brnf_sysctl_call_tables,
1052 .procname = "bridge-nf-call-ip6tables",
1053 .maxlen = sizeof(int),
1055 .proc_handler = brnf_sysctl_call_tables,
1058 .procname = "bridge-nf-filter-vlan-tagged",
1059 .maxlen = sizeof(int),
1061 .proc_handler = brnf_sysctl_call_tables,
1064 .procname = "bridge-nf-filter-pppoe-tagged",
1065 .maxlen = sizeof(int),
1067 .proc_handler = brnf_sysctl_call_tables,
1070 .procname = "bridge-nf-pass-vlan-input-dev",
1071 .maxlen = sizeof(int),
1073 .proc_handler = brnf_sysctl_call_tables,
1078 static inline void br_netfilter_sysctl_default(struct brnf_net *brnf)
1080 brnf->call_iptables = 1;
1081 brnf->call_ip6tables = 1;
1082 brnf->call_arptables = 1;
1083 brnf->filter_vlan_tagged = 0;
1084 brnf->filter_pppoe_tagged = 0;
1085 brnf->pass_vlan_indev = 0;
1088 static int br_netfilter_sysctl_init_net(struct net *net)
1090 struct ctl_table *table = brnf_table;
1091 struct brnf_net *brnet;
1093 if (!net_eq(net, &init_net)) {
1094 table = kmemdup(table, sizeof(brnf_table), GFP_KERNEL);
1099 brnet = net_generic(net, brnf_net_id);
1100 table[0].data = &brnet->call_arptables;
1101 table[1].data = &brnet->call_iptables;
1102 table[2].data = &brnet->call_ip6tables;
1103 table[3].data = &brnet->filter_vlan_tagged;
1104 table[4].data = &brnet->filter_pppoe_tagged;
1105 table[5].data = &brnet->pass_vlan_indev;
1107 br_netfilter_sysctl_default(brnet);
1109 brnet->ctl_hdr = register_net_sysctl(net, "net/bridge", table);
1110 if (!brnet->ctl_hdr) {
1111 if (!net_eq(net, &init_net))
1120 static void br_netfilter_sysctl_exit_net(struct net *net,
1121 struct brnf_net *brnet)
1123 struct ctl_table *table = brnet->ctl_hdr->ctl_table_arg;
1125 unregister_net_sysctl_table(brnet->ctl_hdr);
1126 if (!net_eq(net, &init_net))
1130 static int __net_init brnf_init_net(struct net *net)
1132 return br_netfilter_sysctl_init_net(net);
1136 static void __net_exit brnf_exit_net(struct net *net)
1138 struct brnf_net *brnet;
1140 brnet = net_generic(net, brnf_net_id);
1141 if (brnet->enabled) {
1142 nf_unregister_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
1143 brnet->enabled = false;
1146 #ifdef CONFIG_SYSCTL
1147 br_netfilter_sysctl_exit_net(net, brnet);
1151 static struct pernet_operations brnf_net_ops __read_mostly = {
1152 #ifdef CONFIG_SYSCTL
1153 .init = brnf_init_net,
1155 .exit = brnf_exit_net,
1157 .size = sizeof(struct brnf_net),
1160 static int __init br_netfilter_init(void)
1164 ret = register_pernet_subsys(&brnf_net_ops);
1168 ret = register_netdevice_notifier(&brnf_notifier);
1170 unregister_pernet_subsys(&brnf_net_ops);
1174 RCU_INIT_POINTER(nf_br_ops, &br_ops);
1175 printk(KERN_NOTICE "Bridge firewalling registered\n");
1179 static void __exit br_netfilter_fini(void)
1181 RCU_INIT_POINTER(nf_br_ops, NULL);
1182 unregister_netdevice_notifier(&brnf_notifier);
1183 unregister_pernet_subsys(&brnf_net_ops);
1186 module_init(br_netfilter_init);
1187 module_exit(br_netfilter_fini);
1189 MODULE_LICENSE("GPL");
1190 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1191 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1192 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");