summaryrefslogtreecommitdiffstats
path: root/extensions/arpt_MARK.c
blob: 3e5a9f96db8a752e3b203a48d7a0c1bfd9ee4539 (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
144
145
146
147
148
149
150
/*
 * (C) 2014 by Gao Feng <gaofeng@cn.fujitsu.com>
 *
 * arpt_MARK.c -- arptables extension to set mark for arp packet
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <getopt.h>
#include <arptables.h>
#include <linux/netfilter/xt_mark.h>
#include <linux/netfilter/xt_MARK.h>
#include <linux/netfilter/x_tables.h>

static void help(void)
{
	printf(
"MARK target v%s options:\n"
"--set-mark mark : set the mark value\n"
"--and-mark value : binary AND the mark with value\n"
"--or-mark value : binary OR the mark with value\n",
	ARPTABLES_VERSION);
}

#define MARK_OPT 1
#define AND_MARK_OPT 2
#define OR_MARK_OPT 3

static struct option opts[] = {
	{ .name = "set-mark", .has_arg = required_argument, .flag = 0, .val = MARK_OPT },
	{ .name = "and-mark", .has_arg = required_argument, .flag = 0, .val = AND_MARK_OPT },
	{ .name = "or-mark", .has_arg = required_argument, .flag = 0, .val =  OR_MARK_OPT },
	{ .name = NULL}
};

static void init(struct arpt_entry_target *t)
{
	struct xt_mark_tginfo2 *info = (struct xt_mark_tginfo2 *) t->data;

	info->mark = 0;
}

static int parse(int c, char **argv, int invert, unsigned int *flags,
		 const struct arpt_entry *e, struct arpt_entry_target **t)
{
	struct xt_mark_tginfo2 *info = (struct xt_mark_tginfo2 *)(*t)->data;
	int i;

	switch (c) {
	case MARK_OPT:
		if (sscanf(argv[optind-1], "%x", &i) != 1) {
			exit_error(PARAMETER_PROBLEM,
				"Bad mark value `%s'", optarg);
			return 0;
		}
		info->mark = i;
		if (*flags)
			exit_error(PARAMETER_PROBLEM,
				"MARK: Can't specify --set-mark twice");
		*flags = 1;
		break;
	case AND_MARK_OPT:
		if (sscanf(argv[optind-1], "%x", &i) != 1) {
			exit_error(PARAMETER_PROBLEM,
				"Bad mark value `%s'", optarg);
			return 0;
		}
		info->mark = 0;
		info->mask = ~i;
		if (*flags)
			exit_error(PARAMETER_PROBLEM,
				"MARK: Can't specify --and-mark twice");
		*flags = 1;
		break;
	case OR_MARK_OPT:
		if (sscanf(argv[optind-1], "%x", &i) != 1) {
			exit_error(PARAMETER_PROBLEM,
				"Bad mark value `%s'", optarg);
			return 0;
		}
		info->mark = info->mask = i;
		if (*flags)
			exit_error(PARAMETER_PROBLEM,
				"MARK: Can't specify --or-mark twice");
		*flags = 1;
		break;
	default:
		return 0;
	}
	return 1;
}

static void final_check(unsigned int flags)
{
	if (!flags)
		exit_error(PARAMETER_PROBLEM, "MARK: Parameter --set-mark/--and-mark/--or-mark is required");
}

static void print(const struct arpt_arp *ip,
		  const struct arpt_entry_target *target, int numeric)
{
	struct xt_mark_tginfo2 *info = (struct xt_mark_tginfo2 *)(target->data);

	if (info->mark == 0)
		printf("--and-mark %x", (unsigned int)(uint32_t)~info->mask);
	else if (info->mark == info->mask)
		printf("--or-mark %x", info->mark);
	else
		printf("--set-mark %x", info->mark);
}

static void save(const struct arpt_arp *ip,
		 const struct arpt_entry_target *target)
{
}

static struct arptables_target mark = {
	.next          = NULL,
	.name          = "MARK",
	.version       = ARPTABLES_VERSION,
	.size          = ARPT_ALIGN(sizeof(struct xt_mark_tginfo2)),
	.userspacesize = ARPT_ALIGN(sizeof(struct xt_mark_tginfo2)),
	.revision      = 2,
	.help          = help,
	.init          = init,
	.parse         = parse,
	.final_check   = final_check,
	.print         = print,
	.save          = save,
	.extra_opts    = opts
};

static void _init(void) __attribute__ ((constructor));
static void _init(void)
{
	register_target(&mark);
}