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
];
123 static DEFINE_PER_CPU(struct brnf_frag_data
, brnf_frag_data_storage
);
126 static void nf_bridge_info_free(struct sk_buff
*skb
)
128 if (skb
->nf_bridge
) {
129 nf_bridge_put(skb
->nf_bridge
);
130 skb
->nf_bridge
= NULL
;
134 static inline struct net_device
*bridge_parent(const struct net_device
*dev
)
136 struct net_bridge_port
*port
;
138 port
= br_port_get_rcu(dev
);
139 return port
? port
->br
->dev
: NULL
;
142 static inline struct nf_bridge_info
*nf_bridge_unshare(struct sk_buff
*skb
)
144 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
146 if (atomic_read(&nf_bridge
->use
) > 1) {
147 struct nf_bridge_info
*tmp
= nf_bridge_alloc(skb
);
150 memcpy(tmp
, nf_bridge
, sizeof(struct nf_bridge_info
));
151 atomic_set(&tmp
->use
, 1);
153 nf_bridge_put(nf_bridge
);
159 unsigned int nf_bridge_encap_header_len(const struct sk_buff
*skb
)
161 switch (skb
->protocol
) {
162 case __cpu_to_be16(ETH_P_8021Q
):
164 case __cpu_to_be16(ETH_P_PPP_SES
):
165 return PPPOE_SES_HLEN
;
171 static inline void nf_bridge_pull_encap_header(struct sk_buff
*skb
)
173 unsigned int len
= nf_bridge_encap_header_len(skb
);
176 skb
->network_header
+= len
;
179 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff
*skb
)
181 unsigned int len
= nf_bridge_encap_header_len(skb
);
183 skb_pull_rcsum(skb
, len
);
184 skb
->network_header
+= len
;
187 /* When handing a packet over to the IP layer
188 * check whether we have a skb that is in the
192 static int br_validate_ipv4(struct sk_buff
*skb
)
194 const struct iphdr
*iph
;
195 struct net_device
*dev
= skb
->dev
;
198 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
203 /* Basic sanity checks */
204 if (iph
->ihl
< 5 || iph
->version
!= 4)
207 if (!pskb_may_pull(skb
, iph
->ihl
*4))
211 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
214 len
= ntohs(iph
->tot_len
);
215 if (skb
->len
< len
) {
216 IP_INC_STATS_BH(dev_net(dev
), IPSTATS_MIB_INTRUNCATEDPKTS
);
218 } else if (len
< (iph
->ihl
*4))
221 if (pskb_trim_rcsum(skb
, len
)) {
222 IP_INC_STATS_BH(dev_net(dev
), IPSTATS_MIB_INDISCARDS
);
226 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
227 /* We should really parse IP options here but until
228 * somebody who actually uses IP options complains to
229 * us we'll just silently ignore the options because
235 IP_INC_STATS_BH(dev_net(dev
), IPSTATS_MIB_INHDRERRORS
);
240 void nf_bridge_update_protocol(struct sk_buff
*skb
)
242 switch (skb
->nf_bridge
->orig_proto
) {
243 case BRNF_PROTO_8021Q
:
244 skb
->protocol
= htons(ETH_P_8021Q
);
246 case BRNF_PROTO_PPPOE
:
247 skb
->protocol
= htons(ETH_P_PPP_SES
);
249 case BRNF_PROTO_UNCHANGED
:
254 /* Obtain the correct destination MAC address, while preserving the original
255 * source MAC address. If we already know this address, we just copy it. If we
256 * don't, we use the neighbour framework to find out. In both cases, we make
257 * sure that br_handle_frame_finish() is called afterwards.
259 int br_nf_pre_routing_finish_bridge(struct sock
*sk
, struct sk_buff
*skb
)
261 struct neighbour
*neigh
;
262 struct dst_entry
*dst
;
264 skb
->dev
= bridge_parent(skb
->dev
);
268 neigh
= dst_neigh_lookup_skb(dst
, skb
);
270 struct nf_bridge_info
*nf_bridge
= nf_bridge_info_get(skb
);
273 if (neigh
->hh
.hh_len
) {
274 neigh_hh_bridge(&neigh
->hh
, skb
);
275 skb
->dev
= nf_bridge
->physindev
;
276 ret
= br_handle_frame_finish(sk
, skb
);
278 /* the neighbour function below overwrites the complete
279 * MAC header, so we save the Ethernet source address and
282 skb_copy_from_linear_data_offset(skb
,
283 -(ETH_HLEN
-ETH_ALEN
),
284 nf_bridge
->neigh_header
,
286 /* tell br_dev_xmit to continue with forwarding */
287 nf_bridge
->mask
|= BRNF_BRIDGED_DNAT
;
288 /* FIXME Need to refragment */
289 ret
= neigh
->output(neigh
, skb
);
291 neigh_release(neigh
);
300 br_nf_ipv4_daddr_was_changed(const struct sk_buff
*skb
,
301 const struct nf_bridge_info
*nf_bridge
)
303 return ip_hdr(skb
)->daddr
!= nf_bridge
->ipv4_daddr
;
306 /* This requires some explaining. If DNAT has taken place,
307 * we will need to fix up the destination Ethernet address.
308 * This is also true when SNAT takes place (for the reply direction).
310 * There are two cases to consider:
311 * 1. The packet was DNAT'ed to a device in the same bridge
312 * port group as it was received on. We can still bridge
314 * 2. The packet was DNAT'ed to a different device, either
315 * a non-bridged device or another bridge port group.
316 * The packet will need to be routed.
318 * The correct way of distinguishing between these two cases is to
319 * call ip_route_input() and to look at skb->dst->dev, which is
320 * changed to the destination device if ip_route_input() succeeds.
322 * Let's first consider the case that ip_route_input() succeeds:
324 * If the output device equals the logical bridge device the packet
325 * came in on, we can consider this bridging. The corresponding MAC
326 * address will be obtained in br_nf_pre_routing_finish_bridge.
327 * Otherwise, the packet is considered to be routed and we just
328 * change the destination MAC address so that the packet will
329 * later be passed up to the IP stack to be routed. For a redirected
330 * packet, ip_route_input() will give back the localhost as output device,
331 * which differs from the bridge device.
333 * Let's now consider the case that ip_route_input() fails:
335 * This can be because the destination address is martian, in which case
336 * the packet will be dropped.
337 * If IP forwarding is disabled, ip_route_input() will fail, while
338 * ip_route_output_key() can return success. The source
339 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
340 * thinks we're handling a locally generated packet and won't care
341 * if IP forwarding is enabled. If the output device equals the logical bridge
342 * device, we proceed as if ip_route_input() succeeded. If it differs from the
343 * logical bridge port or if ip_route_output_key() fails we drop the packet.
345 static int br_nf_pre_routing_finish(struct sock
*sk
, struct sk_buff
*skb
)
347 struct net_device
*dev
= skb
->dev
;
348 struct iphdr
*iph
= ip_hdr(skb
);
349 struct nf_bridge_info
*nf_bridge
= nf_bridge_info_get(skb
);
353 nf_bridge
->frag_max_size
= IPCB(skb
)->frag_max_size
;
355 if (nf_bridge
->pkt_otherhost
) {
356 skb
->pkt_type
= PACKET_OTHERHOST
;
357 nf_bridge
->pkt_otherhost
= false;
359 nf_bridge
->mask
&= ~BRNF_NF_BRIDGE_PREROUTING
;
360 if (br_nf_ipv4_daddr_was_changed(skb
, nf_bridge
)) {
361 if ((err
= ip_route_input(skb
, iph
->daddr
, iph
->saddr
, iph
->tos
, dev
))) {
362 struct in_device
*in_dev
= __in_dev_get_rcu(dev
);
364 /* If err equals -EHOSTUNREACH the error is due to a
365 * martian destination or due to the fact that
366 * forwarding is disabled. For most martian packets,
367 * ip_route_output_key() will fail. It won't fail for 2 types of
368 * martian destinations: loopback destinations and destination
369 * 0.0.0.0. In both cases the packet will be dropped because the
370 * destination is the loopback device and not the bridge. */
371 if (err
!= -EHOSTUNREACH
|| !in_dev
|| IN_DEV_FORWARD(in_dev
))
374 rt
= ip_route_output(dev_net(dev
), iph
->daddr
, 0,
375 RT_TOS(iph
->tos
), 0);
377 /* - Bridged-and-DNAT'ed traffic doesn't
378 * require ip_forwarding. */
379 if (rt
->dst
.dev
== dev
) {
380 skb_dst_set(skb
, &rt
->dst
);
389 if (skb_dst(skb
)->dev
== dev
) {
391 skb
->dev
= nf_bridge
->physindev
;
392 nf_bridge_update_protocol(skb
);
393 nf_bridge_push_encap_header(skb
);
394 NF_HOOK_THRESH(NFPROTO_BRIDGE
,
396 sk
, skb
, skb
->dev
, NULL
,
397 br_nf_pre_routing_finish_bridge
,
401 ether_addr_copy(eth_hdr(skb
)->h_dest
, dev
->dev_addr
);
402 skb
->pkt_type
= PACKET_HOST
;
405 rt
= bridge_parent_rtable(nf_bridge
->physindev
);
410 skb_dst_set_noref(skb
, &rt
->dst
);
413 skb
->dev
= nf_bridge
->physindev
;
414 nf_bridge_update_protocol(skb
);
415 nf_bridge_push_encap_header(skb
);
416 NF_HOOK_THRESH(NFPROTO_BRIDGE
, NF_BR_PRE_ROUTING
, sk
, skb
,
418 br_handle_frame_finish
, 1);
423 static struct net_device
*brnf_get_logical_dev(struct sk_buff
*skb
, const struct net_device
*dev
)
425 struct net_device
*vlan
, *br
;
427 br
= bridge_parent(dev
);
428 if (brnf_pass_vlan_indev
== 0 || !skb_vlan_tag_present(skb
))
431 vlan
= __vlan_find_dev_deep_rcu(br
, skb
->vlan_proto
,
432 skb_vlan_tag_get(skb
) & VLAN_VID_MASK
);
434 return vlan
? vlan
: br
;
437 /* Some common code for IPv4/IPv6 */
438 struct net_device
*setup_pre_routing(struct sk_buff
*skb
)
440 struct nf_bridge_info
*nf_bridge
= nf_bridge_info_get(skb
);
442 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
443 skb
->pkt_type
= PACKET_HOST
;
444 nf_bridge
->pkt_otherhost
= true;
447 nf_bridge
->mask
|= BRNF_NF_BRIDGE_PREROUTING
;
448 nf_bridge
->physindev
= skb
->dev
;
449 skb
->dev
= brnf_get_logical_dev(skb
, skb
->dev
);
451 if (skb
->protocol
== htons(ETH_P_8021Q
))
452 nf_bridge
->orig_proto
= BRNF_PROTO_8021Q
;
453 else if (skb
->protocol
== htons(ETH_P_PPP_SES
))
454 nf_bridge
->orig_proto
= BRNF_PROTO_PPPOE
;
456 /* Must drop socket now because of tproxy. */
461 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
462 * Replicate the checks that IPv4 does on packet reception.
463 * Set skb->dev to the bridge device (i.e. parent of the
464 * receiving device) to make netfilter happy, the REDIRECT
465 * target in particular. Save the original destination IP
466 * address to be able to detect DNAT afterwards. */
467 static unsigned int br_nf_pre_routing(const struct nf_hook_ops
*ops
,
469 const struct nf_hook_state
*state
)
471 struct nf_bridge_info
*nf_bridge
;
472 struct net_bridge_port
*p
;
473 struct net_bridge
*br
;
474 __u32 len
= nf_bridge_encap_header_len(skb
);
476 if (unlikely(!pskb_may_pull(skb
, len
)))
479 p
= br_port_get_rcu(state
->in
);
484 if (IS_IPV6(skb
) || IS_VLAN_IPV6(skb
) || IS_PPPOE_IPV6(skb
)) {
485 if (!brnf_call_ip6tables
&& !br
->nf_call_ip6tables
)
488 nf_bridge_pull_encap_header_rcsum(skb
);
489 return br_nf_pre_routing_ipv6(ops
, skb
, state
);
492 if (!brnf_call_iptables
&& !br
->nf_call_iptables
)
495 if (!IS_IP(skb
) && !IS_VLAN_IP(skb
) && !IS_PPPOE_IP(skb
))
498 nf_bridge_pull_encap_header_rcsum(skb
);
500 if (br_validate_ipv4(skb
))
503 nf_bridge_put(skb
->nf_bridge
);
504 if (!nf_bridge_alloc(skb
))
506 if (!setup_pre_routing(skb
))
509 nf_bridge
= nf_bridge_info_get(skb
);
510 nf_bridge
->ipv4_daddr
= ip_hdr(skb
)->daddr
;
512 skb
->protocol
= htons(ETH_P_IP
);
514 NF_HOOK(NFPROTO_IPV4
, NF_INET_PRE_ROUTING
, state
->sk
, skb
,
516 br_nf_pre_routing_finish
);
522 /* PF_BRIDGE/LOCAL_IN ************************************************/
523 /* The packet is locally destined, which requires a real
524 * dst_entry, so detach the fake one. On the way up, the
525 * packet would pass through PRE_ROUTING again (which already
526 * took place when the packet entered the bridge), but we
527 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
528 * prevent this from happening. */
529 static unsigned int br_nf_local_in(const struct nf_hook_ops
*ops
,
531 const struct nf_hook_state
*state
)
533 br_drop_fake_rtable(skb
);
537 /* PF_BRIDGE/FORWARD *************************************************/
538 static int br_nf_forward_finish(struct sock
*sk
, struct sk_buff
*skb
)
540 struct nf_bridge_info
*nf_bridge
= nf_bridge_info_get(skb
);
541 struct net_device
*in
;
543 if (!IS_ARP(skb
) && !IS_VLAN_ARP(skb
)) {
545 if (skb
->protocol
== htons(ETH_P_IP
))
546 nf_bridge
->frag_max_size
= IPCB(skb
)->frag_max_size
;
548 if (skb
->protocol
== htons(ETH_P_IPV6
))
549 nf_bridge
->frag_max_size
= IP6CB(skb
)->frag_max_size
;
551 in
= nf_bridge
->physindev
;
552 if (nf_bridge
->pkt_otherhost
) {
553 skb
->pkt_type
= PACKET_OTHERHOST
;
554 nf_bridge
->pkt_otherhost
= false;
556 nf_bridge_update_protocol(skb
);
558 in
= *((struct net_device
**)(skb
->cb
));
560 nf_bridge_push_encap_header(skb
);
562 NF_HOOK_THRESH(NFPROTO_BRIDGE
, NF_BR_FORWARD
, sk
, skb
,
563 in
, skb
->dev
, br_forward_finish
, 1);
568 /* This is the 'purely bridged' case. For IP, we pass the packet to
569 * netfilter with indev and outdev set to the bridge device,
570 * but we are still able to filter on the 'real' indev/outdev
571 * because of the physdev module. For ARP, indev and outdev are the
573 static unsigned int br_nf_forward_ip(const struct nf_hook_ops
*ops
,
575 const struct nf_hook_state
*state
)
577 struct nf_bridge_info
*nf_bridge
;
578 struct net_device
*parent
;
584 /* Need exclusive nf_bridge_info since we might have multiple
585 * different physoutdevs. */
586 if (!nf_bridge_unshare(skb
))
589 nf_bridge
= nf_bridge_info_get(skb
);
593 parent
= bridge_parent(state
->out
);
597 if (IS_IP(skb
) || IS_VLAN_IP(skb
) || IS_PPPOE_IP(skb
))
599 else if (IS_IPV6(skb
) || IS_VLAN_IPV6(skb
) || IS_PPPOE_IPV6(skb
))
604 nf_bridge_pull_encap_header(skb
);
606 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
607 skb
->pkt_type
= PACKET_HOST
;
608 nf_bridge
->pkt_otherhost
= true;
611 if (pf
== NFPROTO_IPV4
) {
612 if (br_validate_ipv4(skb
))
614 IPCB(skb
)->frag_max_size
= nf_bridge
->frag_max_size
;
617 if (pf
== NFPROTO_IPV6
) {
618 if (br_validate_ipv6(skb
))
620 IP6CB(skb
)->frag_max_size
= nf_bridge
->frag_max_size
;
623 nf_bridge
->physoutdev
= skb
->dev
;
624 if (pf
== NFPROTO_IPV4
)
625 skb
->protocol
= htons(ETH_P_IP
);
627 skb
->protocol
= htons(ETH_P_IPV6
);
629 NF_HOOK(pf
, NF_INET_FORWARD
, NULL
, skb
,
630 brnf_get_logical_dev(skb
, state
->in
),
631 parent
, br_nf_forward_finish
);
636 static unsigned int br_nf_forward_arp(const struct nf_hook_ops
*ops
,
638 const struct nf_hook_state
*state
)
640 struct net_bridge_port
*p
;
641 struct net_bridge
*br
;
642 struct net_device
**d
= (struct net_device
**)(skb
->cb
);
644 p
= br_port_get_rcu(state
->out
);
649 if (!brnf_call_arptables
&& !br
->nf_call_arptables
)
653 if (!IS_VLAN_ARP(skb
))
655 nf_bridge_pull_encap_header(skb
);
658 if (arp_hdr(skb
)->ar_pln
!= 4) {
659 if (IS_VLAN_ARP(skb
))
660 nf_bridge_push_encap_header(skb
);
664 NF_HOOK(NFPROTO_ARP
, NF_ARP_FORWARD
, state
->sk
, skb
,
665 state
->in
, state
->out
, br_nf_forward_finish
);
670 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) || IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
671 static int br_nf_push_frag_xmit(struct sock
*sk
, struct sk_buff
*skb
)
673 struct brnf_frag_data
*data
;
676 data
= this_cpu_ptr(&brnf_frag_data_storage
);
677 err
= skb_cow_head(skb
, data
->size
);
684 if (data
->vlan_tci
) {
685 skb
->vlan_tci
= data
->vlan_tci
;
686 skb
->vlan_proto
= data
->vlan_proto
;
689 skb_copy_to_linear_data_offset(skb
, -data
->size
, data
->mac
, data
->size
);
690 __skb_push(skb
, data
->encap_size
);
692 nf_bridge_info_free(skb
);
693 return br_dev_queue_push_xmit(sk
, skb
);
697 static int br_nf_ip_fragment(struct sock
*sk
, struct sk_buff
*skb
,
698 int (*output
)(struct sock
*, struct sk_buff
*))
700 unsigned int mtu
= ip_skb_dst_mtu(skb
);
701 struct iphdr
*iph
= ip_hdr(skb
);
702 struct rtable
*rt
= skb_rtable(skb
);
703 struct net_device
*dev
= rt
->dst
.dev
;
705 if (unlikely(((iph
->frag_off
& htons(IP_DF
)) && !skb
->ignore_df
) ||
706 (IPCB(skb
)->frag_max_size
&&
707 IPCB(skb
)->frag_max_size
> mtu
))) {
708 IP_INC_STATS(dev_net(dev
), IPSTATS_MIB_FRAGFAILS
);
713 return ip_do_fragment(sk
, skb
, output
);
716 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff
*skb
)
718 if (skb
->nf_bridge
->orig_proto
== BRNF_PROTO_PPPOE
)
719 return PPPOE_SES_HLEN
;
723 static int br_nf_dev_queue_xmit(struct sock
*sk
, struct sk_buff
*skb
)
725 struct nf_bridge_info
*nf_bridge
;
726 unsigned int mtu_reserved
;
728 mtu_reserved
= nf_bridge_mtu_reduction(skb
);
730 if (skb_is_gso(skb
) || skb
->len
+ mtu_reserved
<= skb
->dev
->mtu
) {
731 nf_bridge_info_free(skb
);
732 return br_dev_queue_push_xmit(sk
, skb
);
735 nf_bridge
= nf_bridge_info_get(skb
);
737 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4)
738 /* This is wrong! We should preserve the original fragment
739 * boundaries by preserving frag_list rather than refragmenting.
741 if (skb
->protocol
== htons(ETH_P_IP
)) {
742 struct brnf_frag_data
*data
;
744 if (br_validate_ipv4(skb
))
747 IPCB(skb
)->frag_max_size
= nf_bridge
->frag_max_size
;
749 nf_bridge_update_protocol(skb
);
751 data
= this_cpu_ptr(&brnf_frag_data_storage
);
753 data
->vlan_tci
= skb
->vlan_tci
;
754 data
->vlan_proto
= skb
->vlan_proto
;
755 data
->encap_size
= nf_bridge_encap_header_len(skb
);
756 data
->size
= ETH_HLEN
+ data
->encap_size
;
758 skb_copy_from_linear_data_offset(skb
, -data
->size
, data
->mac
,
761 return br_nf_ip_fragment(sk
, skb
, br_nf_push_frag_xmit
);
764 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
765 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
766 const struct nf_ipv6_ops
*v6ops
= nf_get_ipv6_ops();
767 struct brnf_frag_data
*data
;
769 if (br_validate_ipv6(skb
))
772 IP6CB(skb
)->frag_max_size
= nf_bridge
->frag_max_size
;
774 nf_bridge_update_protocol(skb
);
776 data
= this_cpu_ptr(&brnf_frag_data_storage
);
777 data
->encap_size
= nf_bridge_encap_header_len(skb
);
778 data
->size
= ETH_HLEN
+ data
->encap_size
;
780 skb_copy_from_linear_data_offset(skb
, -data
->size
, data
->mac
,
784 return v6ops
->fragment(sk
, skb
, br_nf_push_frag_xmit
);
789 nf_bridge_info_free(skb
);
790 return br_dev_queue_push_xmit(sk
, skb
);
793 /* PF_BRIDGE/POST_ROUTING ********************************************/
794 static unsigned int br_nf_post_routing(const struct nf_hook_ops
*ops
,
796 const struct nf_hook_state
*state
)
798 struct nf_bridge_info
*nf_bridge
= nf_bridge_info_get(skb
);
799 struct net_device
*realoutdev
= bridge_parent(skb
->dev
);
802 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
803 * on a bridge, but was delivered locally and is now being routed:
805 * POST_ROUTING was already invoked from the ip stack.
807 if (!nf_bridge
|| !nf_bridge
->physoutdev
)
813 if (IS_IP(skb
) || IS_VLAN_IP(skb
) || IS_PPPOE_IP(skb
))
815 else if (IS_IPV6(skb
) || IS_VLAN_IPV6(skb
) || IS_PPPOE_IPV6(skb
))
820 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
821 * about the value of skb->pkt_type. */
822 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
823 skb
->pkt_type
= PACKET_HOST
;
824 nf_bridge
->pkt_otherhost
= true;
827 nf_bridge_pull_encap_header(skb
);
828 if (pf
== NFPROTO_IPV4
)
829 skb
->protocol
= htons(ETH_P_IP
);
831 skb
->protocol
= htons(ETH_P_IPV6
);
833 NF_HOOK(pf
, NF_INET_POST_ROUTING
, state
->sk
, skb
,
835 br_nf_dev_queue_xmit
);
840 /* IP/SABOTAGE *****************************************************/
841 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
842 * for the second time. */
843 static unsigned int ip_sabotage_in(const struct nf_hook_ops
*ops
,
845 const struct nf_hook_state
*state
)
847 if (skb
->nf_bridge
&&
848 !(skb
->nf_bridge
->mask
& BRNF_NF_BRIDGE_PREROUTING
)) {
855 /* This is called when br_netfilter has called into iptables/netfilter,
856 * and DNAT has taken place on a bridge-forwarded packet.
858 * neigh->output has created a new MAC header, with local br0 MAC
861 * This restores the original MAC saddr of the bridged packet
862 * before invoking bridge forward logic to transmit the packet.
864 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff
*skb
)
866 struct nf_bridge_info
*nf_bridge
= nf_bridge_info_get(skb
);
868 skb_pull(skb
, ETH_HLEN
);
869 nf_bridge
->mask
&= ~BRNF_BRIDGED_DNAT
;
871 BUILD_BUG_ON(sizeof(nf_bridge
->neigh_header
) != (ETH_HLEN
- ETH_ALEN
));
873 skb_copy_to_linear_data_offset(skb
, -(ETH_HLEN
- ETH_ALEN
),
874 nf_bridge
->neigh_header
,
875 ETH_HLEN
- ETH_ALEN
);
876 skb
->dev
= nf_bridge
->physindev
;
878 nf_bridge
->physoutdev
= NULL
;
879 br_handle_frame_finish(NULL
, skb
);
882 static int br_nf_dev_xmit(struct sk_buff
*skb
)
884 if (skb
->nf_bridge
&& (skb
->nf_bridge
->mask
& BRNF_BRIDGED_DNAT
)) {
885 br_nf_pre_routing_finish_bridge_slow(skb
);
891 static const struct nf_br_ops br_ops
= {
892 .br_dev_xmit_hook
= br_nf_dev_xmit
,
895 void br_netfilter_enable(void)
898 EXPORT_SYMBOL_GPL(br_netfilter_enable
);
900 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
901 * br_dev_queue_push_xmit is called afterwards */
902 static struct nf_hook_ops br_nf_ops
[] __read_mostly
= {
904 .hook
= br_nf_pre_routing
,
905 .owner
= THIS_MODULE
,
906 .pf
= NFPROTO_BRIDGE
,
907 .hooknum
= NF_BR_PRE_ROUTING
,
908 .priority
= NF_BR_PRI_BRNF
,
911 .hook
= br_nf_local_in
,
912 .owner
= THIS_MODULE
,
913 .pf
= NFPROTO_BRIDGE
,
914 .hooknum
= NF_BR_LOCAL_IN
,
915 .priority
= NF_BR_PRI_BRNF
,
918 .hook
= br_nf_forward_ip
,
919 .owner
= THIS_MODULE
,
920 .pf
= NFPROTO_BRIDGE
,
921 .hooknum
= NF_BR_FORWARD
,
922 .priority
= NF_BR_PRI_BRNF
- 1,
925 .hook
= br_nf_forward_arp
,
926 .owner
= THIS_MODULE
,
927 .pf
= NFPROTO_BRIDGE
,
928 .hooknum
= NF_BR_FORWARD
,
929 .priority
= NF_BR_PRI_BRNF
,
932 .hook
= br_nf_post_routing
,
933 .owner
= THIS_MODULE
,
934 .pf
= NFPROTO_BRIDGE
,
935 .hooknum
= NF_BR_POST_ROUTING
,
936 .priority
= NF_BR_PRI_LAST
,
939 .hook
= ip_sabotage_in
,
940 .owner
= THIS_MODULE
,
942 .hooknum
= NF_INET_PRE_ROUTING
,
943 .priority
= NF_IP_PRI_FIRST
,
946 .hook
= ip_sabotage_in
,
947 .owner
= THIS_MODULE
,
949 .hooknum
= NF_INET_PRE_ROUTING
,
950 .priority
= NF_IP6_PRI_FIRST
,
956 int brnf_sysctl_call_tables(struct ctl_table
*ctl
, int write
,
957 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
961 ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
963 if (write
&& *(int *)(ctl
->data
))
964 *(int *)(ctl
->data
) = 1;
968 static struct ctl_table brnf_table
[] = {
970 .procname
= "bridge-nf-call-arptables",
971 .data
= &brnf_call_arptables
,
972 .maxlen
= sizeof(int),
974 .proc_handler
= brnf_sysctl_call_tables
,
977 .procname
= "bridge-nf-call-iptables",
978 .data
= &brnf_call_iptables
,
979 .maxlen
= sizeof(int),
981 .proc_handler
= brnf_sysctl_call_tables
,
984 .procname
= "bridge-nf-call-ip6tables",
985 .data
= &brnf_call_ip6tables
,
986 .maxlen
= sizeof(int),
988 .proc_handler
= brnf_sysctl_call_tables
,
991 .procname
= "bridge-nf-filter-vlan-tagged",
992 .data
= &brnf_filter_vlan_tagged
,
993 .maxlen
= sizeof(int),
995 .proc_handler
= brnf_sysctl_call_tables
,
998 .procname
= "bridge-nf-filter-pppoe-tagged",
999 .data
= &brnf_filter_pppoe_tagged
,
1000 .maxlen
= sizeof(int),
1002 .proc_handler
= brnf_sysctl_call_tables
,
1005 .procname
= "bridge-nf-pass-vlan-input-dev",
1006 .data
= &brnf_pass_vlan_indev
,
1007 .maxlen
= sizeof(int),
1009 .proc_handler
= brnf_sysctl_call_tables
,
1015 static int __init
br_netfilter_init(void)
1019 ret
= nf_register_hooks(br_nf_ops
, ARRAY_SIZE(br_nf_ops
));
1023 #ifdef CONFIG_SYSCTL
1024 brnf_sysctl_header
= register_net_sysctl(&init_net
, "net/bridge", brnf_table
);
1025 if (brnf_sysctl_header
== NULL
) {
1027 "br_netfilter: can't register to sysctl.\n");
1028 nf_unregister_hooks(br_nf_ops
, ARRAY_SIZE(br_nf_ops
));
1032 RCU_INIT_POINTER(nf_br_ops
, &br_ops
);
1033 printk(KERN_NOTICE
"Bridge firewalling registered\n");
1037 static void __exit
br_netfilter_fini(void)
1039 RCU_INIT_POINTER(nf_br_ops
, NULL
);
1040 nf_unregister_hooks(br_nf_ops
, ARRAY_SIZE(br_nf_ops
));
1041 #ifdef CONFIG_SYSCTL
1042 unregister_net_sysctl_table(brnf_sysctl_header
);
1046 module_init(br_netfilter_init
);
1047 module_exit(br_netfilter_fini
);
1049 MODULE_LICENSE("GPL");
1050 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1051 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1052 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");