summaryrefslogtreecommitdiffstats
path: root/extensions/libipt_realm.c
blob: e01d048e902bf52d7418c5d739274919d86a8ec6 (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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#if defined(__GLIBC__) && __GLIBC__ == 2
#include <net/ethernet.h>
#else
#include <linux/if_ether.h>
#endif
#include <xtables.h>
#include <linux/netfilter_ipv4/ipt_realm.h>

enum {
	O_REALM = 0,
};

static void realm_help(void)
{
	printf(
"realm match options:\n"
"[!] --realm value[/mask]\n"
"				Match realm\n");
}

static const struct xt_option_entry realm_opts[] = {
	{.name = "realm", .id = O_REALM, .type = XTTYPE_STRING,
	 .flags = XTOPT_MAND | XTOPT_INVERT},
	XTOPT_TABLEEND,
};

static const char f_realms[] = "/etc/iproute2/rt_realms";
/* array of realms from f_realms[] */
static struct xtables_lmap *realms;

static void realm_parse(struct xt_option_call *cb)
{
	struct xt_realm_info *ri = cb->data;
	unsigned int id, mask;

	xtables_option_parse(cb);
	xtables_parse_val_mask(cb, &id, &mask, realms);

	ri->id = id;
	ri->mask = mask;

	if (cb->invert)
		ri->invert = 1;
}

static void realm_print(const void *ip, const struct xt_entry_match *match,
			int numeric)
{
	const struct xt_realm_info *ri = (const void *)match->data;

	if (ri->invert)
		printf(" !");

	printf(" realm");
	xtables_print_val_mask(ri->id, ri->mask, numeric ? NULL : realms);
}

static void realm_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_realm_info *ri = (const void *)match->data;

	if (ri->invert)
		printf(" !");

	printf(" --realm");
	xtables_print_val_mask(ri->id, ri->mask, realms);
}

static void
print_realm_xlate(unsigned long id, unsigned long mask,
		  int numeric, struct xt_xlate *xl, uint32_t op)
{
	const char *name = NULL;

	if (mask != 0xffffffff)
		xt_xlate_add(xl, " and 0x%lx %s 0x%lx", mask,
			   op == XT_OP_EQ ? "==" : "!=", id);
	else {
		if (numeric == 0)
			name = xtables_lmap_id2name(realms, id);
		if (name)
			xt_xlate_add(xl, " %s%s",
				   op == XT_OP_EQ ? "" : "!= ", name);
		else
			xt_xlate_add(xl, " %s0x%lx",
				   op == XT_OP_EQ ? "" : "!= ", id);
	}
}

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

	if (ri->invert)
		op = XT_OP_NEQ;

	xt_xlate_add(xl, "rtclassid");
	print_realm_xlate(ri->id, ri->mask, 0, xl, op);

	return 1;
}

static struct xtables_match realm_mt_reg = {
	.name		= "realm",
	.version	= XTABLES_VERSION,
	.family		= NFPROTO_IPV4,
	.size		= XT_ALIGN(sizeof(struct xt_realm_info)),
	.userspacesize	= XT_ALIGN(sizeof(struct xt_realm_info)),
	.help		= realm_help,
	.print		= realm_print,
	.save		= realm_save,
	.x6_parse	= realm_parse,
	.x6_options	= realm_opts,
	.xlate		= realm_xlate,
};

void _init(void)
{
	realms = xtables_lmap_init(f_realms);
	if (realms == NULL && errno != ENOENT)
		fprintf(stderr, "Warning: %s: %s\n", f_realms,
			strerror(errno));

	xtables_register_match(&realm_mt_reg);
}