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
;
55 struct ctl_table_header
*ctl_hdr
;
58 /* default value is 1 */
63 /* default value is 0 */
64 int filter_vlan_tagged
;
65 int filter_pppoe_tagged
;
70 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
72 #define IS_IPV6(skb) \
73 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
76 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
78 static inline __be16
vlan_proto(const struct sk_buff
*skb
)
80 if (skb_vlan_tag_present(skb
))
82 else if (skb
->protocol
== htons(ETH_P_8021Q
))
83 return vlan_eth_hdr(skb
)->h_vlan_encapsulated_proto
;
88 static inline bool is_vlan_ip(const struct sk_buff
*skb
, const struct net
*net
)
90 struct brnf_net
*brnet
= net_generic(net
, brnf_net_id
);
92 return vlan_proto(skb
) == htons(ETH_P_IP
) && brnet
->filter_vlan_tagged
;
95 static inline bool is_vlan_ipv6(const struct sk_buff
*skb
,
96 const struct net
*net
)
98 struct brnf_net
*brnet
= net_generic(net
, brnf_net_id
);
100 return vlan_proto(skb
) == htons(ETH_P_IPV6
) &&
101 brnet
->filter_vlan_tagged
;
104 static inline bool is_vlan_arp(const struct sk_buff
*skb
, const struct net
*net
)
106 struct brnf_net
*brnet
= net_generic(net
, brnf_net_id
);
108 return vlan_proto(skb
) == htons(ETH_P_ARP
) && brnet
->filter_vlan_tagged
;
111 static inline __be16
pppoe_proto(const struct sk_buff
*skb
)
113 return *((__be16
*)(skb_mac_header(skb
) + ETH_HLEN
+
114 sizeof(struct pppoe_hdr
)));
117 static inline bool is_pppoe_ip(const struct sk_buff
*skb
, const struct net
*net
)
119 struct brnf_net
*brnet
= net_generic(net
, brnf_net_id
);
121 return skb
->protocol
== htons(ETH_P_PPP_SES
) &&
122 pppoe_proto(skb
) == htons(PPP_IP
) && brnet
->filter_pppoe_tagged
;
125 static inline bool is_pppoe_ipv6(const struct sk_buff
*skb
,
126 const struct net
*net
)
128 struct brnf_net
*brnet
= net_generic(net
, brnf_net_id
);
130 return skb
->protocol
== htons(ETH_P_PPP_SES
) &&
131 pppoe_proto(skb
) == htons(PPP_IPV6
) &&
132 brnet
->filter_pppoe_tagged
;
135 /* largest possible L2 header, see br_nf_dev_queue_xmit() */
136 #define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN)
138 struct brnf_frag_data
{
139 char mac
[NF_BRIDGE_MAX_MAC_HEADER_LENGTH
];
146 static DEFINE_PER_CPU(struct brnf_frag_data
, brnf_frag_data_storage
);
148 static void nf_bridge_info_free(struct sk_buff
*skb
)
150 if (skb
->nf_bridge
) {
151 nf_bridge_put(skb
->nf_bridge
);
152 skb
->nf_bridge
= NULL
;
156 static inline struct net_device
*bridge_parent(const struct net_device
*dev
)
158 struct net_bridge_port
*port
;
160 port
= br_port_get_rcu(dev
);
161 return port
? port
->br
->dev
: NULL
;
164 static inline struct nf_bridge_info
*nf_bridge_unshare(struct sk_buff
*skb
)
166 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
168 if (refcount_read(&nf_bridge
->use
) > 1) {
169 struct nf_bridge_info
*tmp
= nf_bridge_alloc(skb
);
172 memcpy(tmp
, nf_bridge
, sizeof(struct nf_bridge_info
));
173 refcount_set(&tmp
->use
, 1);
175 nf_bridge_put(nf_bridge
);
181 unsigned int nf_bridge_encap_header_len(const struct sk_buff
*skb
)
183 switch (skb
->protocol
) {
184 case __cpu_to_be16(ETH_P_8021Q
):
186 case __cpu_to_be16(ETH_P_PPP_SES
):
187 return PPPOE_SES_HLEN
;
193 static inline void nf_bridge_pull_encap_header(struct sk_buff
*skb
)
195 unsigned int len
= nf_bridge_encap_header_len(skb
);
198 skb
->network_header
+= len
;
201 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff
*skb
)
203 unsigned int len
= nf_bridge_encap_header_len(skb
);
205 skb_pull_rcsum(skb
, len
);
206 skb
->network_header
+= len
;
209 /* When handing a packet over to the IP layer
210 * check whether we have a skb that is in the
214 static int br_validate_ipv4(struct net
*net
, struct sk_buff
*skb
)
216 const struct iphdr
*iph
;
219 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
224 /* Basic sanity checks */
225 if (iph
->ihl
< 5 || iph
->version
!= 4)
228 if (!pskb_may_pull(skb
, iph
->ihl
*4))
232 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
235 len
= ntohs(iph
->tot_len
);
236 if (skb
->len
< len
) {
237 __IP_INC_STATS(net
, IPSTATS_MIB_INTRUNCATEDPKTS
);
239 } else if (len
< (iph
->ihl
*4))
242 if (pskb_trim_rcsum(skb
, len
)) {
243 __IP_INC_STATS(net
, IPSTATS_MIB_INDISCARDS
);
247 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
248 /* We should really parse IP options here but until
249 * somebody who actually uses IP options complains to
250 * us we'll just silently ignore the options because
256 __IP_INC_STATS(net
, IPSTATS_MIB_INHDRERRORS
);
261 void nf_bridge_update_protocol(struct sk_buff
*skb
)
263 switch (skb
->nf_bridge
->orig_proto
) {
264 case BRNF_PROTO_8021Q
:
265 skb
->protocol
= htons(ETH_P_8021Q
);
267 case BRNF_PROTO_PPPOE
:
268 skb
->protocol
= htons(ETH_P_PPP_SES
);
270 case BRNF_PROTO_UNCHANGED
:
275 /* Obtain the correct destination MAC address, while preserving the original
276 * source MAC address. If we already know this address, we just copy it. If we
277 * don't, we use the neighbour framework to find out. In both cases, we make
278 * sure that br_handle_frame_finish() is called afterwards.
280 int br_nf_pre_routing_finish_bridge(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
282 struct neighbour
*neigh
;
283 struct dst_entry
*dst
;
285 skb
->dev
= bridge_parent(skb
->dev
);
289 neigh
= dst_neigh_lookup_skb(dst
, skb
);
291 struct nf_bridge_info
*nf_bridge
= nf_bridge_info_get(skb
);
294 if ((neigh
->nud_state
& NUD_CONNECTED
) && neigh
->hh
.hh_len
) {
295 neigh_hh_bridge(&neigh
->hh
, skb
);
296 skb
->dev
= nf_bridge
->physindev
;
297 ret
= br_handle_frame_finish(net
, sk
, skb
);
299 /* the neighbour function below overwrites the complete
300 * MAC header, so we save the Ethernet source address and
303 skb_copy_from_linear_data_offset(skb
,
304 -(ETH_HLEN
-ETH_ALEN
),
305 nf_bridge
->neigh_header
,
307 /* tell br_dev_xmit to continue with forwarding */
308 nf_bridge
->bridged_dnat
= 1;
309 /* FIXME Need to refragment */
310 ret
= neigh
->output(neigh
, skb
);
312 neigh_release(neigh
);
321 br_nf_ipv4_daddr_was_changed(const struct sk_buff
*skb
,
322 const struct nf_bridge_info
*nf_bridge
)
324 return ip_hdr(skb
)->daddr
!= nf_bridge
->ipv4_daddr
;
327 /* This requires some explaining. If DNAT has taken place,
328 * we will need to fix up the destination Ethernet address.
329 * This is also true when SNAT takes place (for the reply direction).
331 * There are two cases to consider:
332 * 1. The packet was DNAT'ed to a device in the same bridge
333 * port group as it was received on. We can still bridge
335 * 2. The packet was DNAT'ed to a different device, either
336 * a non-bridged device or another bridge port group.
337 * The packet will need to be routed.
339 * The correct way of distinguishing between these two cases is to
340 * call ip_route_input() and to look at skb->dst->dev, which is
341 * changed to the destination device if ip_route_input() succeeds.
343 * Let's first consider the case that ip_route_input() succeeds:
345 * If the output device equals the logical bridge device the packet
346 * came in on, we can consider this bridging. The corresponding MAC
347 * address will be obtained in br_nf_pre_routing_finish_bridge.
348 * Otherwise, the packet is considered to be routed and we just
349 * change the destination MAC address so that the packet will
350 * later be passed up to the IP stack to be routed. For a redirected
351 * packet, ip_route_input() will give back the localhost as output device,
352 * which differs from the bridge device.
354 * Let's now consider the case that ip_route_input() fails:
356 * This can be because the destination address is martian, in which case
357 * the packet will be dropped.
358 * If IP forwarding is disabled, ip_route_input() will fail, while
359 * ip_route_output_key() can return success. The source
360 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
361 * thinks we're handling a locally generated packet and won't care
362 * if IP forwarding is enabled. If the output device equals the logical bridge
363 * device, we proceed as if ip_route_input() succeeded. If it differs from the
364 * logical bridge port or if ip_route_output_key() fails we drop the packet.
366 static int br_nf_pre_routing_finish(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
368 struct net_device
*dev
= skb
->dev
;
369 struct iphdr
*iph
= ip_hdr(skb
);
370 struct nf_bridge_info
*nf_bridge
= nf_bridge_info_get(skb
);
374 nf_bridge
->frag_max_size
= IPCB(skb
)->frag_max_size
;
376 if (nf_bridge
->pkt_otherhost
) {
377 skb
->pkt_type
= PACKET_OTHERHOST
;
378 nf_bridge
->pkt_otherhost
= false;
380 nf_bridge
->in_prerouting
= 0;
381 if (br_nf_ipv4_daddr_was_changed(skb
, nf_bridge
)) {
382 if ((err
= ip_route_input(skb
, iph
->daddr
, iph
->saddr
, iph
->tos
, dev
))) {
383 struct in_device
*in_dev
= __in_dev_get_rcu(dev
);
385 /* If err equals -EHOSTUNREACH the error is due to a
386 * martian destination or due to the fact that
387 * forwarding is disabled. For most martian packets,
388 * ip_route_output_key() will fail. It won't fail for 2 types of
389 * martian destinations: loopback destinations and destination
390 * 0.0.0.0. In both cases the packet will be dropped because the
391 * destination is the loopback device and not the bridge. */
392 if (err
!= -EHOSTUNREACH
|| !in_dev
|| IN_DEV_FORWARD(in_dev
))
395 rt
= ip_route_output(net
, iph
->daddr
, 0,
396 RT_TOS(iph
->tos
), 0);
398 /* - Bridged-and-DNAT'ed traffic doesn't
399 * require ip_forwarding. */
400 if (rt
->dst
.dev
== dev
) {
401 skb_dst_set(skb
, &rt
->dst
);
410 if (skb_dst(skb
)->dev
== dev
) {
412 skb
->dev
= nf_bridge
->physindev
;
413 nf_bridge_update_protocol(skb
);
414 nf_bridge_push_encap_header(skb
);
415 br_nf_hook_thresh(NF_BR_PRE_ROUTING
,
416 net
, sk
, skb
, skb
->dev
,
418 br_nf_pre_routing_finish_bridge
);
421 ether_addr_copy(eth_hdr(skb
)->h_dest
, dev
->dev_addr
);
422 skb
->pkt_type
= PACKET_HOST
;
425 rt
= bridge_parent_rtable(nf_bridge
->physindev
);
430 skb_dst_set_noref(skb
, &rt
->dst
);
433 skb
->dev
= nf_bridge
->physindev
;
434 nf_bridge_update_protocol(skb
);
435 nf_bridge_push_encap_header(skb
);
436 br_nf_hook_thresh(NF_BR_PRE_ROUTING
, net
, sk
, skb
, skb
->dev
, NULL
,
437 br_handle_frame_finish
);
441 static struct net_device
*brnf_get_logical_dev(struct sk_buff
*skb
,
442 const struct net_device
*dev
,
443 const struct net
*net
)
445 struct net_device
*vlan
, *br
;
446 struct brnf_net
*brnet
= net_generic(net
, brnf_net_id
);
448 br
= bridge_parent(dev
);
450 if (brnet
->pass_vlan_indev
== 0 || !skb_vlan_tag_present(skb
))
453 vlan
= __vlan_find_dev_deep_rcu(br
, skb
->vlan_proto
,
454 skb_vlan_tag_get(skb
) & VLAN_VID_MASK
);
456 return vlan
? vlan
: br
;
459 /* Some common code for IPv4/IPv6 */
460 struct net_device
*setup_pre_routing(struct sk_buff
*skb
, const struct net
*net
)
462 struct nf_bridge_info
*nf_bridge
= nf_bridge_info_get(skb
);
464 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
465 skb
->pkt_type
= PACKET_HOST
;
466 nf_bridge
->pkt_otherhost
= true;
469 nf_bridge
->in_prerouting
= 1;
470 nf_bridge
->physindev
= skb
->dev
;
471 skb
->dev
= brnf_get_logical_dev(skb
, skb
->dev
, net
);
473 if (skb
->protocol
== htons(ETH_P_8021Q
))
474 nf_bridge
->orig_proto
= BRNF_PROTO_8021Q
;
475 else if (skb
->protocol
== htons(ETH_P_PPP_SES
))
476 nf_bridge
->orig_proto
= BRNF_PROTO_PPPOE
;
478 /* Must drop socket now because of tproxy. */
483 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
484 * Replicate the checks that IPv4 does on packet reception.
485 * Set skb->dev to the bridge device (i.e. parent of the
486 * receiving device) to make netfilter happy, the REDIRECT
487 * target in particular. Save the original destination IP
488 * address to be able to detect DNAT afterwards. */
489 static unsigned int br_nf_pre_routing(void *priv
,
491 const struct nf_hook_state
*state
)
493 struct nf_bridge_info
*nf_bridge
;
494 struct net_bridge_port
*p
;
495 struct net_bridge
*br
;
496 __u32 len
= nf_bridge_encap_header_len(skb
);
497 struct brnf_net
*brnet
;
499 if (unlikely(!pskb_may_pull(skb
, len
)))
502 p
= br_port_get_rcu(state
->in
);
507 brnet
= net_generic(state
->net
, brnf_net_id
);
508 if (IS_IPV6(skb
) || is_vlan_ipv6(skb
, state
->net
) ||
509 is_pppoe_ipv6(skb
, state
->net
)) {
510 if (!brnet
->call_ip6tables
&&
511 !br_opt_get(br
, BROPT_NF_CALL_IP6TABLES
))
514 nf_bridge_pull_encap_header_rcsum(skb
);
515 return br_nf_pre_routing_ipv6(priv
, skb
, state
);
518 if (!brnet
->call_iptables
&& !br_opt_get(br
, BROPT_NF_CALL_IPTABLES
))
521 if (!IS_IP(skb
) && !is_vlan_ip(skb
, state
->net
) &&
522 !is_pppoe_ip(skb
, state
->net
))
525 nf_bridge_pull_encap_header_rcsum(skb
);
527 if (br_validate_ipv4(state
->net
, skb
))
530 nf_bridge_put(skb
->nf_bridge
);
531 if (!nf_bridge_alloc(skb
))
533 if (!setup_pre_routing(skb
, state
->net
))
536 nf_bridge
= nf_bridge_info_get(skb
);
537 nf_bridge
->ipv4_daddr
= ip_hdr(skb
)->daddr
;
539 skb
->protocol
= htons(ETH_P_IP
);
540 skb
->transport_header
= skb
->network_header
+ ip_hdr(skb
)->ihl
* 4;
542 NF_HOOK(NFPROTO_IPV4
, NF_INET_PRE_ROUTING
, state
->net
, state
->sk
, skb
,
544 br_nf_pre_routing_finish
);
550 /* PF_BRIDGE/FORWARD *************************************************/
551 static int br_nf_forward_finish(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
553 struct nf_bridge_info
*nf_bridge
= nf_bridge_info_get(skb
);
554 struct net_device
*in
;
556 if (!IS_ARP(skb
) && !is_vlan_arp(skb
, net
)) {
558 if (skb
->protocol
== htons(ETH_P_IP
))
559 nf_bridge
->frag_max_size
= IPCB(skb
)->frag_max_size
;
561 if (skb
->protocol
== htons(ETH_P_IPV6
))
562 nf_bridge
->frag_max_size
= IP6CB(skb
)->frag_max_size
;
564 in
= nf_bridge
->physindev
;
565 if (nf_bridge
->pkt_otherhost
) {
566 skb
->pkt_type
= PACKET_OTHERHOST
;
567 nf_bridge
->pkt_otherhost
= false;
569 nf_bridge_update_protocol(skb
);
571 in
= *((struct net_device
**)(skb
->cb
));
573 nf_bridge_push_encap_header(skb
);
575 br_nf_hook_thresh(NF_BR_FORWARD
, net
, sk
, skb
, in
, skb
->dev
,
581 /* This is the 'purely bridged' case. For IP, we pass the packet to
582 * netfilter with indev and outdev set to the bridge device,
583 * but we are still able to filter on the 'real' indev/outdev
584 * because of the physdev module. For ARP, indev and outdev are the
586 static unsigned int br_nf_forward_ip(void *priv
,
588 const struct nf_hook_state
*state
)
590 struct nf_bridge_info
*nf_bridge
;
591 struct net_device
*parent
;
597 /* Need exclusive nf_bridge_info since we might have multiple
598 * different physoutdevs. */
599 if (!nf_bridge_unshare(skb
))
602 nf_bridge
= nf_bridge_info_get(skb
);
606 parent
= bridge_parent(state
->out
);
610 if (IS_IP(skb
) || is_vlan_ip(skb
, state
->net
) ||
611 is_pppoe_ip(skb
, state
->net
))
613 else if (IS_IPV6(skb
) || is_vlan_ipv6(skb
, state
->net
) ||
614 is_pppoe_ipv6(skb
, state
->net
))
619 nf_bridge_pull_encap_header(skb
);
621 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
622 skb
->pkt_type
= PACKET_HOST
;
623 nf_bridge
->pkt_otherhost
= true;
626 if (pf
== NFPROTO_IPV4
) {
627 if (br_validate_ipv4(state
->net
, skb
))
629 IPCB(skb
)->frag_max_size
= nf_bridge
->frag_max_size
;
632 if (pf
== NFPROTO_IPV6
) {
633 if (br_validate_ipv6(state
->net
, skb
))
635 IP6CB(skb
)->frag_max_size
= nf_bridge
->frag_max_size
;
638 nf_bridge
->physoutdev
= skb
->dev
;
639 if (pf
== NFPROTO_IPV4
)
640 skb
->protocol
= htons(ETH_P_IP
);
642 skb
->protocol
= htons(ETH_P_IPV6
);
644 NF_HOOK(pf
, NF_INET_FORWARD
, state
->net
, NULL
, skb
,
645 brnf_get_logical_dev(skb
, state
->in
, state
->net
),
646 parent
, br_nf_forward_finish
);
651 static unsigned int br_nf_forward_arp(void *priv
,
653 const struct nf_hook_state
*state
)
655 struct net_bridge_port
*p
;
656 struct net_bridge
*br
;
657 struct net_device
**d
= (struct net_device
**)(skb
->cb
);
658 struct brnf_net
*brnet
;
660 p
= br_port_get_rcu(state
->out
);
665 brnet
= net_generic(state
->net
, brnf_net_id
);
666 if (!brnet
->call_arptables
&& !br_opt_get(br
, BROPT_NF_CALL_ARPTABLES
))
670 if (!is_vlan_arp(skb
, state
->net
))
672 nf_bridge_pull_encap_header(skb
);
675 if (arp_hdr(skb
)->ar_pln
!= 4) {
676 if (is_vlan_arp(skb
, state
->net
))
677 nf_bridge_push_encap_header(skb
);
681 NF_HOOK(NFPROTO_ARP
, NF_ARP_FORWARD
, state
->net
, state
->sk
, skb
,
682 state
->in
, state
->out
, br_nf_forward_finish
);
687 static int br_nf_push_frag_xmit(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
689 struct brnf_frag_data
*data
;
692 data
= this_cpu_ptr(&brnf_frag_data_storage
);
693 err
= skb_cow_head(skb
, data
->size
);
700 if (data
->vlan_tci
) {
701 skb
->vlan_tci
= data
->vlan_tci
;
702 skb
->vlan_proto
= data
->vlan_proto
;
705 skb_copy_to_linear_data_offset(skb
, -data
->size
, data
->mac
, data
->size
);
706 __skb_push(skb
, data
->encap_size
);
708 nf_bridge_info_free(skb
);
709 return br_dev_queue_push_xmit(net
, sk
, skb
);
713 br_nf_ip_fragment(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
,
714 int (*output
)(struct net
*, struct sock
*, struct sk_buff
*))
716 unsigned int mtu
= ip_skb_dst_mtu(sk
, skb
);
717 struct iphdr
*iph
= ip_hdr(skb
);
719 if (unlikely(((iph
->frag_off
& htons(IP_DF
)) && !skb
->ignore_df
) ||
720 (IPCB(skb
)->frag_max_size
&&
721 IPCB(skb
)->frag_max_size
> mtu
))) {
722 IP_INC_STATS(net
, IPSTATS_MIB_FRAGFAILS
);
727 return ip_do_fragment(net
, sk
, skb
, output
);
730 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff
*skb
)
732 if (skb
->nf_bridge
->orig_proto
== BRNF_PROTO_PPPOE
)
733 return PPPOE_SES_HLEN
;
737 static int br_nf_dev_queue_xmit(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
739 struct nf_bridge_info
*nf_bridge
= nf_bridge_info_get(skb
);
740 unsigned int mtu
, mtu_reserved
;
742 mtu_reserved
= nf_bridge_mtu_reduction(skb
);
745 if (nf_bridge
->frag_max_size
&& nf_bridge
->frag_max_size
< mtu
)
746 mtu
= nf_bridge
->frag_max_size
;
748 if (skb_is_gso(skb
) || skb
->len
+ mtu_reserved
<= mtu
) {
749 nf_bridge_info_free(skb
);
750 return br_dev_queue_push_xmit(net
, sk
, skb
);
753 /* This is wrong! We should preserve the original fragment
754 * boundaries by preserving frag_list rather than refragmenting.
756 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4
) &&
757 skb
->protocol
== htons(ETH_P_IP
)) {
758 struct brnf_frag_data
*data
;
760 if (br_validate_ipv4(net
, skb
))
763 IPCB(skb
)->frag_max_size
= nf_bridge
->frag_max_size
;
765 nf_bridge_update_protocol(skb
);
767 data
= this_cpu_ptr(&brnf_frag_data_storage
);
769 data
->vlan_tci
= skb
->vlan_tci
;
770 data
->vlan_proto
= skb
->vlan_proto
;
771 data
->encap_size
= nf_bridge_encap_header_len(skb
);
772 data
->size
= ETH_HLEN
+ data
->encap_size
;
774 skb_copy_from_linear_data_offset(skb
, -data
->size
, data
->mac
,
777 return br_nf_ip_fragment(net
, sk
, skb
, br_nf_push_frag_xmit
);
779 if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6
) &&
780 skb
->protocol
== htons(ETH_P_IPV6
)) {
781 const struct nf_ipv6_ops
*v6ops
= nf_get_ipv6_ops();
782 struct brnf_frag_data
*data
;
784 if (br_validate_ipv6(net
, skb
))
787 IP6CB(skb
)->frag_max_size
= nf_bridge
->frag_max_size
;
789 nf_bridge_update_protocol(skb
);
791 data
= this_cpu_ptr(&brnf_frag_data_storage
);
792 data
->encap_size
= nf_bridge_encap_header_len(skb
);
793 data
->size
= ETH_HLEN
+ data
->encap_size
;
795 skb_copy_from_linear_data_offset(skb
, -data
->size
, data
->mac
,
799 return v6ops
->fragment(net
, sk
, skb
, br_nf_push_frag_xmit
);
804 nf_bridge_info_free(skb
);
805 return br_dev_queue_push_xmit(net
, sk
, skb
);
811 /* PF_BRIDGE/POST_ROUTING ********************************************/
812 static unsigned int br_nf_post_routing(void *priv
,
814 const struct nf_hook_state
*state
)
816 struct nf_bridge_info
*nf_bridge
= nf_bridge_info_get(skb
);
817 struct net_device
*realoutdev
= bridge_parent(skb
->dev
);
820 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
821 * on a bridge, but was delivered locally and is now being routed:
823 * POST_ROUTING was already invoked from the ip stack.
825 if (!nf_bridge
|| !nf_bridge
->physoutdev
)
831 if (IS_IP(skb
) || is_vlan_ip(skb
, state
->net
) ||
832 is_pppoe_ip(skb
, state
->net
))
834 else if (IS_IPV6(skb
) || is_vlan_ipv6(skb
, state
->net
) ||
835 is_pppoe_ipv6(skb
, state
->net
))
840 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
841 * about the value of skb->pkt_type. */
842 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
843 skb
->pkt_type
= PACKET_HOST
;
844 nf_bridge
->pkt_otherhost
= true;
847 nf_bridge_pull_encap_header(skb
);
848 if (pf
== NFPROTO_IPV4
)
849 skb
->protocol
= htons(ETH_P_IP
);
851 skb
->protocol
= htons(ETH_P_IPV6
);
853 NF_HOOK(pf
, NF_INET_POST_ROUTING
, state
->net
, state
->sk
, skb
,
855 br_nf_dev_queue_xmit
);
860 /* IP/SABOTAGE *****************************************************/
861 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
862 * for the second time. */
863 static unsigned int ip_sabotage_in(void *priv
,
865 const struct nf_hook_state
*state
)
867 if (skb
->nf_bridge
&& !skb
->nf_bridge
->in_prerouting
&&
868 !netif_is_l3_master(skb
->dev
)) {
869 state
->okfn(state
->net
, state
->sk
, skb
);
876 /* This is called when br_netfilter has called into iptables/netfilter,
877 * and DNAT has taken place on a bridge-forwarded packet.
879 * neigh->output has created a new MAC header, with local br0 MAC
882 * This restores the original MAC saddr of the bridged packet
883 * before invoking bridge forward logic to transmit the packet.
885 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff
*skb
)
887 struct nf_bridge_info
*nf_bridge
= nf_bridge_info_get(skb
);
889 skb_pull(skb
, ETH_HLEN
);
890 nf_bridge
->bridged_dnat
= 0;
892 BUILD_BUG_ON(sizeof(nf_bridge
->neigh_header
) != (ETH_HLEN
- ETH_ALEN
));
894 skb_copy_to_linear_data_offset(skb
, -(ETH_HLEN
- ETH_ALEN
),
895 nf_bridge
->neigh_header
,
896 ETH_HLEN
- ETH_ALEN
);
897 skb
->dev
= nf_bridge
->physindev
;
899 nf_bridge
->physoutdev
= NULL
;
900 br_handle_frame_finish(dev_net(skb
->dev
), NULL
, skb
);
903 static int br_nf_dev_xmit(struct sk_buff
*skb
)
905 if (skb
->nf_bridge
&& skb
->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
[NFPROTO_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");