blob: 3adff12c7d9e0a4da960a80d1b4c4726288d1a81 [file] [log] [blame]
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +02001/*
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>
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +02009#include <xtables.h>
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020010#include <linux/netfilter/xt_cluster.h>
11
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020012static void
13cluster_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
23enum {
Jan Engelhardtb18ffe32011-02-27 17:52:23 +010024 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,
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020032};
33
Jan Engelhardtb18ffe32011-02-27 17:52:23 +010034#define s struct xt_cluster_match_info
35static 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,
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020050};
51
Jan Engelhardtb18ffe32011-02-27 17:52:23 +010052static void cluster_parse(struct xt_option_call *cb)
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020053{
Jan Engelhardtb18ffe32011-02-27 17:52:23 +010054 struct xt_cluster_match_info *info = cb->data;
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020055
Jan Engelhardtb18ffe32011-02-27 17:52:23 +010056 xtables_option_parse(cb);
57 switch (cb->entry->id) {
58 case O_CL_LOCAL_NODE:
59 if (cb->invert)
Pablo Neira Ayusoe76ec992011-02-06 21:34:33 +010060 info->flags |= XT_CLUSTER_F_INV;
Jan Engelhardtb18ffe32011-02-27 17:52:23 +010061 info->node_mask = 1 << (cb->val.u32 - 1);
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020062 break;
Jan Engelhardtb18ffe32011-02-27 17:52:23 +010063 case O_CL_LOCAL_NODEMASK:
64 if (cb->invert)
Pablo Neira Ayusoe76ec992011-02-06 21:34:33 +010065 info->flags |= XT_CLUSTER_F_INV;
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020066 break;
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020067 }
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020068}
69
Jan Engelhardtb18ffe32011-02-27 17:52:23 +010070static void cluster_check(struct xt_fcheck_call *cb)
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020071{
Jan Engelhardtb18ffe32011-02-27 17:52:23 +010072 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))
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020078 xtables_error(PARAMETER_PROBLEM,
79 "cluster match: "
80 "`--cluster-local-node' "
81 "must be <= `--cluster-total-nodes'");
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020082 return;
83 }
Jan Engelhardtb18ffe32011-02-27 17:52:23 +010084
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))
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020088 xtables_error(PARAMETER_PROBLEM,
89 "cluster match: "
90 "`--cluster-local-nodemask' too big "
91 "for `--cluster-total-nodes'");
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020092 return;
93 }
Jan Engelhardtb18ffe32011-02-27 17:52:23 +010094 if (!(cb->xflags & (F_CL_LOCAL_NODE | F_CL_LOCAL_NODEMASK)))
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020095 xtables_error(PARAMETER_PROBLEM,
96 "cluster match: `--cluster-local-node' or"
97 "`--cluster-local-nodemask' is missing");
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +020098}
99
100static void
101cluster_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
Jan Engelhardt73866352010-12-18 02:04:59 +0100105 printf(" cluster ");
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +0200106 if (info->flags & XT_CLUSTER_F_INV)
Jan Engelhardt73866352010-12-18 02:04:59 +0100107 printf("!node_mask=0x%08x", info->node_mask);
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +0200108 else
Jan Engelhardt73866352010-12-18 02:04:59 +0100109 printf("node_mask=0x%08x", info->node_mask);
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +0200110
Jan Engelhardt73866352010-12-18 02:04:59 +0100111 printf(" total_nodes=%u hash_seed=0x%08x",
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +0200112 info->total_nodes, info->hash_seed);
113}
114
115static void
116cluster_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)
Jan Engelhardt73866352010-12-18 02:04:59 +0100121 printf(" ! --cluster-local-nodemask 0x%08x", info->node_mask);
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +0200122 else
Jan Engelhardt73866352010-12-18 02:04:59 +0100123 printf(" --cluster-local-nodemask 0x%08x", info->node_mask);
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +0200124
Jan Engelhardt73866352010-12-18 02:04:59 +0100125 printf(" --cluster-total-nodes %u --cluster-hash-seed 0x%08x",
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +0200126 info->total_nodes, info->hash_seed);
127}
128
129static struct xtables_match cluster_mt_reg = {
Jan Engelhardt42979362009-06-01 11:56:23 +0200130 .family = NFPROTO_UNSPEC,
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +0200131 .name = "cluster",
132 .version = XTABLES_VERSION,
133 .size = XT_ALIGN(sizeof(struct xt_cluster_match_info)),
134 .userspacesize = XT_ALIGN(sizeof(struct xt_cluster_match_info)),
135 .help = cluster_help,
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +0200136 .print = cluster_print,
137 .save = cluster_save,
Jan Engelhardtb18ffe32011-02-27 17:52:23 +0100138 .x6_parse = cluster_parse,
139 .x6_fcheck = cluster_check,
140 .x6_options = cluster_opts,
Pablo Neira Ayusocd958a62009-05-06 13:01:20 +0200141};
142
143void _init(void)
144{
145 xtables_register_match(&cluster_mt_reg);
146}