Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 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> |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 9 | #include <xtables.h> |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 10 | #include <linux/netfilter/xt_cluster.h> |
| 11 | |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 12 | static 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 { |
Jan Engelhardt | b18ffe3 | 2011-02-27 17:52:23 +0100 | [diff] [blame] | 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, |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
Jan Engelhardt | b18ffe3 | 2011-02-27 17:52:23 +0100 | [diff] [blame] | 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, |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 50 | }; |
| 51 | |
Jan Engelhardt | b18ffe3 | 2011-02-27 17:52:23 +0100 | [diff] [blame] | 52 | static void cluster_parse(struct xt_option_call *cb) |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 53 | { |
Jan Engelhardt | b18ffe3 | 2011-02-27 17:52:23 +0100 | [diff] [blame] | 54 | struct xt_cluster_match_info *info = cb->data; |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 55 | |
Jan Engelhardt | b18ffe3 | 2011-02-27 17:52:23 +0100 | [diff] [blame] | 56 | xtables_option_parse(cb); |
| 57 | switch (cb->entry->id) { |
| 58 | case O_CL_LOCAL_NODE: |
| 59 | if (cb->invert) |
Pablo Neira Ayuso | e76ec99 | 2011-02-06 21:34:33 +0100 | [diff] [blame] | 60 | info->flags |= XT_CLUSTER_F_INV; |
Jan Engelhardt | b18ffe3 | 2011-02-27 17:52:23 +0100 | [diff] [blame] | 61 | info->node_mask = 1 << (cb->val.u32 - 1); |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 62 | break; |
Jan Engelhardt | b18ffe3 | 2011-02-27 17:52:23 +0100 | [diff] [blame] | 63 | case O_CL_LOCAL_NODEMASK: |
| 64 | if (cb->invert) |
Pablo Neira Ayuso | e76ec99 | 2011-02-06 21:34:33 +0100 | [diff] [blame] | 65 | info->flags |= XT_CLUSTER_F_INV; |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 66 | break; |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 67 | } |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Jan Engelhardt | b18ffe3 | 2011-02-27 17:52:23 +0100 | [diff] [blame] | 70 | static void cluster_check(struct xt_fcheck_call *cb) |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 71 | { |
Jan Engelhardt | b18ffe3 | 2011-02-27 17:52:23 +0100 | [diff] [blame] | 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)) |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 78 | xtables_error(PARAMETER_PROBLEM, |
| 79 | "cluster match: " |
| 80 | "`--cluster-local-node' " |
| 81 | "must be <= `--cluster-total-nodes'"); |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 82 | return; |
| 83 | } |
Jan Engelhardt | b18ffe3 | 2011-02-27 17:52:23 +0100 | [diff] [blame] | 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)) |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 88 | xtables_error(PARAMETER_PROBLEM, |
| 89 | "cluster match: " |
| 90 | "`--cluster-local-nodemask' too big " |
| 91 | "for `--cluster-total-nodes'"); |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 92 | return; |
| 93 | } |
Jan Engelhardt | b18ffe3 | 2011-02-27 17:52:23 +0100 | [diff] [blame] | 94 | if (!(cb->xflags & (F_CL_LOCAL_NODE | F_CL_LOCAL_NODEMASK))) |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 95 | xtables_error(PARAMETER_PROBLEM, |
| 96 | "cluster match: `--cluster-local-node' or" |
| 97 | "`--cluster-local-nodemask' is missing"); |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static void |
| 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 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 105 | printf(" cluster "); |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 106 | if (info->flags & XT_CLUSTER_F_INV) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 107 | printf("!node_mask=0x%08x", info->node_mask); |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 108 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 109 | printf("node_mask=0x%08x", info->node_mask); |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 110 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 111 | printf(" total_nodes=%u hash_seed=0x%08x", |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 112 | info->total_nodes, info->hash_seed); |
| 113 | } |
| 114 | |
| 115 | static void |
| 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) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 121 | printf(" ! --cluster-local-nodemask 0x%08x", info->node_mask); |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 122 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 123 | printf(" --cluster-local-nodemask 0x%08x", info->node_mask); |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 124 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 125 | printf(" --cluster-total-nodes %u --cluster-hash-seed 0x%08x", |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 126 | info->total_nodes, info->hash_seed); |
| 127 | } |
| 128 | |
| 129 | static struct xtables_match cluster_mt_reg = { |
Jan Engelhardt | 4297936 | 2009-06-01 11:56:23 +0200 | [diff] [blame] | 130 | .family = NFPROTO_UNSPEC, |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 131 | .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 Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 136 | .print = cluster_print, |
| 137 | .save = cluster_save, |
Jan Engelhardt | b18ffe3 | 2011-02-27 17:52:23 +0100 | [diff] [blame] | 138 | .x6_parse = cluster_parse, |
| 139 | .x6_fcheck = cluster_check, |
| 140 | .x6_options = cluster_opts, |
Pablo Neira Ayuso | cd958a6 | 2009-05-06 13:01:20 +0200 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | void _init(void) |
| 144 | { |
| 145 | xtables_register_match(&cluster_mt_reg); |
| 146 | } |