summaryrefslogtreecommitdiff
path: root/extensions/libxt_cluster.c
blob: 3adff12c7d9e0a4da960a80d1b4c4726288d1a81 (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
/*
 * (C) 2009 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <stdio.h>
#include <xtables.h>
#include <linux/netfilter/xt_cluster.h>

static void
cluster_help(void)
{
	printf(
"cluster match options:\n"
"  --cluster-total-nodes <num>		Set number of total nodes in cluster\n"
"  [!] --cluster-local-node <num>	Set the local node number\n"
"  [!] --cluster-local-nodemask <num>	Set the local node mask\n"
"  --cluster-hash-seed <num>		Set seed value of the Jenkins hash\n");
}

enum {
	O_CL_TOTAL_NODES = 0,
	O_CL_LOCAL_NODE,
	O_CL_LOCAL_NODEMASK,
	O_CL_HASH_SEED,
	F_CL_TOTAL_NODES    = 1 << O_CL_TOTAL_NODES,
	F_CL_LOCAL_NODE     = 1 << O_CL_LOCAL_NODE,
	F_CL_LOCAL_NODEMASK = 1 << O_CL_LOCAL_NODEMASK,
	F_CL_HASH_SEED      = 1 << O_CL_HASH_SEED,
};

#define s struct xt_cluster_match_info
static const struct xt_option_entry cluster_opts[] = {
	{.name = "cluster-total-nodes", .id = O_CL_TOTAL_NODES,
	 .type = XTTYPE_UINT32, .min = 1, .max = XT_CLUSTER_NODES_MAX,
	 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, total_nodes)},
	{.name = "cluster-local-node", .id = O_CL_LOCAL_NODE,
	 .excl = F_CL_LOCAL_NODEMASK, .flags = XTOPT_INVERT,
	 .type = XTTYPE_UINT32, .min = 1, .max = XT_CLUSTER_NODES_MAX},
	{.name = "cluster-local-nodemask", .id = O_CL_LOCAL_NODEMASK,
	 .excl = F_CL_LOCAL_NODE, .type = XTTYPE_UINT32,
	 .min = 1, .max = XT_CLUSTER_NODES_MAX,
	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, node_mask)},
	{.name = "cluster-hash-seed", .id = O_CL_HASH_SEED,
	 .type = XTTYPE_UINT32, .flags = XTOPT_MAND | XTOPT_PUT,
	 XTOPT_POINTER(s, hash_seed)},
	XTOPT_TABLEEND,
};

static void cluster_parse(struct xt_option_call *cb)
{
	struct xt_cluster_match_info *info = cb->data;

	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_CL_LOCAL_NODE:
		if (cb->invert)
			info->flags |= XT_CLUSTER_F_INV;
		info->node_mask = 1 << (cb->val.u32 - 1);
		break;
	case O_CL_LOCAL_NODEMASK:
		if (cb->invert)
			info->flags |= XT_CLUSTER_F_INV;
		break;
	}
}

static void cluster_check(struct xt_fcheck_call *cb)
{
	const struct xt_cluster_match_info *info = cb->data;
	unsigned int test;

	test = F_CL_TOTAL_NODES | F_CL_LOCAL_NODE | F_CL_HASH_SEED;
	if ((cb->xflags & test) == test) {
		if (info->node_mask >= (1ULL << info->total_nodes))
			xtables_error(PARAMETER_PROBLEM,
				      "cluster match: "
				      "`--cluster-local-node' "
				      "must be <= `--cluster-total-nodes'");
		return;
	}

	test = F_CL_TOTAL_NODES | F_CL_LOCAL_NODEMASK | F_CL_HASH_SEED;
	if ((cb->xflags & test) == test) {
		if (info->node_mask >= (1ULL << info->total_nodes))
			xtables_error(PARAMETER_PROBLEM,
				      "cluster match: "
				      "`--cluster-local-nodemask' too big "
				      "for `--cluster-total-nodes'");
		return;
	}
	if (!(cb->xflags & (F_CL_LOCAL_NODE | F_CL_LOCAL_NODEMASK)))
		xtables_error(PARAMETER_PROBLEM,
			      "cluster match: `--cluster-local-node' or"
			      "`--cluster-local-nodemask' is missing");
}

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

	printf(" cluster ");
	if (info->flags & XT_CLUSTER_F_INV)
		printf("!node_mask=0x%08x", info->node_mask);
	else
		printf("node_mask=0x%08x", info->node_mask);

	printf(" total_nodes=%u hash_seed=0x%08x",
		info->total_nodes, info->hash_seed);
}

static void
cluster_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_cluster_match_info *info = (void *)match->data;

	if (info->flags & XT_CLUSTER_F_INV)
		printf(" ! --cluster-local-nodemask 0x%08x", info->node_mask);
	else
		printf(" --cluster-local-nodemask 0x%08x", info->node_mask);

	printf(" --cluster-total-nodes %u --cluster-hash-seed 0x%08x",
		info->total_nodes, info->hash_seed);
}

static struct xtables_match cluster_mt_reg = {
	.family		= NFPROTO_UNSPEC,
	.name		= "cluster",
	.version	= XTABLES_VERSION,
	.size		= XT_ALIGN(sizeof(struct xt_cluster_match_info)),
	.userspacesize  = XT_ALIGN(sizeof(struct xt_cluster_match_info)),
 	.help		= cluster_help,
	.print		= cluster_print,
	.save		= cluster_save,
	.x6_parse	= cluster_parse,
	.x6_fcheck	= cluster_check,
	.x6_options	= cluster_opts,
};

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