2011-02-01 14:56:00 +00:00
|
|
|
/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
|
|
|
|
* Patrick Schaaf <bof@bof.de>
|
|
|
|
* Martin Josefsson <gandalf@wlug.westbo.se>
|
2013-04-27 12:28:55 +00:00
|
|
|
* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
|
2011-02-01 14:56:00 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Kernel module which implements the set match and SET target
|
2015-06-13 17:45:33 +00:00
|
|
|
* for netfilter/iptables.
|
|
|
|
*/
|
2011-02-01 14:56:00 +00:00
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
2015-05-02 17:28:23 +00:00
|
|
|
#include <linux/netfilter/ipset/ip_set.h>
|
2012-06-29 09:42:28 +00:00
|
|
|
#include <linux/netfilter/ipset/ip_set_timeout.h>
|
2015-05-02 17:28:23 +00:00
|
|
|
#include <uapi/linux/netfilter/xt_set.h>
|
2011-02-01 14:56:00 +00:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
|
|
|
|
MODULE_DESCRIPTION("Xtables: IP set match and target module");
|
|
|
|
MODULE_ALIAS("xt_SET");
|
|
|
|
MODULE_ALIAS("ipt_set");
|
|
|
|
MODULE_ALIAS("ip6t_set");
|
|
|
|
MODULE_ALIAS("ipt_SET");
|
|
|
|
MODULE_ALIAS("ip6t_SET");
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
match_set(ip_set_id_t index, const struct sk_buff *skb,
|
2011-06-16 16:56:47 +00:00
|
|
|
const struct xt_action_param *par,
|
2013-04-27 12:28:55 +00:00
|
|
|
struct ip_set_adt_opt *opt, int inv)
|
2011-02-01 14:56:00 +00:00
|
|
|
{
|
2011-06-16 16:56:47 +00:00
|
|
|
if (ip_set_test(index, skb, par, opt))
|
2011-02-01 14:56:00 +00:00
|
|
|
inv = !inv;
|
|
|
|
return inv;
|
|
|
|
}
|
|
|
|
|
2011-06-16 17:01:26 +00:00
|
|
|
#define ADT_OPT(n, f, d, fs, cfs, t) \
|
2012-05-07 02:35:44 +00:00
|
|
|
struct ip_set_adt_opt n = { \
|
|
|
|
.family = f, \
|
|
|
|
.dim = d, \
|
|
|
|
.flags = fs, \
|
|
|
|
.cmdflags = cfs, \
|
2013-04-27 12:28:55 +00:00
|
|
|
.ext.timeout = t, \
|
2012-05-07 02:35:44 +00:00
|
|
|
}
|
2011-06-16 16:42:40 +00:00
|
|
|
|
2011-02-01 14:56:00 +00:00
|
|
|
/* Revision 0 interface: backward compatible with netfilter/iptables */
|
|
|
|
|
|
|
|
static bool
|
|
|
|
set_match_v0(const struct sk_buff *skb, struct xt_action_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_set_info_match_v0 *info = par->matchinfo;
|
2015-06-13 17:45:33 +00:00
|
|
|
|
2011-06-16 16:42:40 +00:00
|
|
|
ADT_OPT(opt, par->family, info->match_set.u.compat.dim,
|
|
|
|
info->match_set.u.compat.flags, 0, UINT_MAX);
|
2011-02-01 14:56:00 +00:00
|
|
|
|
2011-06-16 16:56:47 +00:00
|
|
|
return match_set(info->match_set.index, skb, par, &opt,
|
2011-02-01 14:56:00 +00:00
|
|
|
info->match_set.u.compat.flags & IPSET_INV_MATCH);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
compat_flags(struct xt_set_info_v0 *info)
|
|
|
|
{
|
|
|
|
u_int8_t i;
|
|
|
|
|
|
|
|
/* Fill out compatibility data according to enum ip_set_kopt */
|
|
|
|
info->u.compat.dim = IPSET_DIM_ZERO;
|
|
|
|
if (info->u.flags[0] & IPSET_MATCH_INV)
|
|
|
|
info->u.compat.flags |= IPSET_INV_MATCH;
|
2015-06-13 17:45:33 +00:00
|
|
|
for (i = 0; i < IPSET_DIM_MAX - 1 && info->u.flags[i]; i++) {
|
2011-02-01 14:56:00 +00:00
|
|
|
info->u.compat.dim++;
|
|
|
|
if (info->u.flags[i] & IPSET_SRC)
|
2015-06-13 17:45:33 +00:00
|
|
|
info->u.compat.flags |= (1 << info->u.compat.dim);
|
2011-02-01 14:56:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_match_v0_checkentry(const struct xt_mtchk_param *par)
|
|
|
|
{
|
|
|
|
struct xt_set_info_match_v0 *info = par->matchinfo;
|
|
|
|
ip_set_id_t index;
|
|
|
|
|
2013-09-30 15:07:02 +00:00
|
|
|
index = ip_set_nfnl_get_byindex(par->net, info->match_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
|
|
|
|
if (index == IPSET_INVALID_ID) {
|
2014-09-10 04:17:32 +00:00
|
|
|
pr_warn("Cannot find set identified by id %u to match\n",
|
|
|
|
info->match_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
2015-06-13 17:45:33 +00:00
|
|
|
if (info->match_set.u.flags[IPSET_DIM_MAX - 1] != 0) {
|
2014-09-10 04:17:32 +00:00
|
|
|
pr_warn("Protocol error: set match dimension is over the limit!\n");
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->match_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill out compatibility data */
|
|
|
|
compat_flags(&info->match_set);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_match_v0_destroy(const struct xt_mtdtor_param *par)
|
|
|
|
{
|
|
|
|
struct xt_set_info_match_v0 *info = par->matchinfo;
|
|
|
|
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->match_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 05:49:47 +00:00
|
|
|
/* Revision 1 match */
|
|
|
|
|
|
|
|
static bool
|
|
|
|
set_match_v1(const struct sk_buff *skb, struct xt_action_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_set_info_match_v1 *info = par->matchinfo;
|
2015-06-13 17:45:33 +00:00
|
|
|
|
2013-09-30 05:49:47 +00:00
|
|
|
ADT_OPT(opt, par->family, info->match_set.dim,
|
|
|
|
info->match_set.flags, 0, UINT_MAX);
|
|
|
|
|
|
|
|
if (opt.flags & IPSET_RETURN_NOMATCH)
|
|
|
|
opt.cmdflags |= IPSET_FLAG_RETURN_NOMATCH;
|
|
|
|
|
|
|
|
return match_set(info->match_set.index, skb, par, &opt,
|
|
|
|
info->match_set.flags & IPSET_INV_MATCH);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_match_v1_checkentry(const struct xt_mtchk_param *par)
|
|
|
|
{
|
|
|
|
struct xt_set_info_match_v1 *info = par->matchinfo;
|
|
|
|
ip_set_id_t index;
|
|
|
|
|
2013-09-30 15:07:02 +00:00
|
|
|
index = ip_set_nfnl_get_byindex(par->net, info->match_set.index);
|
2013-09-30 05:49:47 +00:00
|
|
|
|
|
|
|
if (index == IPSET_INVALID_ID) {
|
2014-09-10 04:17:32 +00:00
|
|
|
pr_warn("Cannot find set identified by id %u to match\n",
|
|
|
|
info->match_set.index);
|
2013-09-30 05:49:47 +00:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
if (info->match_set.dim > IPSET_DIM_MAX) {
|
2014-09-10 04:17:32 +00:00
|
|
|
pr_warn("Protocol error: set match dimension is over the limit!\n");
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->match_set.index);
|
2013-09-30 05:49:47 +00:00
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_match_v1_destroy(const struct xt_mtdtor_param *par)
|
|
|
|
{
|
|
|
|
struct xt_set_info_match_v1 *info = par->matchinfo;
|
|
|
|
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->match_set.index);
|
2013-09-30 05:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Revision 3 match */
|
|
|
|
|
|
|
|
static bool
|
2014-11-30 18:56:53 +00:00
|
|
|
match_counter0(u64 counter, const struct ip_set_counter_match0 *info)
|
2013-09-30 05:49:47 +00:00
|
|
|
{
|
|
|
|
switch (info->op) {
|
|
|
|
case IPSET_COUNTER_NONE:
|
|
|
|
return true;
|
|
|
|
case IPSET_COUNTER_EQ:
|
|
|
|
return counter == info->value;
|
|
|
|
case IPSET_COUNTER_NE:
|
|
|
|
return counter != info->value;
|
|
|
|
case IPSET_COUNTER_LT:
|
|
|
|
return counter < info->value;
|
|
|
|
case IPSET_COUNTER_GT:
|
|
|
|
return counter > info->value;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
set_match_v3(const struct sk_buff *skb, struct xt_action_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_set_info_match_v3 *info = par->matchinfo;
|
2015-06-13 17:45:33 +00:00
|
|
|
int ret;
|
|
|
|
|
2013-09-30 05:49:47 +00:00
|
|
|
ADT_OPT(opt, par->family, info->match_set.dim,
|
|
|
|
info->match_set.flags, info->flags, UINT_MAX);
|
|
|
|
|
|
|
|
if (info->packets.op != IPSET_COUNTER_NONE ||
|
|
|
|
info->bytes.op != IPSET_COUNTER_NONE)
|
|
|
|
opt.cmdflags |= IPSET_FLAG_MATCH_COUNTERS;
|
|
|
|
|
|
|
|
ret = match_set(info->match_set.index, skb, par, &opt,
|
|
|
|
info->match_set.flags & IPSET_INV_MATCH);
|
|
|
|
|
|
|
|
if (!(ret && opt.cmdflags & IPSET_FLAG_MATCH_COUNTERS))
|
|
|
|
return ret;
|
|
|
|
|
2014-11-30 18:56:53 +00:00
|
|
|
if (!match_counter0(opt.ext.packets, &info->packets))
|
2015-02-11 12:33:05 +00:00
|
|
|
return false;
|
2014-11-30 18:56:53 +00:00
|
|
|
return match_counter0(opt.ext.bytes, &info->bytes);
|
2013-09-30 05:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define set_match_v3_checkentry set_match_v1_checkentry
|
|
|
|
#define set_match_v3_destroy set_match_v1_destroy
|
|
|
|
|
2014-11-30 18:56:53 +00:00
|
|
|
/* Revision 4 match */
|
|
|
|
|
|
|
|
static bool
|
|
|
|
match_counter(u64 counter, const struct ip_set_counter_match *info)
|
|
|
|
{
|
|
|
|
switch (info->op) {
|
|
|
|
case IPSET_COUNTER_NONE:
|
|
|
|
return true;
|
|
|
|
case IPSET_COUNTER_EQ:
|
|
|
|
return counter == info->value;
|
|
|
|
case IPSET_COUNTER_NE:
|
|
|
|
return counter != info->value;
|
|
|
|
case IPSET_COUNTER_LT:
|
|
|
|
return counter < info->value;
|
|
|
|
case IPSET_COUNTER_GT:
|
|
|
|
return counter > info->value;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
set_match_v4(const struct sk_buff *skb, struct xt_action_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_set_info_match_v4 *info = par->matchinfo;
|
2015-06-13 17:45:33 +00:00
|
|
|
int ret;
|
|
|
|
|
2014-11-30 18:56:53 +00:00
|
|
|
ADT_OPT(opt, par->family, info->match_set.dim,
|
|
|
|
info->match_set.flags, info->flags, UINT_MAX);
|
|
|
|
|
|
|
|
if (info->packets.op != IPSET_COUNTER_NONE ||
|
|
|
|
info->bytes.op != IPSET_COUNTER_NONE)
|
|
|
|
opt.cmdflags |= IPSET_FLAG_MATCH_COUNTERS;
|
|
|
|
|
|
|
|
ret = match_set(info->match_set.index, skb, par, &opt,
|
|
|
|
info->match_set.flags & IPSET_INV_MATCH);
|
|
|
|
|
|
|
|
if (!(ret && opt.cmdflags & IPSET_FLAG_MATCH_COUNTERS))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!match_counter(opt.ext.packets, &info->packets))
|
2015-02-11 12:33:05 +00:00
|
|
|
return false;
|
2014-11-30 18:56:53 +00:00
|
|
|
return match_counter(opt.ext.bytes, &info->bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define set_match_v4_checkentry set_match_v1_checkentry
|
|
|
|
#define set_match_v4_destroy set_match_v1_destroy
|
|
|
|
|
2013-09-30 05:49:47 +00:00
|
|
|
/* Revision 0 interface: backward compatible with netfilter/iptables */
|
|
|
|
|
2011-02-01 14:56:00 +00:00
|
|
|
static unsigned int
|
|
|
|
set_target_v0(struct sk_buff *skb, const struct xt_action_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_set_info_target_v0 *info = par->targinfo;
|
2015-06-13 17:45:33 +00:00
|
|
|
|
2011-06-16 16:42:40 +00:00
|
|
|
ADT_OPT(add_opt, par->family, info->add_set.u.compat.dim,
|
|
|
|
info->add_set.u.compat.flags, 0, UINT_MAX);
|
|
|
|
ADT_OPT(del_opt, par->family, info->del_set.u.compat.dim,
|
|
|
|
info->del_set.u.compat.flags, 0, UINT_MAX);
|
2011-02-01 14:56:00 +00:00
|
|
|
|
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
2011-06-16 16:56:47 +00:00
|
|
|
ip_set_add(info->add_set.index, skb, par, &add_opt);
|
2011-02-01 14:56:00 +00:00
|
|
|
if (info->del_set.index != IPSET_INVALID_ID)
|
2011-06-16 16:56:47 +00:00
|
|
|
ip_set_del(info->del_set.index, skb, par, &del_opt);
|
2011-02-01 14:56:00 +00:00
|
|
|
|
|
|
|
return XT_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_target_v0_checkentry(const struct xt_tgchk_param *par)
|
|
|
|
{
|
|
|
|
struct xt_set_info_target_v0 *info = par->targinfo;
|
|
|
|
ip_set_id_t index;
|
|
|
|
|
|
|
|
if (info->add_set.index != IPSET_INVALID_ID) {
|
2013-09-30 15:07:02 +00:00
|
|
|
index = ip_set_nfnl_get_byindex(par->net, info->add_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
if (index == IPSET_INVALID_ID) {
|
2014-09-10 04:17:32 +00:00
|
|
|
pr_warn("Cannot find add_set index %u as target\n",
|
|
|
|
info->add_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->del_set.index != IPSET_INVALID_ID) {
|
2013-09-30 15:07:02 +00:00
|
|
|
index = ip_set_nfnl_get_byindex(par->net, info->del_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
if (index == IPSET_INVALID_ID) {
|
2014-09-10 04:17:32 +00:00
|
|
|
pr_warn("Cannot find del_set index %u as target\n",
|
|
|
|
info->del_set.index);
|
2011-04-13 11:45:57 +00:00
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->add_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
2015-06-13 17:45:33 +00:00
|
|
|
if (info->add_set.u.flags[IPSET_DIM_MAX - 1] != 0 ||
|
|
|
|
info->del_set.u.flags[IPSET_DIM_MAX - 1] != 0) {
|
2014-09-10 04:17:32 +00:00
|
|
|
pr_warn("Protocol error: SET target dimension is over the limit!\n");
|
2011-04-13 11:45:57 +00:00
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->add_set.index);
|
2011-04-13 11:45:57 +00:00
|
|
|
if (info->del_set.index != IPSET_INVALID_ID)
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->del_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill out compatibility data */
|
|
|
|
compat_flags(&info->add_set);
|
|
|
|
compat_flags(&info->del_set);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_target_v0_destroy(const struct xt_tgdtor_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_set_info_target_v0 *info = par->targinfo;
|
|
|
|
|
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->add_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
if (info->del_set.index != IPSET_INVALID_ID)
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->del_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 05:49:47 +00:00
|
|
|
/* Revision 1 target */
|
2011-02-01 14:56:00 +00:00
|
|
|
|
|
|
|
static unsigned int
|
2011-06-16 16:42:40 +00:00
|
|
|
set_target_v1(struct sk_buff *skb, const struct xt_action_param *par)
|
2011-02-01 14:56:00 +00:00
|
|
|
{
|
2011-06-16 16:42:40 +00:00
|
|
|
const struct xt_set_info_target_v1 *info = par->targinfo;
|
2015-06-13 17:45:33 +00:00
|
|
|
|
2011-06-16 16:42:40 +00:00
|
|
|
ADT_OPT(add_opt, par->family, info->add_set.dim,
|
|
|
|
info->add_set.flags, 0, UINT_MAX);
|
|
|
|
ADT_OPT(del_opt, par->family, info->del_set.dim,
|
|
|
|
info->del_set.flags, 0, UINT_MAX);
|
2011-02-01 14:56:00 +00:00
|
|
|
|
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
2011-06-16 16:56:47 +00:00
|
|
|
ip_set_add(info->add_set.index, skb, par, &add_opt);
|
2011-02-01 14:56:00 +00:00
|
|
|
if (info->del_set.index != IPSET_INVALID_ID)
|
2011-06-16 16:56:47 +00:00
|
|
|
ip_set_del(info->del_set.index, skb, par, &del_opt);
|
2011-02-01 14:56:00 +00:00
|
|
|
|
|
|
|
return XT_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-06-16 16:42:40 +00:00
|
|
|
set_target_v1_checkentry(const struct xt_tgchk_param *par)
|
2011-02-01 14:56:00 +00:00
|
|
|
{
|
2011-06-16 16:42:40 +00:00
|
|
|
const struct xt_set_info_target_v1 *info = par->targinfo;
|
2011-02-01 14:56:00 +00:00
|
|
|
ip_set_id_t index;
|
|
|
|
|
|
|
|
if (info->add_set.index != IPSET_INVALID_ID) {
|
2013-09-30 15:07:02 +00:00
|
|
|
index = ip_set_nfnl_get_byindex(par->net, info->add_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
if (index == IPSET_INVALID_ID) {
|
2014-09-10 04:17:32 +00:00
|
|
|
pr_warn("Cannot find add_set index %u as target\n",
|
|
|
|
info->add_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->del_set.index != IPSET_INVALID_ID) {
|
2013-09-30 15:07:02 +00:00
|
|
|
index = ip_set_nfnl_get_byindex(par->net, info->del_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
if (index == IPSET_INVALID_ID) {
|
2014-09-10 04:17:32 +00:00
|
|
|
pr_warn("Cannot find del_set index %u as target\n",
|
|
|
|
info->del_set.index);
|
2011-04-13 11:45:57 +00:00
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->add_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (info->add_set.dim > IPSET_DIM_MAX ||
|
2011-04-13 11:45:57 +00:00
|
|
|
info->del_set.dim > IPSET_DIM_MAX) {
|
2014-09-10 04:17:32 +00:00
|
|
|
pr_warn("Protocol error: SET target dimension is over the limit!\n");
|
2011-04-13 11:45:57 +00:00
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->add_set.index);
|
2011-04-13 11:45:57 +00:00
|
|
|
if (info->del_set.index != IPSET_INVALID_ID)
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->del_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-16 16:42:40 +00:00
|
|
|
set_target_v1_destroy(const struct xt_tgdtor_param *par)
|
2011-02-01 14:56:00 +00:00
|
|
|
{
|
2011-06-16 16:42:40 +00:00
|
|
|
const struct xt_set_info_target_v1 *info = par->targinfo;
|
2011-02-01 14:56:00 +00:00
|
|
|
|
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->add_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
if (info->del_set.index != IPSET_INVALID_ID)
|
2013-09-30 15:07:02 +00:00
|
|
|
ip_set_nfnl_put(par->net, info->del_set.index);
|
2011-02-01 14:56:00 +00:00
|
|
|
}
|
|
|
|
|
2011-06-16 16:42:40 +00:00
|
|
|
/* Revision 2 target */
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
set_target_v2(struct sk_buff *skb, const struct xt_action_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_set_info_target_v2 *info = par->targinfo;
|
2015-06-13 17:45:33 +00:00
|
|
|
|
2013-04-27 12:28:55 +00:00
|
|
|
ADT_OPT(add_opt, par->family, info->add_set.dim,
|
|
|
|
info->add_set.flags, info->flags, info->timeout);
|
2011-06-16 16:42:40 +00:00
|
|
|
ADT_OPT(del_opt, par->family, info->del_set.dim,
|
|
|
|
info->del_set.flags, 0, UINT_MAX);
|
|
|
|
|
2012-05-07 02:35:44 +00:00
|
|
|
/* Normalize to fit into jiffies */
|
2013-04-27 12:28:55 +00:00
|
|
|
if (add_opt.ext.timeout != IPSET_NO_TIMEOUT &&
|
2015-06-13 17:45:33 +00:00
|
|
|
add_opt.ext.timeout > UINT_MAX / MSEC_PER_SEC)
|
|
|
|
add_opt.ext.timeout = UINT_MAX / MSEC_PER_SEC;
|
2011-06-16 16:42:40 +00:00
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
2011-06-16 16:56:47 +00:00
|
|
|
ip_set_add(info->add_set.index, skb, par, &add_opt);
|
2011-06-16 16:42:40 +00:00
|
|
|
if (info->del_set.index != IPSET_INVALID_ID)
|
2011-06-16 16:56:47 +00:00
|
|
|
ip_set_del(info->del_set.index, skb, par, &del_opt);
|
2011-06-16 16:42:40 +00:00
|
|
|
|
|
|
|
return XT_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define set_target_v2_checkentry set_target_v1_checkentry
|
|
|
|
#define set_target_v2_destroy set_target_v1_destroy
|
|
|
|
|
2014-09-02 10:21:20 +00:00
|
|
|
/* Revision 3 target */
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
set_target_v3(struct sk_buff *skb, const struct xt_action_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_set_info_target_v3 *info = par->targinfo;
|
2015-06-13 17:45:33 +00:00
|
|
|
int ret;
|
|
|
|
|
2014-09-02 10:21:20 +00:00
|
|
|
ADT_OPT(add_opt, par->family, info->add_set.dim,
|
|
|
|
info->add_set.flags, info->flags, info->timeout);
|
|
|
|
ADT_OPT(del_opt, par->family, info->del_set.dim,
|
|
|
|
info->del_set.flags, 0, UINT_MAX);
|
|
|
|
ADT_OPT(map_opt, par->family, info->map_set.dim,
|
|
|
|
info->map_set.flags, 0, UINT_MAX);
|
|
|
|
|
|
|
|
/* Normalize to fit into jiffies */
|
|
|
|
if (add_opt.ext.timeout != IPSET_NO_TIMEOUT &&
|
2015-06-13 17:45:33 +00:00
|
|
|
add_opt.ext.timeout > UINT_MAX / MSEC_PER_SEC)
|
|
|
|
add_opt.ext.timeout = UINT_MAX / MSEC_PER_SEC;
|
2014-09-02 10:21:20 +00:00
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
|
|
|
ip_set_add(info->add_set.index, skb, par, &add_opt);
|
|
|
|
if (info->del_set.index != IPSET_INVALID_ID)
|
|
|
|
ip_set_del(info->del_set.index, skb, par, &del_opt);
|
|
|
|
if (info->map_set.index != IPSET_INVALID_ID) {
|
|
|
|
map_opt.cmdflags |= info->flags & (IPSET_FLAG_MAP_SKBMARK |
|
|
|
|
IPSET_FLAG_MAP_SKBPRIO |
|
|
|
|
IPSET_FLAG_MAP_SKBQUEUE);
|
|
|
|
ret = match_set(info->map_set.index, skb, par, &map_opt,
|
|
|
|
info->map_set.flags & IPSET_INV_MATCH);
|
|
|
|
if (!ret)
|
|
|
|
return XT_CONTINUE;
|
|
|
|
if (map_opt.cmdflags & IPSET_FLAG_MAP_SKBMARK)
|
|
|
|
skb->mark = (skb->mark & ~(map_opt.ext.skbmarkmask))
|
|
|
|
^ (map_opt.ext.skbmark);
|
|
|
|
if (map_opt.cmdflags & IPSET_FLAG_MAP_SKBPRIO)
|
|
|
|
skb->priority = map_opt.ext.skbprio;
|
|
|
|
if ((map_opt.cmdflags & IPSET_FLAG_MAP_SKBQUEUE) &&
|
|
|
|
skb->dev &&
|
|
|
|
skb->dev->real_num_tx_queues > map_opt.ext.skbqueue)
|
|
|
|
skb_set_queue_mapping(skb, map_opt.ext.skbqueue);
|
|
|
|
}
|
|
|
|
return XT_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_target_v3_checkentry(const struct xt_tgchk_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_set_info_target_v3 *info = par->targinfo;
|
|
|
|
ip_set_id_t index;
|
|
|
|
|
|
|
|
if (info->add_set.index != IPSET_INVALID_ID) {
|
|
|
|
index = ip_set_nfnl_get_byindex(par->net,
|
|
|
|
info->add_set.index);
|
|
|
|
if (index == IPSET_INVALID_ID) {
|
|
|
|
pr_warn("Cannot find add_set index %u as target\n",
|
|
|
|
info->add_set.index);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->del_set.index != IPSET_INVALID_ID) {
|
|
|
|
index = ip_set_nfnl_get_byindex(par->net,
|
|
|
|
info->del_set.index);
|
|
|
|
if (index == IPSET_INVALID_ID) {
|
|
|
|
pr_warn("Cannot find del_set index %u as target\n",
|
|
|
|
info->del_set.index);
|
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
|
|
|
ip_set_nfnl_put(par->net,
|
|
|
|
info->add_set.index);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->map_set.index != IPSET_INVALID_ID) {
|
|
|
|
if (strncmp(par->table, "mangle", 7)) {
|
|
|
|
pr_warn("--map-set only usable from mangle table\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (((info->flags & IPSET_FLAG_MAP_SKBPRIO) |
|
|
|
|
(info->flags & IPSET_FLAG_MAP_SKBQUEUE)) &&
|
|
|
|
!(par->hook_mask & (1 << NF_INET_FORWARD |
|
|
|
|
1 << NF_INET_LOCAL_OUT |
|
|
|
|
1 << NF_INET_POST_ROUTING))) {
|
2015-06-13 17:45:33 +00:00
|
|
|
pr_warn("mapping of prio or/and queue is allowed only from OUTPUT/FORWARD/POSTROUTING chains\n");
|
2014-09-02 10:21:20 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
index = ip_set_nfnl_get_byindex(par->net,
|
|
|
|
info->map_set.index);
|
|
|
|
if (index == IPSET_INVALID_ID) {
|
|
|
|
pr_warn("Cannot find map_set index %u as target\n",
|
|
|
|
info->map_set.index);
|
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
|
|
|
ip_set_nfnl_put(par->net,
|
|
|
|
info->add_set.index);
|
|
|
|
if (info->del_set.index != IPSET_INVALID_ID)
|
|
|
|
ip_set_nfnl_put(par->net,
|
|
|
|
info->del_set.index);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->add_set.dim > IPSET_DIM_MAX ||
|
|
|
|
info->del_set.dim > IPSET_DIM_MAX ||
|
|
|
|
info->map_set.dim > IPSET_DIM_MAX) {
|
2015-06-13 17:45:33 +00:00
|
|
|
pr_warn("Protocol error: SET target dimension is over the limit!\n");
|
2014-09-02 10:21:20 +00:00
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
|
|
|
ip_set_nfnl_put(par->net, info->add_set.index);
|
|
|
|
if (info->del_set.index != IPSET_INVALID_ID)
|
|
|
|
ip_set_nfnl_put(par->net, info->del_set.index);
|
|
|
|
if (info->map_set.index != IPSET_INVALID_ID)
|
|
|
|
ip_set_nfnl_put(par->net, info->map_set.index);
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_target_v3_destroy(const struct xt_tgdtor_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_set_info_target_v3 *info = par->targinfo;
|
|
|
|
|
|
|
|
if (info->add_set.index != IPSET_INVALID_ID)
|
|
|
|
ip_set_nfnl_put(par->net, info->add_set.index);
|
|
|
|
if (info->del_set.index != IPSET_INVALID_ID)
|
|
|
|
ip_set_nfnl_put(par->net, info->del_set.index);
|
|
|
|
if (info->map_set.index != IPSET_INVALID_ID)
|
|
|
|
ip_set_nfnl_put(par->net, info->map_set.index);
|
|
|
|
}
|
|
|
|
|
2011-02-01 14:56:00 +00:00
|
|
|
static struct xt_match set_matches[] __read_mostly = {
|
|
|
|
{
|
|
|
|
.name = "set",
|
|
|
|
.family = NFPROTO_IPV4,
|
|
|
|
.revision = 0,
|
|
|
|
.match = set_match_v0,
|
|
|
|
.matchsize = sizeof(struct xt_set_info_match_v0),
|
|
|
|
.checkentry = set_match_v0_checkentry,
|
|
|
|
.destroy = set_match_v0_destroy,
|
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "set",
|
|
|
|
.family = NFPROTO_IPV4,
|
|
|
|
.revision = 1,
|
2011-06-16 16:42:40 +00:00
|
|
|
.match = set_match_v1,
|
|
|
|
.matchsize = sizeof(struct xt_set_info_match_v1),
|
|
|
|
.checkentry = set_match_v1_checkentry,
|
|
|
|
.destroy = set_match_v1_destroy,
|
2011-02-01 14:56:00 +00:00
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "set",
|
|
|
|
.family = NFPROTO_IPV6,
|
|
|
|
.revision = 1,
|
2011-06-16 16:42:40 +00:00
|
|
|
.match = set_match_v1,
|
|
|
|
.matchsize = sizeof(struct xt_set_info_match_v1),
|
|
|
|
.checkentry = set_match_v1_checkentry,
|
|
|
|
.destroy = set_match_v1_destroy,
|
2011-02-01 14:56:00 +00:00
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
2012-09-21 20:02:36 +00:00
|
|
|
/* --return-nomatch flag support */
|
|
|
|
{
|
|
|
|
.name = "set",
|
|
|
|
.family = NFPROTO_IPV4,
|
|
|
|
.revision = 2,
|
|
|
|
.match = set_match_v1,
|
|
|
|
.matchsize = sizeof(struct xt_set_info_match_v1),
|
|
|
|
.checkentry = set_match_v1_checkentry,
|
|
|
|
.destroy = set_match_v1_destroy,
|
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "set",
|
|
|
|
.family = NFPROTO_IPV6,
|
|
|
|
.revision = 2,
|
|
|
|
.match = set_match_v1,
|
|
|
|
.matchsize = sizeof(struct xt_set_info_match_v1),
|
|
|
|
.checkentry = set_match_v1_checkentry,
|
|
|
|
.destroy = set_match_v1_destroy,
|
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
2013-04-27 12:40:50 +00:00
|
|
|
/* counters support: update, match */
|
|
|
|
{
|
|
|
|
.name = "set",
|
|
|
|
.family = NFPROTO_IPV4,
|
|
|
|
.revision = 3,
|
|
|
|
.match = set_match_v3,
|
|
|
|
.matchsize = sizeof(struct xt_set_info_match_v3),
|
|
|
|
.checkentry = set_match_v3_checkentry,
|
|
|
|
.destroy = set_match_v3_destroy,
|
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "set",
|
|
|
|
.family = NFPROTO_IPV6,
|
|
|
|
.revision = 3,
|
|
|
|
.match = set_match_v3,
|
|
|
|
.matchsize = sizeof(struct xt_set_info_match_v3),
|
|
|
|
.checkentry = set_match_v3_checkentry,
|
|
|
|
.destroy = set_match_v3_destroy,
|
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
2014-11-30 18:56:53 +00:00
|
|
|
/* new revision for counters support: update, match */
|
|
|
|
{
|
|
|
|
.name = "set",
|
|
|
|
.family = NFPROTO_IPV4,
|
|
|
|
.revision = 4,
|
|
|
|
.match = set_match_v4,
|
|
|
|
.matchsize = sizeof(struct xt_set_info_match_v4),
|
|
|
|
.checkentry = set_match_v4_checkentry,
|
|
|
|
.destroy = set_match_v4_destroy,
|
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "set",
|
|
|
|
.family = NFPROTO_IPV6,
|
|
|
|
.revision = 4,
|
|
|
|
.match = set_match_v4,
|
|
|
|
.matchsize = sizeof(struct xt_set_info_match_v4),
|
|
|
|
.checkentry = set_match_v4_checkentry,
|
|
|
|
.destroy = set_match_v4_destroy,
|
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
2011-02-01 14:56:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct xt_target set_targets[] __read_mostly = {
|
|
|
|
{
|
|
|
|
.name = "SET",
|
|
|
|
.revision = 0,
|
|
|
|
.family = NFPROTO_IPV4,
|
|
|
|
.target = set_target_v0,
|
|
|
|
.targetsize = sizeof(struct xt_set_info_target_v0),
|
|
|
|
.checkentry = set_target_v0_checkentry,
|
|
|
|
.destroy = set_target_v0_destroy,
|
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "SET",
|
|
|
|
.revision = 1,
|
|
|
|
.family = NFPROTO_IPV4,
|
2011-06-16 16:42:40 +00:00
|
|
|
.target = set_target_v1,
|
|
|
|
.targetsize = sizeof(struct xt_set_info_target_v1),
|
|
|
|
.checkentry = set_target_v1_checkentry,
|
|
|
|
.destroy = set_target_v1_destroy,
|
2011-02-01 14:56:00 +00:00
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "SET",
|
|
|
|
.revision = 1,
|
|
|
|
.family = NFPROTO_IPV6,
|
2011-06-16 16:42:40 +00:00
|
|
|
.target = set_target_v1,
|
|
|
|
.targetsize = sizeof(struct xt_set_info_target_v1),
|
|
|
|
.checkentry = set_target_v1_checkentry,
|
|
|
|
.destroy = set_target_v1_destroy,
|
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
2012-09-21 20:02:36 +00:00
|
|
|
/* --timeout and --exist flags support */
|
2011-06-16 16:42:40 +00:00
|
|
|
{
|
|
|
|
.name = "SET",
|
|
|
|
.revision = 2,
|
|
|
|
.family = NFPROTO_IPV4,
|
|
|
|
.target = set_target_v2,
|
|
|
|
.targetsize = sizeof(struct xt_set_info_target_v2),
|
|
|
|
.checkentry = set_target_v2_checkentry,
|
|
|
|
.destroy = set_target_v2_destroy,
|
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "SET",
|
|
|
|
.revision = 2,
|
|
|
|
.family = NFPROTO_IPV6,
|
|
|
|
.target = set_target_v2,
|
|
|
|
.targetsize = sizeof(struct xt_set_info_target_v2),
|
|
|
|
.checkentry = set_target_v2_checkentry,
|
|
|
|
.destroy = set_target_v2_destroy,
|
2011-02-01 14:56:00 +00:00
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
2014-09-02 10:21:20 +00:00
|
|
|
/* --map-set support */
|
|
|
|
{
|
|
|
|
.name = "SET",
|
|
|
|
.revision = 3,
|
|
|
|
.family = NFPROTO_IPV4,
|
|
|
|
.target = set_target_v3,
|
|
|
|
.targetsize = sizeof(struct xt_set_info_target_v3),
|
|
|
|
.checkentry = set_target_v3_checkentry,
|
|
|
|
.destroy = set_target_v3_destroy,
|
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "SET",
|
|
|
|
.revision = 3,
|
|
|
|
.family = NFPROTO_IPV6,
|
|
|
|
.target = set_target_v3,
|
|
|
|
.targetsize = sizeof(struct xt_set_info_target_v3),
|
|
|
|
.checkentry = set_target_v3_checkentry,
|
|
|
|
.destroy = set_target_v3_destroy,
|
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
2011-02-01 14:56:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __init xt_set_init(void)
|
|
|
|
{
|
|
|
|
int ret = xt_register_matches(set_matches, ARRAY_SIZE(set_matches));
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
ret = xt_register_targets(set_targets,
|
|
|
|
ARRAY_SIZE(set_targets));
|
|
|
|
if (ret)
|
|
|
|
xt_unregister_matches(set_matches,
|
|
|
|
ARRAY_SIZE(set_matches));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit xt_set_fini(void)
|
|
|
|
{
|
|
|
|
xt_unregister_matches(set_matches, ARRAY_SIZE(set_matches));
|
|
|
|
xt_unregister_targets(set_targets, ARRAY_SIZE(set_targets));
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(xt_set_init);
|
|
|
|
module_exit(xt_set_fini);
|