2019-05-19 06:08:20 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2008-06-09 16:55:45 -06:00
|
|
|
/*
|
|
|
|
* ebt_ip6
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Manohar Castelino <manohar.r.castelino@intel.com>
|
|
|
|
* Kuo-Lang Tseng <kuo-lang.tseng@intel.com>
|
2010-02-28 15:19:52 -07:00
|
|
|
* Jan Engelhardt <jengelh@medozas.de>
|
2008-06-09 16:55:45 -06:00
|
|
|
*
|
|
|
|
* Summary:
|
|
|
|
* This is just a modification of the IPv4 code written by
|
|
|
|
* Bart De Schuymer <bdschuym@pandora.be>
|
|
|
|
* with the changes required to support IPv6
|
|
|
|
*
|
|
|
|
* Jan, 2008
|
|
|
|
*/
|
|
|
|
#include <linux/ipv6.h>
|
|
|
|
#include <net/ipv6.h>
|
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <net/dsfield.h>
|
2008-10-08 03:35:13 -06:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
#include <linux/netfilter_bridge/ebtables.h>
|
|
|
|
#include <linux/netfilter_bridge/ebt_ip6.h>
|
2008-06-09 16:55:45 -06:00
|
|
|
|
2010-12-20 07:57:47 -07:00
|
|
|
union pkthdr {
|
|
|
|
struct {
|
|
|
|
__be16 src;
|
|
|
|
__be16 dst;
|
|
|
|
} tcpudphdr;
|
|
|
|
struct {
|
|
|
|
u8 type;
|
|
|
|
u8 code;
|
|
|
|
} icmphdr;
|
2008-06-09 16:55:45 -06:00
|
|
|
};
|
|
|
|
|
2008-10-08 03:35:15 -06:00
|
|
|
static bool
|
2009-07-07 12:42:08 -06:00
|
|
|
ebt_ip6_mt(const struct sk_buff *skb, struct xt_action_param *par)
|
2008-06-09 16:55:45 -06:00
|
|
|
{
|
2008-10-08 03:35:18 -06:00
|
|
|
const struct ebt_ip6_info *info = par->matchinfo;
|
2008-06-09 16:55:45 -06:00
|
|
|
const struct ipv6hdr *ih6;
|
|
|
|
struct ipv6hdr _ip6h;
|
2010-12-20 07:57:47 -07:00
|
|
|
const union pkthdr *pptr;
|
|
|
|
union pkthdr _pkthdr;
|
2008-06-09 16:55:45 -06:00
|
|
|
|
|
|
|
ih6 = skb_header_pointer(skb, 0, sizeof(_ip6h), &_ip6h);
|
|
|
|
if (ih6 == NULL)
|
2008-10-08 03:35:13 -06:00
|
|
|
return false;
|
2016-06-24 14:25:22 -06:00
|
|
|
if ((info->bitmask & EBT_IP6_TCLASS) &&
|
|
|
|
NF_INVF(info, EBT_IP6_TCLASS,
|
|
|
|
info->tclass != ipv6_get_dsfield(ih6)))
|
2008-10-08 03:35:13 -06:00
|
|
|
return false;
|
2016-06-24 14:25:22 -06:00
|
|
|
if (((info->bitmask & EBT_IP6_SOURCE) &&
|
|
|
|
NF_INVF(info, EBT_IP6_SOURCE,
|
|
|
|
ipv6_masked_addr_cmp(&ih6->saddr, &info->smsk,
|
|
|
|
&info->saddr))) ||
|
|
|
|
((info->bitmask & EBT_IP6_DEST) &&
|
|
|
|
NF_INVF(info, EBT_IP6_DEST,
|
|
|
|
ipv6_masked_addr_cmp(&ih6->daddr, &info->dmsk,
|
|
|
|
&info->daddr))))
|
2008-10-08 03:35:13 -06:00
|
|
|
return false;
|
2008-06-09 16:55:45 -06:00
|
|
|
if (info->bitmask & EBT_IP6_PROTO) {
|
|
|
|
uint8_t nexthdr = ih6->nexthdr;
|
2011-11-30 18:05:51 -07:00
|
|
|
__be16 frag_off;
|
2008-06-09 16:55:45 -06:00
|
|
|
int offset_ph;
|
|
|
|
|
2011-11-30 18:05:51 -07:00
|
|
|
offset_ph = ipv6_skip_exthdr(skb, sizeof(_ip6h), &nexthdr, &frag_off);
|
2008-06-09 16:55:45 -06:00
|
|
|
if (offset_ph == -1)
|
2008-10-08 03:35:13 -06:00
|
|
|
return false;
|
2016-06-24 14:25:22 -06:00
|
|
|
if (NF_INVF(info, EBT_IP6_PROTO, info->protocol != nexthdr))
|
2008-10-08 03:35:13 -06:00
|
|
|
return false;
|
2015-10-26 03:10:43 -06:00
|
|
|
if (!(info->bitmask & (EBT_IP6_DPORT |
|
|
|
|
EBT_IP6_SPORT | EBT_IP6_ICMP6)))
|
2008-10-08 03:35:13 -06:00
|
|
|
return true;
|
2010-12-20 07:57:47 -07:00
|
|
|
|
|
|
|
/* min icmpv6 headersize is 4, so sizeof(_pkthdr) is ok. */
|
|
|
|
pptr = skb_header_pointer(skb, offset_ph, sizeof(_pkthdr),
|
|
|
|
&_pkthdr);
|
2008-06-09 16:55:45 -06:00
|
|
|
if (pptr == NULL)
|
2008-10-08 03:35:13 -06:00
|
|
|
return false;
|
2008-06-09 16:55:45 -06:00
|
|
|
if (info->bitmask & EBT_IP6_DPORT) {
|
2010-12-20 07:57:47 -07:00
|
|
|
u16 dst = ntohs(pptr->tcpudphdr.dst);
|
2016-06-24 14:25:22 -06:00
|
|
|
if (NF_INVF(info, EBT_IP6_DPORT,
|
|
|
|
dst < info->dport[0] ||
|
|
|
|
dst > info->dport[1]))
|
2008-10-08 03:35:13 -06:00
|
|
|
return false;
|
2008-06-09 16:55:45 -06:00
|
|
|
}
|
|
|
|
if (info->bitmask & EBT_IP6_SPORT) {
|
2010-12-20 07:57:47 -07:00
|
|
|
u16 src = ntohs(pptr->tcpudphdr.src);
|
2016-06-24 14:25:22 -06:00
|
|
|
if (NF_INVF(info, EBT_IP6_SPORT,
|
|
|
|
src < info->sport[0] ||
|
|
|
|
src > info->sport[1]))
|
2017-08-15 03:50:34 -06:00
|
|
|
return false;
|
2008-06-09 16:55:45 -06:00
|
|
|
}
|
2010-12-20 07:57:47 -07:00
|
|
|
if ((info->bitmask & EBT_IP6_ICMP6) &&
|
2016-06-24 14:25:22 -06:00
|
|
|
NF_INVF(info, EBT_IP6_ICMP6,
|
|
|
|
pptr->icmphdr.type < info->icmpv6_type[0] ||
|
|
|
|
pptr->icmphdr.type > info->icmpv6_type[1] ||
|
|
|
|
pptr->icmphdr.code < info->icmpv6_code[0] ||
|
|
|
|
pptr->icmphdr.code > info->icmpv6_code[1]))
|
2010-12-20 07:57:47 -07:00
|
|
|
return false;
|
2008-06-09 16:55:45 -06:00
|
|
|
}
|
2008-10-08 03:35:13 -06:00
|
|
|
return true;
|
2008-06-09 16:55:45 -06:00
|
|
|
}
|
|
|
|
|
2010-03-19 10:16:42 -06:00
|
|
|
static int ebt_ip6_mt_check(const struct xt_mtchk_param *par)
|
2008-06-09 16:55:45 -06:00
|
|
|
{
|
2008-10-08 03:35:18 -06:00
|
|
|
const struct ebt_entry *e = par->entryinfo;
|
|
|
|
struct ebt_ip6_info *info = par->matchinfo;
|
2008-06-09 16:55:45 -06:00
|
|
|
|
|
|
|
if (e->ethproto != htons(ETH_P_IPV6) || e->invflags & EBT_IPROTO)
|
2010-03-23 09:35:56 -06:00
|
|
|
return -EINVAL;
|
2008-06-09 16:55:45 -06:00
|
|
|
if (info->bitmask & ~EBT_IP6_MASK || info->invflags & ~EBT_IP6_MASK)
|
2010-03-23 09:35:56 -06:00
|
|
|
return -EINVAL;
|
2008-06-09 16:55:45 -06:00
|
|
|
if (info->bitmask & (EBT_IP6_DPORT | EBT_IP6_SPORT)) {
|
|
|
|
if (info->invflags & EBT_IP6_PROTO)
|
2010-03-23 09:35:56 -06:00
|
|
|
return -EINVAL;
|
2008-06-09 16:55:45 -06:00
|
|
|
if (info->protocol != IPPROTO_TCP &&
|
|
|
|
info->protocol != IPPROTO_UDP &&
|
|
|
|
info->protocol != IPPROTO_UDPLITE &&
|
|
|
|
info->protocol != IPPROTO_SCTP &&
|
|
|
|
info->protocol != IPPROTO_DCCP)
|
2010-03-23 09:35:56 -06:00
|
|
|
return -EINVAL;
|
2008-06-09 16:55:45 -06:00
|
|
|
}
|
|
|
|
if (info->bitmask & EBT_IP6_DPORT && info->dport[0] > info->dport[1])
|
2010-03-23 09:35:56 -06:00
|
|
|
return -EINVAL;
|
2008-06-09 16:55:45 -06:00
|
|
|
if (info->bitmask & EBT_IP6_SPORT && info->sport[0] > info->sport[1])
|
2010-03-23 09:35:56 -06:00
|
|
|
return -EINVAL;
|
2010-12-20 07:57:47 -07:00
|
|
|
if (info->bitmask & EBT_IP6_ICMP6) {
|
|
|
|
if ((info->invflags & EBT_IP6_PROTO) ||
|
|
|
|
info->protocol != IPPROTO_ICMPV6)
|
|
|
|
return -EINVAL;
|
|
|
|
if (info->icmpv6_type[0] > info->icmpv6_type[1] ||
|
|
|
|
info->icmpv6_code[0] > info->icmpv6_code[1])
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-03-23 09:35:56 -06:00
|
|
|
return 0;
|
2008-06-09 16:55:45 -06:00
|
|
|
}
|
|
|
|
|
2008-10-08 03:35:15 -06:00
|
|
|
static struct xt_match ebt_ip6_mt_reg __read_mostly = {
|
|
|
|
.name = "ip6",
|
2008-10-08 03:35:14 -06:00
|
|
|
.revision = 0,
|
|
|
|
.family = NFPROTO_BRIDGE,
|
2008-10-08 03:35:15 -06:00
|
|
|
.match = ebt_ip6_mt,
|
|
|
|
.checkentry = ebt_ip6_mt_check,
|
2010-02-15 10:16:26 -07:00
|
|
|
.matchsize = sizeof(struct ebt_ip6_info),
|
2008-06-09 16:55:45 -06:00
|
|
|
.me = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init ebt_ip6_init(void)
|
|
|
|
{
|
2008-10-08 03:35:15 -06:00
|
|
|
return xt_register_match(&ebt_ip6_mt_reg);
|
2008-06-09 16:55:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit ebt_ip6_fini(void)
|
|
|
|
{
|
2008-10-08 03:35:15 -06:00
|
|
|
xt_unregister_match(&ebt_ip6_mt_reg);
|
2008-06-09 16:55:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(ebt_ip6_init);
|
|
|
|
module_exit(ebt_ip6_fini);
|
|
|
|
MODULE_DESCRIPTION("Ebtables: IPv6 protocol packet match");
|
2010-02-28 15:22:04 -07:00
|
|
|
MODULE_AUTHOR("Kuo-Lang Tseng <kuo-lang.tseng@intel.com>");
|
2008-06-09 16:55:45 -06:00
|
|
|
MODULE_LICENSE("GPL");
|