Blame extensions/libxt_cluster.c

Packit 7b22a4
/*
Packit 7b22a4
 * (C) 2009 by Pablo Neira Ayuso <pablo@netfilter.org>
Packit 7b22a4
 *
Packit 7b22a4
 * This program is free software; you can redistribute it and/or modify
Packit 7b22a4
 * it under the terms of the GNU General Public License version 2 as
Packit 7b22a4
 * published by the Free Software Foundation.
Packit 7b22a4
 */
Packit 7b22a4
#include <stdio.h>
Packit 7b22a4
#include <xtables.h>
Packit 7b22a4
#include <linux/netfilter/xt_cluster.h>
Packit 7b22a4
Packit 7b22a4
static void
Packit 7b22a4
cluster_help(void)
Packit 7b22a4
{
Packit 7b22a4
	printf(
Packit 7b22a4
"cluster match options:\n"
Packit 7b22a4
"  --cluster-total-nodes <num>		Set number of total nodes in cluster\n"
Packit 7b22a4
"  [!] --cluster-local-node <num>	Set the local node number\n"
Packit 7b22a4
"  [!] --cluster-local-nodemask <num>	Set the local node mask\n"
Packit 7b22a4
"  --cluster-hash-seed <num>		Set seed value of the Jenkins hash\n");
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
enum {
Packit 7b22a4
	O_CL_TOTAL_NODES = 0,
Packit 7b22a4
	O_CL_LOCAL_NODE,
Packit 7b22a4
	O_CL_LOCAL_NODEMASK,
Packit 7b22a4
	O_CL_HASH_SEED,
Packit 7b22a4
	F_CL_TOTAL_NODES    = 1 << O_CL_TOTAL_NODES,
Packit 7b22a4
	F_CL_LOCAL_NODE     = 1 << O_CL_LOCAL_NODE,
Packit 7b22a4
	F_CL_LOCAL_NODEMASK = 1 << O_CL_LOCAL_NODEMASK,
Packit 7b22a4
	F_CL_HASH_SEED      = 1 << O_CL_HASH_SEED,
Packit 7b22a4
};
Packit 7b22a4
Packit 7b22a4
#define s struct xt_cluster_match_info
Packit 7b22a4
static const struct xt_option_entry cluster_opts[] = {
Packit 7b22a4
	{.name = "cluster-total-nodes", .id = O_CL_TOTAL_NODES,
Packit 7b22a4
	 .type = XTTYPE_UINT32, .min = 1, .max = XT_CLUSTER_NODES_MAX,
Packit 7b22a4
	 .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, total_nodes)},
Packit 7b22a4
	{.name = "cluster-local-node", .id = O_CL_LOCAL_NODE,
Packit 7b22a4
	 .excl = F_CL_LOCAL_NODEMASK, .flags = XTOPT_INVERT,
Packit 7b22a4
	 .type = XTTYPE_UINT32, .min = 1, .max = XT_CLUSTER_NODES_MAX},
Packit 7b22a4
	{.name = "cluster-local-nodemask", .id = O_CL_LOCAL_NODEMASK,
Packit 7b22a4
	 .excl = F_CL_LOCAL_NODE, .type = XTTYPE_UINT32,
Packit 7b22a4
	 .min = 1, .max = XT_CLUSTER_NODES_MAX,
Packit 7b22a4
	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, node_mask)},
Packit 7b22a4
	{.name = "cluster-hash-seed", .id = O_CL_HASH_SEED,
Packit 7b22a4
	 .type = XTTYPE_UINT32, .flags = XTOPT_MAND | XTOPT_PUT,
Packit 7b22a4
	 XTOPT_POINTER(s, hash_seed)},
Packit 7b22a4
	XTOPT_TABLEEND,
Packit 7b22a4
};
Packit 7b22a4
Packit 7b22a4
static void cluster_parse(struct xt_option_call *cb)
Packit 7b22a4
{
Packit 7b22a4
	struct xt_cluster_match_info *info = cb->data;
Packit 7b22a4
Packit 7b22a4
	xtables_option_parse(cb);
Packit 7b22a4
	switch (cb->entry->id) {
Packit 7b22a4
	case O_CL_LOCAL_NODE:
Packit 7b22a4
		if (cb->invert)
Packit 7b22a4
			info->flags |= XT_CLUSTER_F_INV;
Packit 7b22a4
		info->node_mask = 1 << (cb->val.u32 - 1);
Packit 7b22a4
		break;
Packit 7b22a4
	case O_CL_LOCAL_NODEMASK:
Packit 7b22a4
		if (cb->invert)
Packit 7b22a4
			info->flags |= XT_CLUSTER_F_INV;
Packit 7b22a4
		break;
Packit 7b22a4
	}
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
static void cluster_check(struct xt_fcheck_call *cb)
Packit 7b22a4
{
Packit 7b22a4
	const struct xt_cluster_match_info *info = cb->data;
Packit 7b22a4
	unsigned int test;
Packit 7b22a4
Packit 7b22a4
	test = F_CL_TOTAL_NODES | F_CL_LOCAL_NODE | F_CL_HASH_SEED;
Packit 7b22a4
	if ((cb->xflags & test) == test) {
Packit 7b22a4
		if (info->node_mask >= (1ULL << info->total_nodes))
Packit 7b22a4
			xtables_error(PARAMETER_PROBLEM,
Packit 7b22a4
				      "cluster match: "
Packit 7b22a4
				      "`--cluster-local-node' "
Packit 7b22a4
				      "must be <= `--cluster-total-nodes'");
Packit 7b22a4
		return;
Packit 7b22a4
	}
Packit 7b22a4
Packit 7b22a4
	test = F_CL_TOTAL_NODES | F_CL_LOCAL_NODEMASK | F_CL_HASH_SEED;
Packit 7b22a4
	if ((cb->xflags & test) == test) {
Packit 7b22a4
		if (info->node_mask >= (1ULL << info->total_nodes))
Packit 7b22a4
			xtables_error(PARAMETER_PROBLEM,
Packit 7b22a4
				      "cluster match: "
Packit 7b22a4
				      "`--cluster-local-nodemask' too big "
Packit 7b22a4
				      "for `--cluster-total-nodes'");
Packit 7b22a4
		return;
Packit 7b22a4
	}
Packit 7b22a4
	if (!(cb->xflags & (F_CL_LOCAL_NODE | F_CL_LOCAL_NODEMASK)))
Packit 7b22a4
		xtables_error(PARAMETER_PROBLEM,
Packit 7b22a4
			      "cluster match: `--cluster-local-node' or"
Packit 7b22a4
			      "`--cluster-local-nodemask' is missing");
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
static void
Packit 7b22a4
cluster_print(const void *ip, const struct xt_entry_match *match, int numeric)
Packit 7b22a4
{
Packit 7b22a4
	const struct xt_cluster_match_info *info = (void *)match->data;
Packit 7b22a4
Packit 7b22a4
	printf(" cluster ");
Packit 7b22a4
	if (info->flags & XT_CLUSTER_F_INV)
Packit 7b22a4
		printf("!node_mask=0x%08x", info->node_mask);
Packit 7b22a4
	else
Packit 7b22a4
		printf("node_mask=0x%08x", info->node_mask);
Packit 7b22a4
Packit 7b22a4
	printf(" total_nodes=%u hash_seed=0x%08x",
Packit 7b22a4
		info->total_nodes, info->hash_seed);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
static void
Packit 7b22a4
cluster_save(const void *ip, const struct xt_entry_match *match)
Packit 7b22a4
{
Packit 7b22a4
	const struct xt_cluster_match_info *info = (void *)match->data;
Packit 7b22a4
Packit 7b22a4
	if (info->flags & XT_CLUSTER_F_INV)
Packit 7b22a4
		printf(" ! --cluster-local-nodemask 0x%08x", info->node_mask);
Packit 7b22a4
	else
Packit 7b22a4
		printf(" --cluster-local-nodemask 0x%08x", info->node_mask);
Packit 7b22a4
Packit 7b22a4
	printf(" --cluster-total-nodes %u --cluster-hash-seed 0x%08x",
Packit 7b22a4
		info->total_nodes, info->hash_seed);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
static int cluster_xlate(struct xt_xlate *xl,
Packit 7b22a4
			 const struct xt_xlate_mt_params *params)
Packit 7b22a4
{
Packit 7b22a4
	int node, shift_value = 1, comma_needed = 0;
Packit 7b22a4
	uint32_t temp_node_mask, node_id = 0, needs_set = 0;
Packit 7b22a4
	const struct xt_cluster_match_info *info = (void *)params->match->data;
Packit 7b22a4
	const char *jhash_st = "jhash ct original saddr mod";
Packit 7b22a4
	const char *pkttype_st = "meta pkttype set host";
Packit 7b22a4
Packit 7b22a4
	if (!(info->node_mask & (info->node_mask - 1))) {
Packit 7b22a4
		if (info->node_mask <= 2)
Packit 7b22a4
			xt_xlate_add(xl, "%s %u seed 0x%08x eq %u %s", jhash_st,
Packit 7b22a4
					info->total_nodes, info->hash_seed,
Packit 7b22a4
					info->node_mask, pkttype_st);
Packit 7b22a4
		else {
Packit 7b22a4
			temp_node_mask = info->node_mask;
Packit 7b22a4
			while (1) {
Packit 7b22a4
				temp_node_mask = temp_node_mask >> shift_value;
Packit 7b22a4
				node_id++;
Packit 7b22a4
				if (temp_node_mask == 0)
Packit 7b22a4
					break;
Packit 7b22a4
			}
Packit 7b22a4
			xt_xlate_add(xl, "%s %u seed 0x%08x eq %u %s", jhash_st,
Packit 7b22a4
					info->total_nodes, info->hash_seed,
Packit 7b22a4
					node_id, pkttype_st);
Packit 7b22a4
		}
Packit 7b22a4
	} else {
Packit 7b22a4
		xt_xlate_add(xl, "%s %u seed 0x%08x ", jhash_st,
Packit 7b22a4
				info->total_nodes, info->hash_seed);
Packit 7b22a4
		for (node = 0; node < 32; node++) {
Packit Service 8e0764
			if (info->node_mask & (1u << node)) {
Packit 7b22a4
				if (needs_set == 0) {
Packit 7b22a4
					xt_xlate_add(xl, "{ ");
Packit 7b22a4
					needs_set = 1;
Packit 7b22a4
				}
Packit 7b22a4
Packit 7b22a4
				if (comma_needed)
Packit 7b22a4
					xt_xlate_add(xl, ", ");
Packit 7b22a4
				xt_xlate_add(xl, "%u", node);
Packit 7b22a4
				comma_needed++;
Packit 7b22a4
			}
Packit 7b22a4
		}
Packit 7b22a4
		if (needs_set)
Packit 7b22a4
			xt_xlate_add(xl, " }");
Packit 7b22a4
		xt_xlate_add(xl, " %s", pkttype_st);
Packit 7b22a4
	}
Packit 7b22a4
Packit 7b22a4
	return 1;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
static struct xtables_match cluster_mt_reg = {
Packit 7b22a4
	.family		= NFPROTO_UNSPEC,
Packit 7b22a4
	.name		= "cluster",
Packit 7b22a4
	.version	= XTABLES_VERSION,
Packit 7b22a4
	.size		= XT_ALIGN(sizeof(struct xt_cluster_match_info)),
Packit 7b22a4
	.userspacesize  = XT_ALIGN(sizeof(struct xt_cluster_match_info)),
Packit 7b22a4
 	.help		= cluster_help,
Packit 7b22a4
	.print		= cluster_print,
Packit 7b22a4
	.save		= cluster_save,
Packit 7b22a4
	.x6_parse	= cluster_parse,
Packit 7b22a4
	.x6_fcheck	= cluster_check,
Packit 7b22a4
	.x6_options	= cluster_opts,
Packit 7b22a4
	.xlate		= cluster_xlate,
Packit 7b22a4
};
Packit 7b22a4
Packit 7b22a4
void _init(void)
Packit 7b22a4
{
Packit 7b22a4
	xtables_register_match(&cluster_mt_reg);
Packit 7b22a4
}