Blame extensions/libxt_connmark.c

Packit Service d1fe03
/* Shared library add-on to iptables to add connmark matching support.
Packit Service d1fe03
 *
Packit Service d1fe03
 * (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
Packit Service d1fe03
 * by Henrik Nordstrom <hno@marasystems.com>
Packit Service d1fe03
 *
Packit Service d1fe03
 * Version 1.1
Packit Service d1fe03
 *
Packit Service d1fe03
 * This program is free software; you can redistribute it and/or modify
Packit Service d1fe03
 * it under the terms of the GNU General Public License as published by
Packit Service d1fe03
 * the Free Software Foundation; either version 2 of the License, or
Packit Service d1fe03
 * (at your option) any later version.
Packit Service d1fe03
 *
Packit Service d1fe03
 * This program is distributed in the hope that it will be useful,
Packit Service d1fe03
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service d1fe03
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service d1fe03
 * GNU General Public License for more details.
Packit Service d1fe03
 *
Packit Service d1fe03
 * You should have received a copy of the GNU General Public License
Packit Service d1fe03
 * along with this program; if not, write to the Free Software
Packit Service d1fe03
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Packit Service d1fe03
 */
Packit Service d1fe03
#include <stdbool.h>
Packit Service d1fe03
#include <stdint.h>
Packit Service d1fe03
#include <stdio.h>
Packit Service d1fe03
#include <xtables.h>
Packit Service d1fe03
#include <linux/netfilter/xt_connmark.h>
Packit Service d1fe03
Packit Service d1fe03
struct xt_connmark_info {
Packit Service d1fe03
	unsigned long mark, mask;
Packit Service d1fe03
	uint8_t invert;
Packit Service d1fe03
};
Packit Service d1fe03
Packit Service d1fe03
enum {
Packit Service d1fe03
	O_MARK = 0,
Packit Service d1fe03
};
Packit Service d1fe03
Packit Service d1fe03
static void connmark_mt_help(void)
Packit Service d1fe03
{
Packit Service d1fe03
	printf(
Packit Service d1fe03
"connmark match options:\n"
Packit Service d1fe03
"[!] --mark value[/mask]    Match ctmark value with optional mask\n");
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static const struct xt_option_entry connmark_mt_opts[] = {
Packit Service d1fe03
	{.name = "mark", .id = O_MARK, .type = XTTYPE_MARKMASK32,
Packit Service d1fe03
	 .flags = XTOPT_MAND | XTOPT_INVERT},
Packit Service d1fe03
	XTOPT_TABLEEND,
Packit Service d1fe03
};
Packit Service d1fe03
Packit Service d1fe03
static void connmark_mt_parse(struct xt_option_call *cb)
Packit Service d1fe03
{
Packit Service d1fe03
	struct xt_connmark_mtinfo1 *info = cb->data;
Packit Service d1fe03
Packit Service d1fe03
	xtables_option_parse(cb);
Packit Service d1fe03
	if (cb->invert)
Packit Service d1fe03
		info->invert = true;
Packit Service d1fe03
	info->mark = cb->val.mark;
Packit Service d1fe03
	info->mask = cb->val.mask;
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static void connmark_parse(struct xt_option_call *cb)
Packit Service d1fe03
{
Packit Service d1fe03
	struct xt_connmark_info *markinfo = cb->data;
Packit Service d1fe03
Packit Service d1fe03
	xtables_option_parse(cb);
Packit Service d1fe03
	markinfo->mark = cb->val.mark;
Packit Service d1fe03
	markinfo->mask = cb->val.mask;
Packit Service d1fe03
	if (cb->invert)
Packit Service d1fe03
		markinfo->invert = 1;
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static void
Packit Service d1fe03
connmark_print(const void *ip, const struct xt_entry_match *match, int numeric)
Packit Service d1fe03
{
Packit Service d1fe03
	const struct xt_connmark_info *info = (const void *)match->data;
Packit Service d1fe03
Packit Service d1fe03
	printf(" CONNMARK match ");
Packit Service d1fe03
	if (info->invert)
Packit Service d1fe03
		printf("!");
Packit Service d1fe03
Packit Service d1fe03
	xtables_print_mark_mask(info->mark, info->mask);
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static void
Packit Service d1fe03
connmark_mt_print(const void *ip, const struct xt_entry_match *match,
Packit Service d1fe03
		  int numeric)
Packit Service d1fe03
{
Packit Service d1fe03
	const struct xt_connmark_mtinfo1 *info = (const void *)match->data;
Packit Service d1fe03
Packit Service d1fe03
	printf(" connmark match ");
Packit Service d1fe03
	if (info->invert)
Packit Service d1fe03
		printf("!");
Packit Service d1fe03
Packit Service d1fe03
	xtables_print_mark_mask(info->mark, info->mask);
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static void connmark_save(const void *ip, const struct xt_entry_match *match)
Packit Service d1fe03
{
Packit Service d1fe03
	const struct xt_connmark_info *info = (const void *)match->data;
Packit Service d1fe03
Packit Service d1fe03
	if (info->invert)
Packit Service d1fe03
		printf(" !");
Packit Service d1fe03
Packit Service d1fe03
	printf(" --mark");
Packit Service d1fe03
	xtables_print_mark_mask(info->mark, info->mask);
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static void
Packit Service d1fe03
connmark_mt_save(const void *ip, const struct xt_entry_match *match)
Packit Service d1fe03
{
Packit Service d1fe03
	const struct xt_connmark_mtinfo1 *info = (const void *)match->data;
Packit Service d1fe03
Packit Service d1fe03
	if (info->invert)
Packit Service d1fe03
		printf(" !");
Packit Service d1fe03
Packit Service d1fe03
	printf(" --mark");
Packit Service d1fe03
	xtables_print_mark_mask(info->mark, info->mask);
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static void print_mark_xlate(unsigned int mark, unsigned int mask,
Packit Service d1fe03
			     struct xt_xlate *xl, uint32_t op)
Packit Service d1fe03
{
Packit Service d1fe03
	if (mask != 0xffffffffU)
Packit Service d1fe03
		xt_xlate_add(xl, " and 0x%x %s 0x%x", mask,
Packit Service d1fe03
			   op == XT_OP_EQ ? "==" : "!=", mark);
Packit Service d1fe03
	else
Packit Service d1fe03
		xt_xlate_add(xl, " %s0x%x",
Packit Service d1fe03
			   op == XT_OP_EQ ? "" : "!= ", mark);
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static int connmark_xlate(struct xt_xlate *xl,
Packit Service d1fe03
			  const struct xt_xlate_mt_params *params)
Packit Service d1fe03
{
Packit Service d1fe03
	const struct xt_connmark_info *info = (const void *)params->match->data;
Packit Service d1fe03
	enum xt_op op = XT_OP_EQ;
Packit Service d1fe03
Packit Service d1fe03
	if (info->invert)
Packit Service d1fe03
		op = XT_OP_NEQ;
Packit Service d1fe03
Packit Service d1fe03
	xt_xlate_add(xl, "ct mark");
Packit Service d1fe03
	print_mark_xlate(info->mark, info->mask, xl, op);
Packit Service d1fe03
Packit Service d1fe03
	return 1;
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static int
Packit Service d1fe03
connmark_mt_xlate(struct xt_xlate *xl,
Packit Service d1fe03
		  const struct xt_xlate_mt_params *params)
Packit Service d1fe03
{
Packit Service d1fe03
	const struct xt_connmark_mtinfo1 *info =
Packit Service d1fe03
		(const void *)params->match->data;
Packit Service d1fe03
	enum xt_op op = XT_OP_EQ;
Packit Service d1fe03
Packit Service d1fe03
	if (info->invert)
Packit Service d1fe03
		op = XT_OP_NEQ;
Packit Service d1fe03
Packit Service d1fe03
	xt_xlate_add(xl, "ct mark");
Packit Service d1fe03
	print_mark_xlate(info->mark, info->mask, xl, op);
Packit Service d1fe03
Packit Service d1fe03
	return 1;
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static struct xtables_match connmark_mt_reg[] = {
Packit Service d1fe03
	{
Packit Service d1fe03
		.family        = NFPROTO_UNSPEC,
Packit Service d1fe03
		.name          = "connmark",
Packit Service d1fe03
		.revision      = 0,
Packit Service d1fe03
		.version       = XTABLES_VERSION,
Packit Service d1fe03
		.size          = XT_ALIGN(sizeof(struct xt_connmark_info)),
Packit Service d1fe03
		.userspacesize = XT_ALIGN(sizeof(struct xt_connmark_info)),
Packit Service d1fe03
		.help          = connmark_mt_help,
Packit Service d1fe03
		.print         = connmark_print,
Packit Service d1fe03
		.save          = connmark_save,
Packit Service d1fe03
		.x6_parse      = connmark_parse,
Packit Service d1fe03
		.x6_options    = connmark_mt_opts,
Packit Service d1fe03
		.xlate	       = connmark_xlate,
Packit Service d1fe03
	},
Packit Service d1fe03
	{
Packit Service d1fe03
		.version       = XTABLES_VERSION,
Packit Service d1fe03
		.name          = "connmark",
Packit Service d1fe03
		.revision      = 1,
Packit Service d1fe03
		.family        = NFPROTO_UNSPEC,
Packit Service d1fe03
		.size          = XT_ALIGN(sizeof(struct xt_connmark_mtinfo1)),
Packit Service d1fe03
		.userspacesize = XT_ALIGN(sizeof(struct xt_connmark_mtinfo1)),
Packit Service d1fe03
		.help          = connmark_mt_help,
Packit Service d1fe03
		.print         = connmark_mt_print,
Packit Service d1fe03
		.save          = connmark_mt_save,
Packit Service d1fe03
		.x6_parse      = connmark_mt_parse,
Packit Service d1fe03
		.x6_options    = connmark_mt_opts,
Packit Service d1fe03
		.xlate	       = connmark_mt_xlate,
Packit Service d1fe03
	},
Packit Service d1fe03
};
Packit Service d1fe03
Packit Service d1fe03
void _init(void)
Packit Service d1fe03
{
Packit Service d1fe03
	xtables_register_matches(connmark_mt_reg, ARRAY_SIZE(connmark_mt_reg));
Packit Service d1fe03
}