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/rculist.h>
34 #include <linux/inetdevice.h>
38 #include <net/addrconf.h>
39 #include <net/route.h>
40 #include <net/netfilter/br_netfilter.h>
41 #include <net/netns/generic.h>
43 #include <linux/uaccess.h>
44 #include "br_private.h"
46 #include <linux/sysctl.h>
49 static unsigned int brnf_net_id __read_mostly;
56 static struct ctl_table_header *brnf_sysctl_header;
57 static int brnf_call_iptables __read_mostly = 1;
58 static int brnf_call_ip6tables __read_mostly = 1;
59 static int brnf_call_arptables __read_mostly = 1;
60 static int brnf_filter_vlan_tagged __read_mostly;
61 static int brnf_filter_pppoe_tagged __read_mostly;
62 static int brnf_pass_vlan_indev __read_mostly;
64 #define brnf_call_iptables 1
65 #define brnf_call_ip6tables 1
66 #define brnf_call_arptables 1
67 #define brnf_filter_vlan_tagged 0
68 #define brnf_filter_pppoe_tagged 0
69 #define brnf_pass_vlan_indev 0
73 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
75 #define IS_IPV6(skb) \
76 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
79 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
81 static inline __be16 vlan_proto(const struct sk_buff *skb)
83 if (skb_vlan_tag_present(skb))
85 else if (skb->protocol == htons(ETH_P_8021Q))
86 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
91 #define IS_VLAN_IP(skb) \
92 (vlan_proto(skb) == htons(ETH_P_IP) && \
93 brnf_filter_vlan_tagged)
95 #define IS_VLAN_IPV6(skb) \
96 (vlan_proto(skb) == htons(ETH_P_IPV6) && \
97 brnf_filter_vlan_tagged)
99 #define IS_VLAN_ARP(skb) \
100 (vlan_proto(skb) == htons(ETH_P_ARP) && \
101 brnf_filter_vlan_tagged)
103 static inline __be16 pppoe_proto(const struct sk_buff *skb)
105 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
106 sizeof(struct pppoe_hdr)));
109 #define IS_PPPOE_IP(skb) \
110 (skb->protocol == htons(ETH_P_PPP_SES) && \
111 pppoe_proto(skb) == htons(PPP_IP) && \
112 brnf_filter_pppoe_tagged)
114 #define IS_PPPOE_IPV6(skb) \
115 (skb->protocol == htons(ETH_P_PPP_SES) && \
116 pppoe_proto(skb) == htons(PPP_IPV6) && \
117 brnf_filter_pppoe_tagged)
119 /* largest possible L2 header, see br_nf_dev_queue_xmit() */
120 #define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN)
122 struct brnf_frag_data {
123 char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH];
130 static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage);
132 static void nf_bridge_info_free(struct sk_buff *skb)
134 if (skb->nf_bridge) {
135 nf_bridge_put(skb->nf_bridge);
136 skb->nf_bridge = NULL;
140 static inline struct net_device *bridge_parent(const struct net_device *dev)
142 struct net_bridge_port *port;
144 port = br_port_get_rcu(dev);
145 return port ? port->br->dev : NULL;
148 static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
150 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
152 if (refcount_read(&nf_bridge->use) > 1) {
153 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
156 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
157 refcount_set(&tmp->use, 1);
159 nf_bridge_put(nf_bridge);
165 unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
167 switch (skb->protocol) {
168 case __cpu_to_be16(ETH_P_8021Q):
170 case __cpu_to_be16(ETH_P_PPP_SES):
171 return PPPOE_SES_HLEN;
177 static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
179 unsigned int len = nf_bridge_encap_header_len(skb);
182 skb->network_header += len;
185 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
187 unsigned int len = nf_bridge_encap_header_len(skb);
189 skb_pull_rcsum(skb, len);
190 skb->network_header += len;
193 /* When handing a packet over to the IP layer
194 * check whether we have a skb that is in the
198 static int br_validate_ipv4(struct net *net, struct sk_buff *skb)
200 const struct iphdr *iph;
203 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
208 /* Basic sanity checks */
209 if (iph->ihl < 5 || iph->version != 4)
212 if (!pskb_may_pull(skb, iph->ihl*4))
216 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
219 len = ntohs(iph->tot_len);
220 if (skb->len < len) {
221 __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS);
223 } else if (len < (iph->ihl*4))
226 if (pskb_trim_rcsum(skb, len)) {
227 __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
231 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
232 /* We should really parse IP options here but until
233 * somebody who actually uses IP options complains to
234 * us we'll just silently ignore the options because
240 __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
245 void nf_bridge_update_protocol(struct sk_buff *skb)
247 switch (skb->nf_bridge->orig_proto) {
248 case BRNF_PROTO_8021Q:
249 skb->protocol = htons(ETH_P_8021Q);
251 case BRNF_PROTO_PPPOE:
252 skb->protocol = htons(ETH_P_PPP_SES);
254 case BRNF_PROTO_UNCHANGED:
259 /* Obtain the correct destination MAC address, while preserving the original
260 * source MAC address. If we already know this address, we just copy it. If we
261 * don't, we use the neighbour framework to find out. In both cases, we make
262 * sure that br_handle_frame_finish() is called afterwards.
264 int br_nf_pre_routing_finish_bridge(struct net *net, struct sock *sk, struct sk_buff *skb)
266 struct neighbour *neigh;
267 struct dst_entry *dst;
269 skb->dev = bridge_parent(skb->dev);
273 neigh = dst_neigh_lookup_skb(dst, skb);
275 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
278 if ((neigh->nud_state & NUD_CONNECTED) && neigh->hh.hh_len) {
279 neigh_hh_bridge(&neigh->hh, skb);
280 skb->dev = nf_bridge->physindev;
281 ret = br_handle_frame_finish(net, sk, skb);
283 /* the neighbour function below overwrites the complete
284 * MAC header, so we save the Ethernet source address and
287 skb_copy_from_linear_data_offset(skb,
288 -(ETH_HLEN-ETH_ALEN),
289 nf_bridge->neigh_header,
291 /* tell br_dev_xmit to continue with forwarding */
292 nf_bridge->bridged_dnat = 1;
293 /* FIXME Need to refragment */
294 ret = neigh->output(neigh, skb);
296 neigh_release(neigh);
305 br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb,
306 const struct nf_bridge_info *nf_bridge)
308 return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
311 /* This requires some explaining. If DNAT has taken place,
312 * we will need to fix up the destination Ethernet address.
313 * This is also true when SNAT takes place (for the reply direction).
315 * There are two cases to consider:
316 * 1. The packet was DNAT'ed to a device in the same bridge
317 * port group as it was received on. We can still bridge
319 * 2. The packet was DNAT'ed to a different device, either
320 * a non-bridged device or another bridge port group.
321 * The packet will need to be routed.
323 * The correct way of distinguishing between these two cases is to
324 * call ip_route_input() and to look at skb->dst->dev, which is
325 * changed to the destination device if ip_route_input() succeeds.
327 * Let's first consider the case that ip_route_input() succeeds:
329 * If the output device equals the logical bridge device the packet
330 * came in on, we can consider this bridging. The corresponding MAC
331 * address will be obtained in br_nf_pre_routing_finish_bridge.
332 * Otherwise, the packet is considered to be routed and we just
333 * change the destination MAC address so that the packet will
334 * later be passed up to the IP stack to be routed. For a redirected
335 * packet, ip_route_input() will give back the localhost as output device,
336 * which differs from the bridge device.
338 * Let's now consider the case that ip_route_input() fails:
340 * This can be because the destination address is martian, in which case
341 * the packet will be dropped.
342 * If IP forwarding is disabled, ip_route_input() will fail, while
343 * ip_route_output_key() can return success. The source
344 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
345 * thinks we're handling a locally generated packet and won't care
346 * if IP forwarding is enabled. If the output device equals the logical bridge
347 * device, we proceed as if ip_route_input() succeeded. If it differs from the
348 * logical bridge port or if ip_route_output_key() fails we drop the packet.
350 static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
352 struct net_device *dev = skb->dev;
353 struct iphdr *iph = ip_hdr(skb);
354 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
358 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
360 if (nf_bridge->pkt_otherhost) {
361 skb->pkt_type = PACKET_OTHERHOST;
362 nf_bridge->pkt_otherhost = false;
364 nf_bridge->in_prerouting = 0;
365 if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) {
366 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
367 struct in_device *in_dev = __in_dev_get_rcu(dev);
369 /* If err equals -EHOSTUNREACH the error is due to a
370 * martian destination or due to the fact that
371 * forwarding is disabled. For most martian packets,
372 * ip_route_output_key() will fail. It won't fail for 2 types of
373 * martian destinations: loopback destinations and destination
374 * 0.0.0.0. In both cases the packet will be dropped because the
375 * destination is the loopback device and not the bridge. */
376 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
379 rt = ip_route_output(net, iph->daddr, 0,
380 RT_TOS(iph->tos), 0);
382 /* - Bridged-and-DNAT'ed traffic doesn't
383 * require ip_forwarding. */
384 if (rt->dst.dev == dev) {
386 skb_dst_set(skb, &rt->dst);
395 if (skb_dst(skb)->dev == dev) {
397 skb->dev = nf_bridge->physindev;
398 nf_bridge_update_protocol(skb);
399 nf_bridge_push_encap_header(skb);
400 br_nf_hook_thresh(NF_BR_PRE_ROUTING,
401 net, sk, skb, skb->dev,
403 br_nf_pre_routing_finish_bridge);
406 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
407 skb->pkt_type = PACKET_HOST;
410 rt = bridge_parent_rtable(nf_bridge->physindev);
416 skb_dst_set_noref(skb, &rt->dst);
419 skb->dev = nf_bridge->physindev;
420 nf_bridge_update_protocol(skb);
421 nf_bridge_push_encap_header(skb);
422 br_nf_hook_thresh(NF_BR_PRE_ROUTING, net, sk, skb, skb->dev, NULL,
423 br_handle_frame_finish);
427 static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
429 struct net_device *vlan, *br;
431 br = bridge_parent(dev);
432 if (brnf_pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
435 vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
436 skb_vlan_tag_get(skb) & VLAN_VID_MASK);
438 return vlan ? vlan : br;
441 /* Some common code for IPv4/IPv6 */
442 struct net_device *setup_pre_routing(struct sk_buff *skb)
444 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
446 if (skb->pkt_type == PACKET_OTHERHOST) {
447 skb->pkt_type = PACKET_HOST;
448 nf_bridge->pkt_otherhost = true;
451 nf_bridge->in_prerouting = 1;
452 nf_bridge->physindev = skb->dev;
453 skb->dev = brnf_get_logical_dev(skb, skb->dev);
455 if (skb->protocol == htons(ETH_P_8021Q))
456 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
457 else if (skb->protocol == htons(ETH_P_PPP_SES))
458 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
460 /* Must drop socket now because of tproxy. */
465 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
466 * Replicate the checks that IPv4 does on packet reception.
467 * Set skb->dev to the bridge device (i.e. parent of the
468 * receiving device) to make netfilter happy, the REDIRECT
469 * target in particular. Save the original destination IP
470 * address to be able to detect DNAT afterwards. */
471 static unsigned int br_nf_pre_routing(void *priv,
473 const struct nf_hook_state *state)
475 struct nf_bridge_info *nf_bridge;
476 struct net_bridge_port *p;
477 struct net_bridge *br;
478 __u32 len = nf_bridge_encap_header_len(skb);
480 if (unlikely(!pskb_may_pull(skb, len)))
483 p = br_port_get_rcu(state->in);
488 if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
489 if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
492 nf_bridge_pull_encap_header_rcsum(skb);
493 return br_nf_pre_routing_ipv6(priv, skb, state);
496 if (!brnf_call_iptables && !br->nf_call_iptables)
499 if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
502 nf_bridge_pull_encap_header_rcsum(skb);
504 if (br_validate_ipv4(state->net, skb))
507 nf_bridge_put(skb->nf_bridge);
508 if (!nf_bridge_alloc(skb))
510 if (!setup_pre_routing(skb))
513 nf_bridge = nf_bridge_info_get(skb);
514 nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
516 skb->protocol = htons(ETH_P_IP);
517 skb->transport_header = skb->network_header + ip_hdr(skb)->ihl * 4;
519 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->net, state->sk, skb,
521 br_nf_pre_routing_finish);
527 /* PF_BRIDGE/FORWARD *************************************************/
528 static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
530 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
531 struct net_device *in;
533 if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
535 if (skb->protocol == htons(ETH_P_IP))
536 nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
538 if (skb->protocol == htons(ETH_P_IPV6))
539 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
541 in = nf_bridge->physindev;
542 if (nf_bridge->pkt_otherhost) {
543 skb->pkt_type = PACKET_OTHERHOST;
544 nf_bridge->pkt_otherhost = false;
546 nf_bridge_update_protocol(skb);
548 in = *((struct net_device **)(skb->cb));
550 nf_bridge_push_encap_header(skb);
552 br_nf_hook_thresh(NF_BR_FORWARD, net, sk, skb, in, skb->dev,
558 /* This is the 'purely bridged' case. For IP, we pass the packet to
559 * netfilter with indev and outdev set to the bridge device,
560 * but we are still able to filter on the 'real' indev/outdev
561 * because of the physdev module. For ARP, indev and outdev are the
563 static unsigned int br_nf_forward_ip(void *priv,
565 const struct nf_hook_state *state)
567 struct nf_bridge_info *nf_bridge;
568 struct net_device *parent;
574 /* Need exclusive nf_bridge_info since we might have multiple
575 * different physoutdevs. */
576 if (!nf_bridge_unshare(skb))
579 nf_bridge = nf_bridge_info_get(skb);
583 parent = bridge_parent(state->out);
587 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
589 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
594 nf_bridge_pull_encap_header(skb);
596 if (skb->pkt_type == PACKET_OTHERHOST) {
597 skb->pkt_type = PACKET_HOST;
598 nf_bridge->pkt_otherhost = true;
601 if (pf == NFPROTO_IPV4) {
602 if (br_validate_ipv4(state->net, skb))
604 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
607 if (pf == NFPROTO_IPV6) {
608 if (br_validate_ipv6(state->net, skb))
610 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
613 nf_bridge->physoutdev = skb->dev;
614 if (pf == NFPROTO_IPV4)
615 skb->protocol = htons(ETH_P_IP);
617 skb->protocol = htons(ETH_P_IPV6);
619 NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb,
620 brnf_get_logical_dev(skb, state->in),
621 parent, br_nf_forward_finish);
626 static unsigned int br_nf_forward_arp(void *priv,
628 const struct nf_hook_state *state)
630 struct net_bridge_port *p;
631 struct net_bridge *br;
632 struct net_device **d = (struct net_device **)(skb->cb);
634 p = br_port_get_rcu(state->out);
639 if (!brnf_call_arptables && !br->nf_call_arptables)
643 if (!IS_VLAN_ARP(skb))
645 nf_bridge_pull_encap_header(skb);
648 if (unlikely(!pskb_may_pull(skb, sizeof(struct arphdr))))
651 if (arp_hdr(skb)->ar_pln != 4) {
652 if (IS_VLAN_ARP(skb))
653 nf_bridge_push_encap_header(skb);
657 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb,
658 state->in, state->out, br_nf_forward_finish);
663 static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
665 struct brnf_frag_data *data;
668 data = this_cpu_ptr(&brnf_frag_data_storage);
669 err = skb_cow_head(skb, data->size);
676 if (data->vlan_tci) {
677 skb->vlan_tci = data->vlan_tci;
678 skb->vlan_proto = data->vlan_proto;
681 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
682 __skb_push(skb, data->encap_size);
684 nf_bridge_info_free(skb);
685 return br_dev_queue_push_xmit(net, sk, skb);
689 br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
690 int (*output)(struct net *, struct sock *, struct sk_buff *))
692 unsigned int mtu = ip_skb_dst_mtu(sk, skb);
693 struct iphdr *iph = ip_hdr(skb);
695 if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
696 (IPCB(skb)->frag_max_size &&
697 IPCB(skb)->frag_max_size > mtu))) {
698 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
703 return ip_do_fragment(net, sk, skb, output);
706 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
708 if (skb->nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
709 return PPPOE_SES_HLEN;
713 static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
715 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
716 unsigned int mtu, mtu_reserved;
718 mtu_reserved = nf_bridge_mtu_reduction(skb);
721 if (nf_bridge->pkt_otherhost) {
722 skb->pkt_type = PACKET_OTHERHOST;
723 nf_bridge->pkt_otherhost = false;
726 if (nf_bridge->frag_max_size && nf_bridge->frag_max_size < mtu)
727 mtu = nf_bridge->frag_max_size;
729 nf_bridge_update_protocol(skb);
730 nf_bridge_push_encap_header(skb);
732 if (skb_is_gso(skb) || skb->len + mtu_reserved <= mtu) {
733 nf_bridge_info_free(skb);
734 return br_dev_queue_push_xmit(net, sk, skb);
737 /* This is wrong! We should preserve the original fragment
738 * boundaries by preserving frag_list rather than refragmenting.
740 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) &&
741 skb->protocol == htons(ETH_P_IP)) {
742 struct brnf_frag_data *data;
744 if (br_validate_ipv4(net, skb))
747 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
749 data = this_cpu_ptr(&brnf_frag_data_storage);
751 data->vlan_tci = skb->vlan_tci;
752 data->vlan_proto = skb->vlan_proto;
753 data->encap_size = nf_bridge_encap_header_len(skb);
754 data->size = ETH_HLEN + data->encap_size;
756 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
759 return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit);
761 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) &&
762 skb->protocol == htons(ETH_P_IPV6)) {
763 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
764 struct brnf_frag_data *data;
766 if (br_validate_ipv6(net, skb))
769 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
771 data = this_cpu_ptr(&brnf_frag_data_storage);
772 data->encap_size = nf_bridge_encap_header_len(skb);
773 data->size = ETH_HLEN + data->encap_size;
775 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
779 return v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit);
784 nf_bridge_info_free(skb);
785 return br_dev_queue_push_xmit(net, sk, skb);
791 /* PF_BRIDGE/POST_ROUTING ********************************************/
792 static unsigned int br_nf_post_routing(void *priv,
794 const struct nf_hook_state *state)
796 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
797 struct net_device *realoutdev = bridge_parent(skb->dev);
800 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
801 * on a bridge, but was delivered locally and is now being routed:
803 * POST_ROUTING was already invoked from the ip stack.
805 if (!nf_bridge || !nf_bridge->physoutdev)
811 if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
813 else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
818 if (skb->pkt_type == PACKET_OTHERHOST) {
819 skb->pkt_type = PACKET_HOST;
820 nf_bridge->pkt_otherhost = true;
823 nf_bridge_pull_encap_header(skb);
824 if (pf == NFPROTO_IPV4)
825 skb->protocol = htons(ETH_P_IP);
827 skb->protocol = htons(ETH_P_IPV6);
829 NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb,
831 br_nf_dev_queue_xmit);
836 /* IP/SABOTAGE *****************************************************/
837 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
838 * for the second time. */
839 static unsigned int ip_sabotage_in(void *priv,
841 const struct nf_hook_state *state)
843 if (skb->nf_bridge && !skb->nf_bridge->in_prerouting &&
844 !netif_is_l3_master(skb->dev)) {
845 state->okfn(state->net, state->sk, skb);
852 /* This is called when br_netfilter has called into iptables/netfilter,
853 * and DNAT has taken place on a bridge-forwarded packet.
855 * neigh->output has created a new MAC header, with local br0 MAC
858 * This restores the original MAC saddr of the bridged packet
859 * before invoking bridge forward logic to transmit the packet.
861 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
863 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
865 skb_pull(skb, ETH_HLEN);
866 nf_bridge->bridged_dnat = 0;
868 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
870 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
871 nf_bridge->neigh_header,
872 ETH_HLEN - ETH_ALEN);
873 skb->dev = nf_bridge->physindev;
875 nf_bridge->physoutdev = NULL;
876 br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
879 static int br_nf_dev_xmit(struct sk_buff *skb)
881 if (skb->nf_bridge && skb->nf_bridge->bridged_dnat) {
882 br_nf_pre_routing_finish_bridge_slow(skb);
888 static const struct nf_br_ops br_ops = {
889 .br_dev_xmit_hook = br_nf_dev_xmit,
892 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
893 * br_dev_queue_push_xmit is called afterwards */
894 static const struct nf_hook_ops br_nf_ops[] = {
896 .hook = br_nf_pre_routing,
897 .pf = NFPROTO_BRIDGE,
898 .hooknum = NF_BR_PRE_ROUTING,
899 .priority = NF_BR_PRI_BRNF,
902 .hook = br_nf_forward_ip,
903 .pf = NFPROTO_BRIDGE,
904 .hooknum = NF_BR_FORWARD,
905 .priority = NF_BR_PRI_BRNF - 1,
908 .hook = br_nf_forward_arp,
909 .pf = NFPROTO_BRIDGE,
910 .hooknum = NF_BR_FORWARD,
911 .priority = NF_BR_PRI_BRNF,
914 .hook = br_nf_post_routing,
915 .pf = NFPROTO_BRIDGE,
916 .hooknum = NF_BR_POST_ROUTING,
917 .priority = NF_BR_PRI_LAST,
920 .hook = ip_sabotage_in,
922 .hooknum = NF_INET_PRE_ROUTING,
923 .priority = NF_IP_PRI_FIRST,
926 .hook = ip_sabotage_in,
928 .hooknum = NF_INET_PRE_ROUTING,
929 .priority = NF_IP6_PRI_FIRST,
933 static int brnf_device_event(struct notifier_block *unused, unsigned long event,
936 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
937 struct brnf_net *brnet;
941 if (event != NETDEV_REGISTER || !(dev->priv_flags & IFF_EBRIDGE))
947 brnet = net_generic(net, brnf_net_id);
951 ret = nf_register_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
955 brnet->enabled = true;
959 static void __net_exit brnf_exit_net(struct net *net)
961 struct brnf_net *brnet = net_generic(net, brnf_net_id);
966 nf_unregister_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
967 brnet->enabled = false;
970 static struct pernet_operations brnf_net_ops __read_mostly = {
971 .exit = brnf_exit_net,
973 .size = sizeof(struct brnf_net),
976 static struct notifier_block brnf_notifier __read_mostly = {
977 .notifier_call = brnf_device_event,
980 /* recursively invokes nf_hook_slow (again), skipping already-called
981 * hooks (< NF_BR_PRI_BRNF).
983 * Called with rcu read lock held.
985 int br_nf_hook_thresh(unsigned int hook, struct net *net,
986 struct sock *sk, struct sk_buff *skb,
987 struct net_device *indev,
988 struct net_device *outdev,
989 int (*okfn)(struct net *, struct sock *,
992 const struct nf_hook_entries *e;
993 struct nf_hook_state state;
994 struct nf_hook_ops **ops;
998 e = rcu_dereference(net->nf.hooks[NFPROTO_BRIDGE][hook]);
1000 return okfn(net, sk, skb);
1002 ops = nf_hook_entries_get_hook_ops(e);
1003 for (i = 0; i < e->num_hook_entries; i++) {
1004 /* These hooks have already been called */
1005 if (ops[i]->priority < NF_BR_PRI_BRNF)
1008 /* These hooks have not been called yet, run them. */
1009 if (ops[i]->priority > NF_BR_PRI_BRNF)
1012 /* take a closer look at NF_BR_PRI_BRNF. */
1013 if (ops[i]->hook == br_nf_pre_routing) {
1014 /* This hook diverted the skb to this function,
1015 * hooks after this have not been run yet.
1022 nf_hook_state_init(&state, hook, NFPROTO_BRIDGE, indev, outdev,
1025 ret = nf_hook_slow(skb, &state, e, i);
1027 ret = okfn(net, sk, skb);
1032 #ifdef CONFIG_SYSCTL
1034 int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
1035 void __user *buffer, size_t *lenp, loff_t *ppos)
1039 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1041 if (write && *(int *)(ctl->data))
1042 *(int *)(ctl->data) = 1;
1046 static struct ctl_table brnf_table[] = {
1048 .procname = "bridge-nf-call-arptables",
1049 .data = &brnf_call_arptables,
1050 .maxlen = sizeof(int),
1052 .proc_handler = brnf_sysctl_call_tables,
1055 .procname = "bridge-nf-call-iptables",
1056 .data = &brnf_call_iptables,
1057 .maxlen = sizeof(int),
1059 .proc_handler = brnf_sysctl_call_tables,
1062 .procname = "bridge-nf-call-ip6tables",
1063 .data = &brnf_call_ip6tables,
1064 .maxlen = sizeof(int),
1066 .proc_handler = brnf_sysctl_call_tables,
1069 .procname = "bridge-nf-filter-vlan-tagged",
1070 .data = &brnf_filter_vlan_tagged,
1071 .maxlen = sizeof(int),
1073 .proc_handler = brnf_sysctl_call_tables,
1076 .procname = "bridge-nf-filter-pppoe-tagged",
1077 .data = &brnf_filter_pppoe_tagged,
1078 .maxlen = sizeof(int),
1080 .proc_handler = brnf_sysctl_call_tables,
1083 .procname = "bridge-nf-pass-vlan-input-dev",
1084 .data = &brnf_pass_vlan_indev,
1085 .maxlen = sizeof(int),
1087 .proc_handler = brnf_sysctl_call_tables,
1093 static int __init br_netfilter_init(void)
1097 ret = register_pernet_subsys(&brnf_net_ops);
1101 ret = register_netdevice_notifier(&brnf_notifier);
1103 unregister_pernet_subsys(&brnf_net_ops);
1107 #ifdef CONFIG_SYSCTL
1108 brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
1109 if (brnf_sysctl_header == NULL) {
1111 "br_netfilter: can't register to sysctl.\n");
1112 unregister_netdevice_notifier(&brnf_notifier);
1113 unregister_pernet_subsys(&brnf_net_ops);
1117 RCU_INIT_POINTER(nf_br_ops, &br_ops);
1118 printk(KERN_NOTICE "Bridge firewalling registered\n");
1122 static void __exit br_netfilter_fini(void)
1124 RCU_INIT_POINTER(nf_br_ops, NULL);
1125 unregister_netdevice_notifier(&brnf_notifier);
1126 unregister_pernet_subsys(&brnf_net_ops);
1127 #ifdef CONFIG_SYSCTL
1128 unregister_net_sysctl_table(brnf_sysctl_header);
1132 module_init(br_netfilter_init);
1133 module_exit(br_netfilter_fini);
1135 MODULE_LICENSE("GPL");
1136 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1137 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1138 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");