2019-06-04 08:11:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-16 22:20:36 +00:00
|
|
|
/* ipv6header match - matches IPv6 packets based
|
|
|
|
on whether they contain certain headers */
|
|
|
|
|
2007-02-09 14:24:49 +00:00
|
|
|
/* Original idea: Brad Chapman
|
2005-04-16 22:20:36 +00:00
|
|
|
* Rewritten by: Andras Kis-Szabo <kisza@sch.bme.hu> */
|
|
|
|
|
|
|
|
/* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/ipv6.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <net/checksum.h>
|
|
|
|
#include <net/ipv6.h>
|
|
|
|
|
2007-02-07 23:11:19 +00:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
2019-09-13 08:13:07 +00:00
|
|
|
#include <linux/netfilter_ipv6.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/netfilter_ipv6/ip6t_ipv6header.h>
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
2008-01-15 07:42:28 +00:00
|
|
|
MODULE_DESCRIPTION("Xtables: IPv6 header types match");
|
2005-04-16 22:20:36 +00:00
|
|
|
MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
|
|
|
|
|
2007-07-08 05:15:35 +00:00
|
|
|
static bool
|
2009-07-07 18:42:08 +00:00
|
|
|
ipv6header_mt6(const struct sk_buff *skb, struct xt_action_param *par)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-10-08 09:35:18 +00:00
|
|
|
const struct ip6t_ipv6header_info *info = par->matchinfo;
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned int temp;
|
|
|
|
int len;
|
|
|
|
u8 nexthdr;
|
|
|
|
unsigned int ptr;
|
|
|
|
|
|
|
|
/* Make sure this isn't an evil packet */
|
|
|
|
|
|
|
|
/* type of the 1st exthdr */
|
2007-04-26 00:54:47 +00:00
|
|
|
nexthdr = ipv6_hdr(skb)->nexthdr;
|
2005-04-16 22:20:36 +00:00
|
|
|
/* pointer to the 1st exthdr */
|
|
|
|
ptr = sizeof(struct ipv6hdr);
|
|
|
|
/* available length */
|
|
|
|
len = skb->len - ptr;
|
|
|
|
temp = 0;
|
|
|
|
|
2019-09-13 08:13:07 +00:00
|
|
|
while (nf_ip6_ext_hdr(nexthdr)) {
|
2008-04-14 07:56:05 +00:00
|
|
|
const struct ipv6_opt_hdr *hp;
|
|
|
|
struct ipv6_opt_hdr _hdr;
|
2006-01-17 10:39:39 +00:00
|
|
|
int hdrlen;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* No more exthdr -> evaluate */
|
2006-01-17 10:39:39 +00:00
|
|
|
if (nexthdr == NEXTHDR_NONE) {
|
2005-04-16 22:20:36 +00:00
|
|
|
temp |= MASK_NONE;
|
|
|
|
break;
|
|
|
|
}
|
2009-05-05 13:32:16 +00:00
|
|
|
/* Is there enough space for the next ext header? */
|
|
|
|
if (len < (int)sizeof(struct ipv6_opt_hdr))
|
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
/* ESP -> evaluate */
|
2006-01-17 10:39:39 +00:00
|
|
|
if (nexthdr == NEXTHDR_ESP) {
|
2005-04-16 22:20:36 +00:00
|
|
|
temp |= MASK_ESP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
|
2018-09-04 14:01:57 +00:00
|
|
|
if (!hp) {
|
|
|
|
par->hotdrop = true;
|
|
|
|
return false;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Calculate the header length */
|
2007-07-08 05:19:08 +00:00
|
|
|
if (nexthdr == NEXTHDR_FRAGMENT)
|
2006-01-17 10:39:39 +00:00
|
|
|
hdrlen = 8;
|
2007-07-08 05:19:08 +00:00
|
|
|
else if (nexthdr == NEXTHDR_AUTH)
|
2019-07-10 13:14:10 +00:00
|
|
|
hdrlen = ipv6_authlen(hp);
|
2006-01-17 10:39:39 +00:00
|
|
|
else
|
|
|
|
hdrlen = ipv6_optlen(hp);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* set the flag */
|
2006-01-17 10:39:39 +00:00
|
|
|
switch (nexthdr) {
|
|
|
|
case NEXTHDR_HOP:
|
|
|
|
temp |= MASK_HOPOPTS;
|
|
|
|
break;
|
|
|
|
case NEXTHDR_ROUTING:
|
|
|
|
temp |= MASK_ROUTING;
|
|
|
|
break;
|
|
|
|
case NEXTHDR_FRAGMENT:
|
|
|
|
temp |= MASK_FRAGMENT;
|
|
|
|
break;
|
|
|
|
case NEXTHDR_AUTH:
|
|
|
|
temp |= MASK_AH;
|
|
|
|
break;
|
|
|
|
case NEXTHDR_DEST:
|
|
|
|
temp |= MASK_DSTOPTS;
|
|
|
|
break;
|
|
|
|
default:
|
2007-07-08 05:15:35 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-01-17 10:39:39 +00:00
|
|
|
nexthdr = hp->nexthdr;
|
|
|
|
len -= hdrlen;
|
|
|
|
ptr += hdrlen;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (ptr > skb->len)
|
|
|
|
break;
|
2006-01-17 10:39:39 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-07-08 05:19:08 +00:00
|
|
|
if (nexthdr != NEXTHDR_NONE && nexthdr != NEXTHDR_ESP)
|
2005-04-16 22:20:36 +00:00
|
|
|
temp |= MASK_PROTO;
|
|
|
|
|
|
|
|
if (info->modeflag)
|
|
|
|
return !((temp ^ info->matchflags ^ info->invflags)
|
|
|
|
& info->matchflags);
|
|
|
|
else {
|
|
|
|
if (info->invflags)
|
|
|
|
return temp != info->matchflags;
|
|
|
|
else
|
|
|
|
return temp == info->matchflags;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-19 16:16:42 +00:00
|
|
|
static int ipv6header_mt6_check(const struct xt_mtchk_param *par)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-10-08 09:35:18 +00:00
|
|
|
const struct ip6t_ipv6header_info *info = par->matchinfo;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* invflags is 0 or 0xff in hard mode */
|
2006-01-17 10:39:39 +00:00
|
|
|
if ((!info->modeflag) && info->invflags != 0x00 &&
|
|
|
|
info->invflags != 0xFF)
|
2010-03-23 15:35:56 +00:00
|
|
|
return -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2010-03-23 15:35:56 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static struct xt_match ipv6header_mt6_reg __read_mostly = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.name = "ipv6header",
|
2008-10-08 09:35:01 +00:00
|
|
|
.family = NFPROTO_IPV6,
|
2007-12-05 07:24:03 +00:00
|
|
|
.match = ipv6header_mt6,
|
2006-03-21 02:01:43 +00:00
|
|
|
.matchsize = sizeof(struct ip6t_ipv6header_info),
|
2007-12-05 07:24:03 +00:00
|
|
|
.checkentry = ipv6header_mt6_check,
|
2005-04-16 22:20:36 +00:00
|
|
|
.destroy = NULL,
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static int __init ipv6header_mt6_init(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-12-05 07:24:03 +00:00
|
|
|
return xt_register_match(&ipv6header_mt6_reg);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static void __exit ipv6header_mt6_exit(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-12-05 07:24:03 +00:00
|
|
|
xt_unregister_match(&ipv6header_mt6_reg);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
module_init(ipv6header_mt6_init);
|
|
|
|
module_exit(ipv6header_mt6_exit);
|