• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * (C) 2009 by Pablo Neira Ayuso <pablo@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 #include <stdio.h>
9 #include <xtables.h>
10 #include <linux/netfilter/xt_cluster.h>
11 
12 static void
cluster_help(void)13 cluster_help(void)
14 {
15 	printf(
16 "cluster match options:\n"
17 "  --cluster-total-nodes <num>		Set number of total nodes in cluster\n"
18 "  [!] --cluster-local-node <num>	Set the local node number\n"
19 "  [!] --cluster-local-nodemask <num>	Set the local node mask\n"
20 "  --cluster-hash-seed <num>		Set seed value of the Jenkins hash\n");
21 }
22 
23 enum {
24 	O_CL_TOTAL_NODES = 0,
25 	O_CL_LOCAL_NODE,
26 	O_CL_LOCAL_NODEMASK,
27 	O_CL_HASH_SEED,
28 	F_CL_TOTAL_NODES    = 1 << O_CL_TOTAL_NODES,
29 	F_CL_LOCAL_NODE     = 1 << O_CL_LOCAL_NODE,
30 	F_CL_LOCAL_NODEMASK = 1 << O_CL_LOCAL_NODEMASK,
31 	F_CL_HASH_SEED      = 1 << O_CL_HASH_SEED,
32 };
33 
34 #define s struct xt_cluster_match_info
35 static const struct xt_option_entry cluster_opts[] = {
36 	{.name = "cluster-total-nodes", .id = O_CL_TOTAL_NODES,
37 	 .type = XTTYPE_UINT32, .min = 1, .max = XT_CLUSTER_NODES_MAX,
38 	 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, total_nodes)},
39 	{.name = "cluster-local-node", .id = O_CL_LOCAL_NODE,
40 	 .excl = F_CL_LOCAL_NODEMASK, .flags = XTOPT_INVERT,
41 	 .type = XTTYPE_UINT32, .min = 1, .max = XT_CLUSTER_NODES_MAX},
42 	{.name = "cluster-local-nodemask", .id = O_CL_LOCAL_NODEMASK,
43 	 .excl = F_CL_LOCAL_NODE, .type = XTTYPE_UINT32,
44 	 .min = 1, .max = XT_CLUSTER_NODES_MAX,
45 	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, node_mask)},
46 	{.name = "cluster-hash-seed", .id = O_CL_HASH_SEED,
47 	 .type = XTTYPE_UINT32, .flags = XTOPT_MAND | XTOPT_PUT,
48 	 XTOPT_POINTER(s, hash_seed)},
49 	XTOPT_TABLEEND,
50 };
51 
cluster_parse(struct xt_option_call * cb)52 static void cluster_parse(struct xt_option_call *cb)
53 {
54 	struct xt_cluster_match_info *info = cb->data;
55 
56 	xtables_option_parse(cb);
57 	switch (cb->entry->id) {
58 	case O_CL_LOCAL_NODE:
59 		if (cb->invert)
60 			info->flags |= XT_CLUSTER_F_INV;
61 		info->node_mask = 1 << (cb->val.u32 - 1);
62 		break;
63 	case O_CL_LOCAL_NODEMASK:
64 		if (cb->invert)
65 			info->flags |= XT_CLUSTER_F_INV;
66 		break;
67 	}
68 }
69 
cluster_check(struct xt_fcheck_call * cb)70 static void cluster_check(struct xt_fcheck_call *cb)
71 {
72 	const struct xt_cluster_match_info *info = cb->data;
73 	unsigned int test;
74 
75 	test = F_CL_TOTAL_NODES | F_CL_LOCAL_NODE | F_CL_HASH_SEED;
76 	if ((cb->xflags & test) == test) {
77 		if (info->node_mask >= (1ULL << info->total_nodes))
78 			xtables_error(PARAMETER_PROBLEM,
79 				      "cluster match: "
80 				      "`--cluster-local-node' "
81 				      "must be <= `--cluster-total-nodes'");
82 		return;
83 	}
84 
85 	test = F_CL_TOTAL_NODES | F_CL_LOCAL_NODEMASK | F_CL_HASH_SEED;
86 	if ((cb->xflags & test) == test) {
87 		if (info->node_mask >= (1ULL << info->total_nodes))
88 			xtables_error(PARAMETER_PROBLEM,
89 				      "cluster match: "
90 				      "`--cluster-local-nodemask' too big "
91 				      "for `--cluster-total-nodes'");
92 		return;
93 	}
94 	if (!(cb->xflags & (F_CL_LOCAL_NODE | F_CL_LOCAL_NODEMASK)))
95 		xtables_error(PARAMETER_PROBLEM,
96 			      "cluster match: `--cluster-local-node' or"
97 			      "`--cluster-local-nodemask' is missing");
98 }
99 
100 static void
cluster_print(const void * ip,const struct xt_entry_match * match,int numeric)101 cluster_print(const void *ip, const struct xt_entry_match *match, int numeric)
102 {
103 	const struct xt_cluster_match_info *info = (void *)match->data;
104 
105 	printf(" cluster ");
106 	if (info->flags & XT_CLUSTER_F_INV)
107 		printf("!node_mask=0x%08x", info->node_mask);
108 	else
109 		printf("node_mask=0x%08x", info->node_mask);
110 
111 	printf(" total_nodes=%u hash_seed=0x%08x",
112 		info->total_nodes, info->hash_seed);
113 }
114 
115 static void
cluster_save(const void * ip,const struct xt_entry_match * match)116 cluster_save(const void *ip, const struct xt_entry_match *match)
117 {
118 	const struct xt_cluster_match_info *info = (void *)match->data;
119 
120 	if (info->flags & XT_CLUSTER_F_INV)
121 		printf(" ! --cluster-local-nodemask 0x%08x", info->node_mask);
122 	else
123 		printf(" --cluster-local-nodemask 0x%08x", info->node_mask);
124 
125 	printf(" --cluster-total-nodes %u --cluster-hash-seed 0x%08x",
126 		info->total_nodes, info->hash_seed);
127 }
128 
cluster_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)129 static int cluster_xlate(struct xt_xlate *xl,
130 			 const struct xt_xlate_mt_params *params)
131 {
132 	int node, shift_value = 1, comma_needed = 0;
133 	uint32_t temp_node_mask, node_id = 0, needs_set = 0;
134 	const struct xt_cluster_match_info *info = (void *)params->match->data;
135 	const char *jhash_st = "jhash ct original saddr mod";
136 	const char *pkttype_st = "meta pkttype set host";
137 
138 	if (!(info->node_mask & (info->node_mask - 1))) {
139 		if (info->node_mask <= 2)
140 			xt_xlate_add(xl, "%s %u seed 0x%08x eq %u %s", jhash_st,
141 					info->total_nodes, info->hash_seed,
142 					info->node_mask, pkttype_st);
143 		else {
144 			temp_node_mask = info->node_mask;
145 			while (1) {
146 				temp_node_mask = temp_node_mask >> shift_value;
147 				node_id++;
148 				if (temp_node_mask == 0)
149 					break;
150 			}
151 			xt_xlate_add(xl, "%s %u seed 0x%08x eq %u %s", jhash_st,
152 					info->total_nodes, info->hash_seed,
153 					node_id, pkttype_st);
154 		}
155 	} else {
156 		xt_xlate_add(xl, "%s %u seed 0x%08x ", jhash_st,
157 				info->total_nodes, info->hash_seed);
158 		for (node = 0; node < 32; node++) {
159 			if (info->node_mask & (1 << node)) {
160 				if (needs_set == 0) {
161 					xt_xlate_add(xl, "{ ");
162 					needs_set = 1;
163 				}
164 
165 				if (comma_needed)
166 					xt_xlate_add(xl, ", ");
167 				xt_xlate_add(xl, "%u", node);
168 				comma_needed++;
169 			}
170 		}
171 		if (needs_set)
172 			xt_xlate_add(xl, " }");
173 		xt_xlate_add(xl, " %s", pkttype_st);
174 	}
175 
176 	return 1;
177 }
178 
179 static struct xtables_match cluster_mt_reg = {
180 	.family		= NFPROTO_UNSPEC,
181 	.name		= "cluster",
182 	.version	= XTABLES_VERSION,
183 	.size		= XT_ALIGN(sizeof(struct xt_cluster_match_info)),
184 	.userspacesize  = XT_ALIGN(sizeof(struct xt_cluster_match_info)),
185  	.help		= cluster_help,
186 	.print		= cluster_print,
187 	.save		= cluster_save,
188 	.x6_parse	= cluster_parse,
189 	.x6_fcheck	= cluster_check,
190 	.x6_options	= cluster_opts,
191 	.xlate		= cluster_xlate,
192 };
193 
_init(void)194 void _init(void)
195 {
196 	xtables_register_match(&cluster_mt_reg);
197 }
198