2019-06-04 02:11:33 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2006-03-20 19:03:40 -07:00
|
|
|
/* IP tables module for matching IPsec policy
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004,2005 Patrick McHardy, <kaber@trash.net>
|
|
|
|
*/
|
2010-03-17 09:04:40 -06:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
2006-03-20 19:03:40 -07:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <net/xfrm.h>
|
|
|
|
|
2008-01-15 00:42:06 -07:00
|
|
|
#include <linux/netfilter.h>
|
2006-03-20 19:03:40 -07:00
|
|
|
#include <linux/netfilter/xt_policy.h>
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
|
2008-01-15 00:42:28 -07:00
|
|
|
MODULE_DESCRIPTION("Xtables: IPsec policy match");
|
2006-03-20 19:03:40 -07:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
2007-07-07 23:15:35 -06:00
|
|
|
static inline bool
|
2008-01-15 00:42:06 -07:00
|
|
|
xt_addr_cmp(const union nf_inet_addr *a1, const union nf_inet_addr *m,
|
|
|
|
const union nf_inet_addr *a2, unsigned short family)
|
2006-03-20 19:03:40 -07:00
|
|
|
{
|
|
|
|
switch (family) {
|
2008-10-08 03:35:01 -06:00
|
|
|
case NFPROTO_IPV4:
|
2008-01-15 00:42:06 -07:00
|
|
|
return ((a1->ip ^ a2->ip) & m->ip) == 0;
|
2008-10-08 03:35:01 -06:00
|
|
|
case NFPROTO_IPV6:
|
2008-01-15 00:42:06 -07:00
|
|
|
return ipv6_masked_addr_cmp(&a1->in6, &m->in6, &a2->in6) == 0;
|
2006-03-20 19:03:40 -07:00
|
|
|
}
|
2007-07-07 23:15:35 -06:00
|
|
|
return false;
|
2006-03-20 19:03:40 -07:00
|
|
|
}
|
|
|
|
|
2008-01-12 22:26:31 -07:00
|
|
|
static bool
|
2007-07-07 23:16:55 -06:00
|
|
|
match_xfrm_state(const struct xfrm_state *x, const struct xt_policy_elem *e,
|
2006-03-20 19:03:40 -07:00
|
|
|
unsigned short family)
|
|
|
|
{
|
|
|
|
#define MATCH_ADDR(x,y,z) (!e->match.x || \
|
2008-01-15 00:42:06 -07:00
|
|
|
(xt_addr_cmp(&e->x, &e->y, (const union nf_inet_addr *)(z), family) \
|
2006-03-20 19:03:40 -07:00
|
|
|
^ e->invert.x))
|
|
|
|
#define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x))
|
|
|
|
|
2008-01-15 00:42:06 -07:00
|
|
|
return MATCH_ADDR(saddr, smask, &x->props.saddr) &&
|
|
|
|
MATCH_ADDR(daddr, dmask, &x->id.daddr) &&
|
2006-03-20 19:03:40 -07:00
|
|
|
MATCH(proto, x->id.proto) &&
|
|
|
|
MATCH(mode, x->props.mode) &&
|
|
|
|
MATCH(spi, x->id.spi) &&
|
|
|
|
MATCH(reqid, x->props.reqid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
match_policy_in(const struct sk_buff *skb, const struct xt_policy_info *info,
|
|
|
|
unsigned short family)
|
|
|
|
{
|
|
|
|
const struct xt_policy_elem *e;
|
2018-12-18 09:15:20 -07:00
|
|
|
const struct sec_path *sp = skb_sec_path(skb);
|
2006-03-20 19:03:40 -07:00
|
|
|
int strict = info->flags & XT_POLICY_MATCH_STRICT;
|
|
|
|
int i, pos;
|
|
|
|
|
|
|
|
if (sp == NULL)
|
|
|
|
return -1;
|
|
|
|
if (strict && info->len != sp->len)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = sp->len - 1; i >= 0; i--) {
|
|
|
|
pos = strict ? i - sp->len + 1 : 0;
|
|
|
|
if (pos >= info->len)
|
|
|
|
return 0;
|
|
|
|
e = &info->pol[pos];
|
|
|
|
|
2006-04-01 01:54:16 -07:00
|
|
|
if (match_xfrm_state(sp->xvec[i], e, family)) {
|
2006-03-20 19:03:40 -07:00
|
|
|
if (!strict)
|
|
|
|
return 1;
|
|
|
|
} else if (strict)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strict ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
match_policy_out(const struct sk_buff *skb, const struct xt_policy_info *info,
|
|
|
|
unsigned short family)
|
|
|
|
{
|
|
|
|
const struct xt_policy_elem *e;
|
2009-06-01 23:19:30 -06:00
|
|
|
const struct dst_entry *dst = skb_dst(skb);
|
2006-03-20 19:03:40 -07:00
|
|
|
int strict = info->flags & XT_POLICY_MATCH_STRICT;
|
|
|
|
int i, pos;
|
|
|
|
|
|
|
|
if (dst->xfrm == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2017-11-28 13:45:44 -07:00
|
|
|
for (i = 0; dst && dst->xfrm;
|
|
|
|
dst = ((struct xfrm_dst *)dst)->child, i++) {
|
2006-03-20 19:03:40 -07:00
|
|
|
pos = strict ? i : 0;
|
|
|
|
if (pos >= info->len)
|
|
|
|
return 0;
|
|
|
|
e = &info->pol[pos];
|
|
|
|
|
|
|
|
if (match_xfrm_state(dst->xfrm, e, family)) {
|
|
|
|
if (!strict)
|
|
|
|
return 1;
|
|
|
|
} else if (strict)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strict ? i == info->len : 0;
|
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static bool
|
2009-07-07 12:42:08 -06:00
|
|
|
policy_mt(const struct sk_buff *skb, struct xt_action_param *par)
|
2006-03-20 19:03:40 -07:00
|
|
|
{
|
2008-10-08 03:35:18 -06:00
|
|
|
const struct xt_policy_info *info = par->matchinfo;
|
2006-03-20 19:03:40 -07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (info->flags & XT_POLICY_MATCH_IN)
|
2016-11-03 03:56:21 -06:00
|
|
|
ret = match_policy_in(skb, info, xt_family(par));
|
2006-03-20 19:03:40 -07:00
|
|
|
else
|
2016-11-03 03:56:21 -06:00
|
|
|
ret = match_policy_out(skb, info, xt_family(par));
|
2006-03-20 19:03:40 -07:00
|
|
|
|
|
|
|
if (ret < 0)
|
2007-07-07 23:15:35 -06:00
|
|
|
ret = info->flags & XT_POLICY_MATCH_NONE ? true : false;
|
2006-03-20 19:03:40 -07:00
|
|
|
else if (info->flags & XT_POLICY_MATCH_NONE)
|
2007-07-07 23:15:35 -06:00
|
|
|
ret = false;
|
2006-03-20 19:03:40 -07:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-19 10:16:42 -06:00
|
|
|
static int policy_mt_check(const struct xt_mtchk_param *par)
|
2006-03-20 19:03:40 -07:00
|
|
|
{
|
2008-10-08 03:35:18 -06:00
|
|
|
const struct xt_policy_info *info = par->matchinfo;
|
2018-02-09 07:52:06 -07:00
|
|
|
const char *errmsg = "neither incoming nor outgoing policy selected";
|
|
|
|
|
|
|
|
if (!(info->flags & (XT_POLICY_MATCH_IN|XT_POLICY_MATCH_OUT)))
|
|
|
|
goto err;
|
2006-03-20 19:03:40 -07:00
|
|
|
|
2008-10-08 03:35:18 -06:00
|
|
|
if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) |
|
|
|
|
(1 << NF_INET_LOCAL_IN)) && info->flags & XT_POLICY_MATCH_OUT) {
|
2018-02-09 07:52:06 -07:00
|
|
|
errmsg = "output policy not valid in PREROUTING and INPUT";
|
|
|
|
goto err;
|
2006-03-20 19:03:40 -07:00
|
|
|
}
|
2008-10-08 03:35:18 -06:00
|
|
|
if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) |
|
|
|
|
(1 << NF_INET_LOCAL_OUT)) && info->flags & XT_POLICY_MATCH_IN) {
|
2018-02-09 07:52:06 -07:00
|
|
|
errmsg = "input policy not valid in POSTROUTING and OUTPUT";
|
|
|
|
goto err;
|
2006-03-20 19:03:40 -07:00
|
|
|
}
|
|
|
|
if (info->len > XT_POLICY_MAX_ELEM) {
|
2018-02-09 07:52:06 -07:00
|
|
|
errmsg = "too many policy elements";
|
|
|
|
goto err;
|
2006-03-20 19:03:40 -07:00
|
|
|
}
|
2010-03-23 09:35:56 -06:00
|
|
|
return 0;
|
2018-02-09 07:52:06 -07:00
|
|
|
err:
|
|
|
|
pr_info_ratelimited("%s\n", errmsg);
|
|
|
|
return -EINVAL;
|
2006-03-20 19:03:40 -07:00
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static struct xt_match policy_mt_reg[] __read_mostly = {
|
2006-08-22 01:34:04 -06:00
|
|
|
{
|
|
|
|
.name = "policy",
|
2008-10-08 03:35:01 -06:00
|
|
|
.family = NFPROTO_IPV4,
|
2007-12-05 00:24:03 -07:00
|
|
|
.checkentry = policy_mt_check,
|
|
|
|
.match = policy_mt,
|
2006-08-22 01:34:04 -06:00
|
|
|
.matchsize = sizeof(struct xt_policy_info),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "policy",
|
2008-10-08 03:35:01 -06:00
|
|
|
.family = NFPROTO_IPV6,
|
2007-12-05 00:24:03 -07:00
|
|
|
.checkentry = policy_mt_check,
|
|
|
|
.match = policy_mt,
|
2006-08-22 01:34:04 -06:00
|
|
|
.matchsize = sizeof(struct xt_policy_info),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
2006-03-20 19:03:40 -07:00
|
|
|
};
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static int __init policy_mt_init(void)
|
2006-03-20 19:03:40 -07:00
|
|
|
{
|
2007-12-05 00:24:03 -07:00
|
|
|
return xt_register_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg));
|
2006-03-20 19:03:40 -07:00
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static void __exit policy_mt_exit(void)
|
2006-03-20 19:03:40 -07:00
|
|
|
{
|
2007-12-05 00:24:03 -07:00
|
|
|
xt_unregister_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg));
|
2006-03-20 19:03:40 -07:00
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
module_init(policy_mt_init);
|
|
|
|
module_exit(policy_mt_exit);
|
2006-03-20 19:03:40 -07:00
|
|
|
MODULE_ALIAS("ipt_policy");
|
|
|
|
MODULE_ALIAS("ip6t_policy");
|