17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
21 #include <linux/netdevice.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>
39 #include <asm/uaccess.h>
42 #include <linux/sysctl.h>
45 #define skb_origaddr(skb) (((struct bridge_skb_cb *) \
46 (skb->nf_bridge->data))->daddr.ipv4)
47 #define store_orig_dstaddr(skb) (skb_origaddr(skb) = ip_hdr(skb)->daddr)
48 #define dnat_took_place(skb) (skb_origaddr(skb) != ip_hdr(skb)->daddr)
59 #define brnf_call_iptables 1
60 #define brnf_call_ip6tables 1
61 #define brnf_call_arptables 1
62 #define brnf_filter_vlan_tagged 0
63 #define brnf_filter_pppoe_tagged 0
64 #define brnf_pass_vlan_indev 0
68 (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
70 #define IS_IPV6(skb) \
71 (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
74 (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
81 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
86 #define IS_VLAN_IP(skb) \
87 (vlan_proto(skb) == htons(ETH_P_IP) && \
88 brnf_filter_vlan_tagged)
90 #define IS_VLAN_IPV6(skb) \
91 (vlan_proto(skb) == htons(ETH_P_IPV6) && \
92 brnf_filter_vlan_tagged)
94 #define IS_VLAN_ARP(skb) \
95 (vlan_proto(skb) == htons(ETH_P_ARP) && \
96 brnf_filter_vlan_tagged)
104 #define IS_PPPOE_IP(skb) \
105 (skb->protocol == htons(ETH_P_PPP_SES) && \
106 pppoe_proto(skb) == htons(PPP_IP) && \
107 brnf_filter_pppoe_tagged)
109 #define IS_PPPOE_IPV6(skb) \
110 (skb->protocol == htons(ETH_P_PPP_SES) && \
111 pppoe_proto(skb) == htons(PPP_IPV6) && \
112 brnf_filter_pppoe_tagged)
136 static unsigned int fake_mtu(
const struct dst_entry *
dst)
138 return dst->
dev->mtu;
141 static struct dst_ops fake_dst_ops = {
144 .update_pmtu = fake_update_pmtu,
145 .redirect = fake_redirect,
146 .cow_metrics = fake_cow_metrics,
147 .neigh_lookup = fake_neigh_lookup,
158 static const u32 br_dst_default_metrics[
RTAX_MAX] = {
164 struct rtable *rt = &br->fake_rtable;
169 dst_init_metrics(&rt->
dst, br_dst_default_metrics,
true);
171 rt->
dst.ops = &fake_dst_ops;
178 port = br_port_get_rcu(dev);
179 return port ? &port->
br->fake_rtable :
NULL;
186 port = br_port_get_rcu(dev);
187 return port ? port->
br->dev :
NULL;
190 static inline struct nf_bridge_info *nf_bridge_alloc(
struct sk_buff *
skb)
192 skb->nf_bridge = kzalloc(
sizeof(
struct nf_bridge_info),
GFP_ATOMIC);
193 if (
likely(skb->nf_bridge))
196 return skb->nf_bridge;
199 static inline struct nf_bridge_info *nf_bridge_unshare(
struct sk_buff *skb)
201 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
204 struct nf_bridge_info *
tmp = nf_bridge_alloc(skb);
207 memcpy(tmp, nf_bridge,
sizeof(
struct nf_bridge_info));
210 nf_bridge_put(nf_bridge);
216 static inline void nf_bridge_push_encap_header(
struct sk_buff *skb)
218 unsigned int len = nf_bridge_encap_header_len(skb);
224 static inline void nf_bridge_pull_encap_header(
struct sk_buff *skb)
226 unsigned int len = nf_bridge_encap_header_len(skb);
232 static inline void nf_bridge_pull_encap_header_rcsum(
struct sk_buff *skb)
234 unsigned int len = nf_bridge_encap_header_len(skb);
240 static inline void nf_bridge_save_header(
struct sk_buff *skb)
244 skb_copy_from_linear_data_offset(skb, -header_size,
245 skb->nf_bridge->
data, header_size);
248 static inline void nf_bridge_update_protocol(
struct sk_buff *skb)
250 if (skb->nf_bridge->mask & BRNF_8021Q)
252 else if (skb->nf_bridge->mask & BRNF_PPPoE)
261 static int br_parse_ip_options(
struct sk_buff *skb)
264 const struct iphdr *iph;
268 if (!pskb_may_pull(skb,
sizeof(
struct iphdr)))
272 opt = &(
IPCB(skb)->opt);
275 if (iph->ihl < 5 || iph->version != 4)
278 if (!pskb_may_pull(skb, iph->ihl*4))
286 if (skb->
len < len) {
289 }
else if (len < (iph->ihl*4))
292 if (pskb_trim_rcsum(skb, len)) {
307 struct in_device *in_dev = __in_dev_get_rcu(dev);
308 if (in_dev && !IN_DEV_SOURCE_ROUTE(in_dev))
331 nf_bridge_update_protocol(skb);
332 header_size =
ETH_HLEN + nf_bridge_encap_header_len(skb);
333 err = skb_cow_head(skb, header_size);
337 skb_copy_to_linear_data_offset(skb, -header_size,
338 skb->nf_bridge->
data, header_size);
339 __skb_push(skb, nf_bridge_encap_header_len(skb));
346 static int br_nf_pre_routing_finish_ipv6(
struct sk_buff *skb)
348 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
351 if (nf_bridge->mask & BRNF_PKT_TYPE) {
353 nf_bridge->mask ^= BRNF_PKT_TYPE;
355 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
357 rt = bridge_parent_rtable(nf_bridge->physindev);
364 skb->
dev = nf_bridge->physindev;
365 nf_bridge_update_protocol(skb);
366 nf_bridge_push_encap_header(skb);
378 static int br_nf_pre_routing_finish_bridge(
struct sk_buff *skb)
380 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
384 skb->
dev = bridge_parent(skb->
dev);
388 neigh = dst_neigh_lookup_skb(dst, skb);
392 if (neigh->
hh.hh_len) {
393 neigh_hh_bridge(&neigh->
hh, skb);
394 skb->
dev = nf_bridge->physindev;
401 skb_copy_from_linear_data_offset(skb,
403 skb->nf_bridge->
data,
406 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
407 ret = neigh->
output(neigh, skb);
409 neigh_release(neigh);
455 static int br_nf_pre_routing_finish(
struct sk_buff *skb)
458 struct iphdr *iph = ip_hdr(skb);
459 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
463 if (nf_bridge->mask & BRNF_PKT_TYPE) {
465 nf_bridge->mask ^= BRNF_PKT_TYPE;
467 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
469 if ((err = ip_route_input(skb, iph->
daddr, iph->
saddr, iph->
tos, dev))) {
470 struct in_device *in_dev = __in_dev_get_rcu(dev);
479 if (err != -
EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
482 rt = ip_route_output(dev_net(dev), iph->
daddr, 0,
487 if (rt->
dst.dev == dev) {
488 skb_dst_set(skb, &rt->
dst);
497 if (skb_dst(skb)->dev == dev) {
499 skb->
dev = nf_bridge->physindev;
500 nf_bridge_update_protocol(skb);
501 nf_bridge_push_encap_header(skb);
505 br_nf_pre_routing_finish_bridge,
513 rt = bridge_parent_rtable(nf_bridge->physindev);
521 skb->
dev = nf_bridge->physindev;
522 nf_bridge_update_protocol(skb);
523 nf_bridge_push_encap_header(skb);
534 br = bridge_parent(dev);
540 return vlan ? vlan : br;
546 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
550 nf_bridge->mask |= BRNF_PKT_TYPE;
553 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
554 nf_bridge->physindev = skb->
dev;
555 skb->
dev = brnf_get_logical_dev(skb, skb->
dev);
557 nf_bridge->mask |= BRNF_8021Q;
559 nf_bridge->mask |= BRNF_PPPoE;
565 static int check_hbh_len(
struct sk_buff *skb)
567 unsigned char *
raw = (
u8 *)(ipv6_hdr(skb) + 1);
569 const unsigned char *nh = skb_network_header(skb);
571 int len = (raw[1] + 1) << 3;
573 if ((raw + len) - skb->
data > skb_headlen(skb))
580 int optlen = nh[off + 1] + 2;
591 if (nh[off + 1] != 4 || (off & 3) != 2)
597 if (pkt_len > skb->
len -
sizeof(
struct ipv6hdr))
599 if (pskb_trim_rcsum(skb,
600 pkt_len +
sizeof(
struct ipv6hdr)))
602 nh = skb_network_header(skb);
621 static unsigned int br_nf_pre_routing_ipv6(
unsigned int hook,
633 if (!pskb_may_pull(skb,
sizeof(
struct ipv6hdr)))
638 if (hdr->version != 6)
644 if (pkt_len +
sizeof(
struct ipv6hdr) > skb->
len)
646 if (pskb_trim_rcsum(skb, pkt_len +
sizeof(
struct ipv6hdr)))
652 nf_bridge_put(skb->nf_bridge);
653 if (!nf_bridge_alloc(skb))
655 if (!setup_pre_routing(skb))
660 br_nf_pre_routing_finish_ipv6);
671 static unsigned int br_nf_pre_routing(
unsigned int hook,
struct sk_buff *skb,
678 __u32 len = nf_bridge_encap_header_len(skb);
680 if (
unlikely(!pskb_may_pull(skb, len)))
683 p = br_port_get_rcu(in);
692 nf_bridge_pull_encap_header_rcsum(skb);
693 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
702 nf_bridge_pull_encap_header_rcsum(skb);
704 if (br_parse_ip_options(skb))
707 nf_bridge_put(skb->nf_bridge);
708 if (!nf_bridge_alloc(skb))
710 if (!setup_pre_routing(skb))
716 br_nf_pre_routing_finish);
729 static unsigned int br_nf_local_in(
unsigned int hook,
struct sk_buff *skb,
739 static int br_nf_forward_finish(
struct sk_buff *skb)
741 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
745 in = nf_bridge->physindev;
746 if (nf_bridge->mask & BRNF_PKT_TYPE) {
748 nf_bridge->mask ^= BRNF_PKT_TYPE;
750 nf_bridge_update_protocol(skb);
754 nf_bridge_push_encap_header(skb);
767 static unsigned int br_nf_forward_ip(
unsigned int hook,
struct sk_buff *skb,
772 struct nf_bridge_info *nf_bridge;
781 if (!nf_bridge_unshare(skb))
784 parent = bridge_parent(out);
795 nf_bridge_pull_encap_header(skb);
797 nf_bridge = skb->nf_bridge;
800 nf_bridge->mask |= BRNF_PKT_TYPE;
807 nf_bridge->mask |= BRNF_BRIDGED;
808 nf_bridge->physoutdev = skb->
dev;
815 br_nf_forward_finish);
820 static unsigned int br_nf_forward_arp(
unsigned int hook,
struct sk_buff *skb,
829 p = br_port_get_rcu(out);
840 nf_bridge_pull_encap_header(skb);
843 if (arp_hdr(skb)->ar_pln != 4) {
845 nf_bridge_push_encap_header(skb);
850 (
struct net_device *)out, br_nf_forward_finish);
855 #if IS_ENABLED(CONFIG_NF_CONNTRACK_IPV4)
856 static int br_nf_dev_queue_xmit(
struct sk_buff *skb)
861 skb->
len + nf_bridge_mtu_reduction(skb) > skb->
dev->mtu &&
863 if (br_parse_ip_options(skb))
873 static int br_nf_dev_queue_xmit(
struct sk_buff *skb)
880 static unsigned int br_nf_post_routing(
unsigned int hook,
struct sk_buff *skb,
885 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
889 if (!nf_bridge || !(nf_bridge->mask & BRNF_BRIDGED))
906 nf_bridge->mask |= BRNF_PKT_TYPE;
909 nf_bridge_pull_encap_header(skb);
910 nf_bridge_save_header(skb);
917 br_nf_dev_queue_xmit);
925 static unsigned int ip_sabotage_in(
unsigned int hook,
struct sk_buff *skb,
930 if (skb->nf_bridge &&
931 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
942 .hook = br_nf_pre_routing,
949 .hook = br_nf_local_in,
956 .hook = br_nf_forward_ip,
963 .hook = br_nf_forward_arp,
970 .hook = br_nf_post_routing,
977 .hook = ip_sabotage_in,
984 .hook = ip_sabotage_in,
995 void __user *
buffer,
size_t * lenp, loff_t * ppos)
1001 if (write && *(
int *)(ctl->
data))
1002 *(
int *)(ctl->
data) = 1;
1008 .
procname =
"bridge-nf-call-arptables",
1010 .maxlen =
sizeof(
int),
1015 .procname =
"bridge-nf-call-iptables",
1017 .maxlen =
sizeof(
int),
1022 .procname =
"bridge-nf-call-ip6tables",
1024 .maxlen =
sizeof(
int),
1029 .procname =
"bridge-nf-filter-vlan-tagged",
1031 .maxlen =
sizeof(
int),
1036 .procname =
"bridge-nf-filter-pppoe-tagged",
1038 .maxlen =
sizeof(
int),
1043 .procname =
"bridge-nf-pass-vlan-input-dev",
1045 .maxlen =
sizeof(
int),
1057 ret = dst_entries_init(&fake_dst_ops);
1063 dst_entries_destroy(&fake_dst_ops);
1066 #ifdef CONFIG_SYSCTL
1068 if (brnf_sysctl_header ==
NULL) {
1070 "br_netfilter: can't register to sysctl.\n");
1072 dst_entries_destroy(&fake_dst_ops);
1083 #ifdef CONFIG_SYSCTL
1086 dst_entries_destroy(&fake_dst_ops);