Blame extensions/libxt_mark.c

Packit Service d1fe03
#include <stdbool.h>
Packit Service d1fe03
#include <stdio.h>
Packit Service d1fe03
#include <xtables.h>
Packit Service d1fe03
#include <linux/netfilter/xt_mark.h>
Packit Service d1fe03
Packit Service d1fe03
struct xt_mark_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 mark_mt_help(void)
Packit Service d1fe03
{
Packit Service d1fe03
	printf(
Packit Service d1fe03
"mark match options:\n"
Packit Service d1fe03
"[!] --mark value[/mask]    Match nfmark value with optional mask\n");
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static const struct xt_option_entry mark_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 mark_mt_parse(struct xt_option_call *cb)
Packit Service d1fe03
{
Packit Service d1fe03
	struct xt_mark_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 mark_parse(struct xt_option_call *cb)
Packit Service d1fe03
{
Packit Service d1fe03
	struct xt_mark_info *markinfo = cb->data;
Packit Service d1fe03
Packit Service d1fe03
	xtables_option_parse(cb);
Packit Service d1fe03
	if (cb->invert)
Packit Service d1fe03
		markinfo->invert = 1;
Packit Service d1fe03
	markinfo->mark = cb->val.mark;
Packit Service d1fe03
	markinfo->mask = cb->val.mask;
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static void
Packit Service d1fe03
mark_mt_print(const void *ip, const struct xt_entry_match *match, int numeric)
Packit Service d1fe03
{
Packit Service d1fe03
	const struct xt_mark_mtinfo1 *info = (const void *)match->data;
Packit Service d1fe03
Packit Service d1fe03
	printf(" mark 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
mark_print(const void *ip, const struct xt_entry_match *match, int numeric)
Packit Service d1fe03
{
Packit Service d1fe03
	const struct xt_mark_info *info = (const void *)match->data;
Packit Service d1fe03
Packit Service d1fe03
	printf(" MARK match");
Packit Service d1fe03
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 mark_mt_save(const void *ip, const struct xt_entry_match *match)
Packit Service d1fe03
{
Packit Service d1fe03
	const struct xt_mark_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
Packit Service d1fe03
mark_save(const void *ip, const struct xt_entry_match *match)
Packit Service d1fe03
{
Packit Service d1fe03
	const struct xt_mark_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
print_mark_xlate(struct xt_xlate *xl, unsigned int mark,
Packit Service d1fe03
		 unsigned int mask, 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 mark_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_mark_mtinfo1 *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, "mark");
Packit Service d1fe03
	print_mark_xlate(xl, info->mark, info->mask, op);
Packit Service d1fe03
Packit Service d1fe03
	return 1;
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static int mark_xlate(struct xt_xlate *xl,
Packit Service d1fe03
		      const struct xt_xlate_mt_params *params)
Packit Service d1fe03
{
Packit Service d1fe03
	const struct xt_mark_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, "mark");
Packit Service d1fe03
	print_mark_xlate(xl, info->mark, info->mask, op);
Packit Service d1fe03
Packit Service d1fe03
	return 1;
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static struct xtables_match mark_mt_reg[] = {
Packit Service d1fe03
	{
Packit Service d1fe03
		.family        = NFPROTO_UNSPEC,
Packit Service d1fe03
		.name          = "mark",
Packit Service d1fe03
		.revision      = 0,
Packit Service d1fe03
		.version       = XTABLES_VERSION,
Packit Service d1fe03
		.size          = XT_ALIGN(sizeof(struct xt_mark_info)),
Packit Service d1fe03
		.userspacesize = XT_ALIGN(sizeof(struct xt_mark_info)),
Packit Service d1fe03
		.help          = mark_mt_help,
Packit Service d1fe03
		.print         = mark_print,
Packit Service d1fe03
		.save          = mark_save,
Packit Service d1fe03
		.x6_parse      = mark_parse,
Packit Service d1fe03
		.x6_options    = mark_mt_opts,
Packit Service d1fe03
		.xlate	       = mark_xlate,
Packit Service d1fe03
	},
Packit Service d1fe03
	{
Packit Service d1fe03
		.version       = XTABLES_VERSION,
Packit Service d1fe03
		.name          = "mark",
Packit Service d1fe03
		.revision      = 1,
Packit Service d1fe03
		.family        = NFPROTO_UNSPEC,
Packit Service d1fe03
		.size          = XT_ALIGN(sizeof(struct xt_mark_mtinfo1)),
Packit Service d1fe03
		.userspacesize = XT_ALIGN(sizeof(struct xt_mark_mtinfo1)),
Packit Service d1fe03
		.help          = mark_mt_help,
Packit Service d1fe03
		.print         = mark_mt_print,
Packit Service d1fe03
		.save          = mark_mt_save,
Packit Service d1fe03
		.x6_parse      = mark_mt_parse,
Packit Service d1fe03
		.x6_options    = mark_mt_opts,
Packit Service d1fe03
		.xlate	       = mark_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(mark_mt_reg, ARRAY_SIZE(mark_mt_reg));
Packit Service d1fe03
}