1 // SPDX-License-Identifier: GPL-2.0-only
6 * Manohar Castelino <manohar.r.castelino@intel.com>
7 * Kuo-Lang Tseng <kuo-lang.tseng@intel.com>
8 * Jan Engelhardt <jengelh@medozas.de>
11 * This is just a modification of the IPv4 code written by
12 * Bart De Schuymer <bdschuym@pandora.be>
13 * with the changes required to support IPv6
17 #include <linux/ipv6.h>
20 #include <linux/module.h>
21 #include <net/dsfield.h>
22 #include <linux/netfilter/x_tables.h>
23 #include <linux/netfilter_bridge/ebtables.h>
24 #include <linux/netfilter_bridge/ebt_ip6.h>
38 ebt_ip6_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
40 const struct ebt_ip6_info
*info
= par
->matchinfo
;
41 const struct ipv6hdr
*ih6
;
43 const union pkthdr
*pptr
;
46 ih6
= skb_header_pointer(skb
, 0, sizeof(_ip6h
), &_ip6h
);
49 if ((info
->bitmask
& EBT_IP6_TCLASS
) &&
50 NF_INVF(info
, EBT_IP6_TCLASS
,
51 info
->tclass
!= ipv6_get_dsfield(ih6
)))
53 if (((info
->bitmask
& EBT_IP6_SOURCE
) &&
54 NF_INVF(info
, EBT_IP6_SOURCE
,
55 ipv6_masked_addr_cmp(&ih6
->saddr
, &info
->smsk
,
57 ((info
->bitmask
& EBT_IP6_DEST
) &&
58 NF_INVF(info
, EBT_IP6_DEST
,
59 ipv6_masked_addr_cmp(&ih6
->daddr
, &info
->dmsk
,
62 if (info
->bitmask
& EBT_IP6_PROTO
) {
63 uint8_t nexthdr
= ih6
->nexthdr
;
67 offset_ph
= ipv6_skip_exthdr(skb
, sizeof(_ip6h
), &nexthdr
, &frag_off
);
70 if (NF_INVF(info
, EBT_IP6_PROTO
, info
->protocol
!= nexthdr
))
72 if (!(info
->bitmask
& (EBT_IP6_DPORT
|
73 EBT_IP6_SPORT
| EBT_IP6_ICMP6
)))
76 /* min icmpv6 headersize is 4, so sizeof(_pkthdr) is ok. */
77 pptr
= skb_header_pointer(skb
, offset_ph
, sizeof(_pkthdr
),
81 if (info
->bitmask
& EBT_IP6_DPORT
) {
82 u16 dst
= ntohs(pptr
->tcpudphdr
.dst
);
83 if (NF_INVF(info
, EBT_IP6_DPORT
,
84 dst
< info
->dport
[0] ||
85 dst
> info
->dport
[1]))
88 if (info
->bitmask
& EBT_IP6_SPORT
) {
89 u16 src
= ntohs(pptr
->tcpudphdr
.src
);
90 if (NF_INVF(info
, EBT_IP6_SPORT
,
91 src
< info
->sport
[0] ||
92 src
> info
->sport
[1]))
95 if ((info
->bitmask
& EBT_IP6_ICMP6
) &&
96 NF_INVF(info
, EBT_IP6_ICMP6
,
97 pptr
->icmphdr
.type
< info
->icmpv6_type
[0] ||
98 pptr
->icmphdr
.type
> info
->icmpv6_type
[1] ||
99 pptr
->icmphdr
.code
< info
->icmpv6_code
[0] ||
100 pptr
->icmphdr
.code
> info
->icmpv6_code
[1]))
106 static int ebt_ip6_mt_check(const struct xt_mtchk_param
*par
)
108 const struct ebt_entry
*e
= par
->entryinfo
;
109 struct ebt_ip6_info
*info
= par
->matchinfo
;
111 if (e
->ethproto
!= htons(ETH_P_IPV6
) || e
->invflags
& EBT_IPROTO
)
113 if (info
->bitmask
& ~EBT_IP6_MASK
|| info
->invflags
& ~EBT_IP6_MASK
)
115 if (info
->bitmask
& (EBT_IP6_DPORT
| EBT_IP6_SPORT
)) {
116 if (info
->invflags
& EBT_IP6_PROTO
)
118 if (info
->protocol
!= IPPROTO_TCP
&&
119 info
->protocol
!= IPPROTO_UDP
&&
120 info
->protocol
!= IPPROTO_UDPLITE
&&
121 info
->protocol
!= IPPROTO_SCTP
&&
122 info
->protocol
!= IPPROTO_DCCP
)
125 if (info
->bitmask
& EBT_IP6_DPORT
&& info
->dport
[0] > info
->dport
[1])
127 if (info
->bitmask
& EBT_IP6_SPORT
&& info
->sport
[0] > info
->sport
[1])
129 if (info
->bitmask
& EBT_IP6_ICMP6
) {
130 if ((info
->invflags
& EBT_IP6_PROTO
) ||
131 info
->protocol
!= IPPROTO_ICMPV6
)
133 if (info
->icmpv6_type
[0] > info
->icmpv6_type
[1] ||
134 info
->icmpv6_code
[0] > info
->icmpv6_code
[1])
140 static struct xt_match ebt_ip6_mt_reg __read_mostly
= {
143 .family
= NFPROTO_BRIDGE
,
145 .checkentry
= ebt_ip6_mt_check
,
146 .matchsize
= sizeof(struct ebt_ip6_info
),
150 static int __init
ebt_ip6_init(void)
152 return xt_register_match(&ebt_ip6_mt_reg
);
155 static void __exit
ebt_ip6_fini(void)
157 xt_unregister_match(&ebt_ip6_mt_reg
);
160 module_init(ebt_ip6_init
);
161 module_exit(ebt_ip6_fini
);
162 MODULE_DESCRIPTION("Ebtables: IPv6 protocol packet match");
163 MODULE_AUTHOR("Kuo-Lang Tseng <kuo-lang.tseng@intel.com>");
164 MODULE_LICENSE("GPL");