summaryrefslogtreecommitdiffstats
path: root/extensions/libebt_mark_m.c
blob: 244fe12a58bb21f00aa98b7ccc7024fd6540977c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* ebt_mark_m
 *
 * Authors:
 * Bart De Schuymer <bdschuym@pandora.be>
 *
 * July, 2002
 *
 * Adapted by Arturo Borrero Gonzalez <arturo@debian.org>
 * to use libxtables for ebtables-compat in 2015.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <xtables.h>
#include <linux/netfilter_bridge/ebt_mark_m.h>

#define MARK '1'

static const struct option brmark_m_opts[] = {
	{ .name = "mark",	.has_arg = true, .val = MARK },
	XT_GETOPT_TABLEEND,
};

static void brmark_m_print_help(void)
{
	printf(
"mark option:\n"
"--mark    [!] [value][/mask]: Match nfmask value (see man page)\n");
}

static void brmark_m_init(struct xt_entry_match *match)
{
	struct ebt_mark_m_info *info = (struct ebt_mark_m_info *)match->data;

	info->mark = 0;
	info->mask = 0;
	info->invert = 0;
	info->bitmask = 0;
}

#define OPT_MARK 0x01
static int
brmark_m_parse(int c, char **argv, int invert, unsigned int *flags,
	       const void *entry, struct xt_entry_match **match)
{
	struct ebt_mark_m_info *info = (struct ebt_mark_m_info *)
				       (*match)->data;
	char *end;

	switch (c) {
	case MARK:
		if (invert)
			info->invert = 1;
		info->mark = strtoul(optarg, &end, 0);
		info->bitmask = EBT_MARK_AND;
		if (*end == '/') {
			if (end == optarg)
				info->bitmask = EBT_MARK_OR;
			info->mask = strtoul(end+1, &end, 0);
		} else {
			info->mask = 0xffffffff;
		}
		if (*end != '\0' || end == optarg)
			xtables_error(PARAMETER_PROBLEM, "Bad mark value '%s'",
				      optarg);
		break;
	default:
		return 0;
	}

	*flags |= info->bitmask;
	return 1;
}

static void brmark_m_final_check(unsigned int flags)
{
	if (!flags)
		xtables_error(PARAMETER_PROBLEM,
			      "You must specify proper arguments");
}

static void brmark_m_print(const void *ip, const struct xt_entry_match *match,
			   int numeric)
{
	struct ebt_mark_m_info *info = (struct ebt_mark_m_info *)match->data;

	printf("--mark ");
	if (info->invert)
		printf("! ");
	if (info->bitmask == EBT_MARK_OR)
		printf("/0x%lx ", info->mask);
	else if (info->mask != 0xffffffff)
		printf("0x%lx/0x%lx ", info->mark, info->mask);
	else
		printf("0x%lx ", info->mark);
}

static int brmark_m_xlate(struct xt_xlate *xl,
			  const struct xt_xlate_mt_params *params)
{
	const struct ebt_mark_m_info *info = (const void*)params->match->data;
	enum xt_op op = XT_OP_EQ;

	if (info->invert)
		op = XT_OP_NEQ;

	xt_xlate_add(xl, "meta mark ");

	if (info->bitmask == EBT_MARK_OR) {
		xt_xlate_add(xl, "and 0x%x %s0 ", info->mask,
			     info->invert ? "" : "!= ");
	} else if (info->mask != 0xffffffffU) {
		xt_xlate_add(xl, "and 0x%x %s0x%x ", info->mask,
			   op == XT_OP_EQ ? "" : "!= ", info->mark);
	} else {
		xt_xlate_add(xl, "%s0x%x ",
			   op == XT_OP_EQ ? "" : "!= ", info->mark);
	}

	return 1;
}
static struct xtables_match brmark_m_match = {
	.name		= "mark_m",
	.revision	= 0,
	.version	= XTABLES_VERSION,
	.family		= NFPROTO_BRIDGE,
	.size		= XT_ALIGN(sizeof(struct ebt_mark_m_info)),
	.userspacesize	= XT_ALIGN(sizeof(struct ebt_mark_m_info)),
	.init		= brmark_m_init,
	.help		= brmark_m_print_help,
	.parse		= brmark_m_parse,
	.final_check	= brmark_m_final_check,
	.print		= brmark_m_print,
	.xlate		= brmark_m_xlate,
	.extra_opts	= brmark_m_opts,
};

void _init(void)
{
	xtables_register_match(&brmark_m_match);
}