2019-06-04 02:11:33 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-16 16:20:36 -06:00
|
|
|
/* Kernel module to match ESP parameters. */
|
|
|
|
|
|
|
|
/* (C) 1999-2000 Yon Uriarte <yon@astaro.de>
|
|
|
|
*/
|
2010-03-17 19:22:32 -06:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
2006-04-01 03:22:30 -07:00
|
|
|
#include <linux/in.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/ip.h>
|
|
|
|
|
2006-04-01 03:22:30 -07:00
|
|
|
#include <linux/netfilter/xt_esp.h>
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/netfilter_ipv4/ip_tables.h>
|
2006-04-01 03:22:30 -07:00
|
|
|
#include <linux/netfilter_ipv6/ip6_tables.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>");
|
2008-01-15 00:42:28 -07:00
|
|
|
MODULE_DESCRIPTION("Xtables: IPsec-ESP packet match");
|
2006-04-01 03:22:30 -07:00
|
|
|
MODULE_ALIAS("ipt_esp");
|
|
|
|
MODULE_ALIAS("ip6t_esp");
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* Returns 1 if the spi is matched by the range, 0 otherwise */
|
2007-07-07 23:15:35 -06:00
|
|
|
static inline bool
|
|
|
|
spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2007-07-07 23:15:35 -06:00
|
|
|
bool r;
|
2010-03-19 14:08:16 -06:00
|
|
|
pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
|
2010-03-17 19:22:32 -06:00
|
|
|
invert ? '!' : ' ', min, spi, max);
|
2006-04-01 03:22:30 -07:00
|
|
|
r = (spi >= min && spi <= max) ^ invert;
|
2010-03-17 19:22:32 -06:00
|
|
|
pr_debug(" result %s\n", r ? "PASS" : "FAILED");
|
2005-04-16 16:20:36 -06:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-07-07 12:42:08 -06:00
|
|
|
static bool esp_mt(const struct sk_buff *skb, struct xt_action_param *par)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-04-14 01:56:05 -06:00
|
|
|
const struct ip_esp_hdr *eh;
|
|
|
|
struct ip_esp_hdr _esp;
|
2008-10-08 03:35:18 -06:00
|
|
|
const struct xt_esp *espinfo = par->matchinfo;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* Must not be a fragment. */
|
2008-10-08 03:35:18 -06:00
|
|
|
if (par->fragoff != 0)
|
2007-07-07 23:15:35 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-10-08 03:35:18 -06:00
|
|
|
eh = skb_header_pointer(skb, par->thoff, sizeof(_esp), &_esp);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (eh == NULL) {
|
|
|
|
/* We've been asked to examine this packet, and we
|
|
|
|
* can't. Hence, no choice but to drop.
|
|
|
|
*/
|
2010-03-17 19:22:32 -06:00
|
|
|
pr_debug("Dropping evil ESP tinygram.\n");
|
2009-07-07 12:54:30 -06:00
|
|
|
par->hotdrop = true;
|
2007-07-07 23:15:35 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2006-04-01 03:22:30 -07:00
|
|
|
return spi_match(espinfo->spis[0], espinfo->spis[1], ntohl(eh->spi),
|
|
|
|
!!(espinfo->invflags & XT_ESP_INV_SPI));
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2010-03-19 10:16:42 -06:00
|
|
|
static int esp_mt_check(const struct xt_mtchk_param *par)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:18 -06:00
|
|
|
const struct xt_esp *espinfo = par->matchinfo;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-04-01 03:22:30 -07:00
|
|
|
if (espinfo->invflags & ~XT_ESP_INV_MASK) {
|
2010-03-17 19:22:32 -06:00
|
|
|
pr_debug("unknown flags %X\n", espinfo->invflags);
|
2010-03-23 09:35:56 -06:00
|
|
|
return -EINVAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2006-04-01 03:22:30 -07:00
|
|
|
|
2010-03-23 09:35:56 -06:00
|
|
|
return 0;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static struct xt_match esp_mt_reg[] __read_mostly = {
|
2006-08-22 01:34:04 -06:00
|
|
|
{
|
|
|
|
.name = "esp",
|
2008-10-08 03:35:01 -06:00
|
|
|
.family = NFPROTO_IPV4,
|
2007-12-05 00:24:03 -07:00
|
|
|
.checkentry = esp_mt_check,
|
|
|
|
.match = esp_mt,
|
2006-08-22 01:34:04 -06:00
|
|
|
.matchsize = sizeof(struct xt_esp),
|
|
|
|
.proto = IPPROTO_ESP,
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "esp",
|
2008-10-08 03:35:01 -06:00
|
|
|
.family = NFPROTO_IPV6,
|
2007-12-05 00:24:03 -07:00
|
|
|
.checkentry = esp_mt_check,
|
|
|
|
.match = esp_mt,
|
2006-08-22 01:34:04 -06:00
|
|
|
.matchsize = sizeof(struct xt_esp),
|
|
|
|
.proto = IPPROTO_ESP,
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
2006-04-01 03:22:30 -07:00
|
|
|
};
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static int __init esp_mt_init(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2007-12-05 00:24:03 -07:00
|
|
|
return xt_register_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg));
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static void __exit esp_mt_exit(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2007-12-05 00:24:03 -07:00
|
|
|
xt_unregister_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg));
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
module_init(esp_mt_init);
|
|
|
|
module_exit(esp_mt_exit);
|