Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add CLUSTERIP target support. |
| 2 | * (C) 2003 by Harald Welte <laforge@gnumonks.org> |
| 3 | * |
| 4 | * Development of this code was funded by SuSE AG, http://www.suse.com/ |
| 5 | */ |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 6 | #include <stdbool.h> |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <getopt.h> |
Pablo Neira | 800938f | 2005-03-07 14:02:02 +0000 | [diff] [blame] | 11 | #include <stddef.h> |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 12 | |
| 13 | #if defined(__GLIBC__) && __GLIBC__ == 2 |
| 14 | #include <net/ethernet.h> |
| 15 | #else |
| 16 | #include <linux/if_ether.h> |
| 17 | #endif |
| 18 | |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 19 | #include <xtables.h> |
Jan Engelhardt | a2a7f2b | 2008-09-01 14:20:13 +0200 | [diff] [blame] | 20 | #include <linux/netfilter_ipv4/ipt_CLUSTERIP.h> |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 21 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 22 | static void CLUSTERIP_help(void) |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 23 | { |
| 24 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 25 | "CLUSTERIP target options:\n" |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 26 | " --new Create a new ClusterIP\n" |
| 27 | " --hashmode <mode> Specify hashing mode\n" |
| 28 | " sourceip\n" |
| 29 | " sourceip-sourceport\n" |
| 30 | " sourceip-sourceport-destport\n" |
| 31 | " --clustermac <mac> Set clusterIP MAC address\n" |
| 32 | " --total-nodes <num> Set number of total nodes in cluster\n" |
| 33 | " --local-node <num> Set the local node number\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 34 | " --hash-init <num> Set init value of the Jenkins hash\n"); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 35 | } |
| 36 | |
KOVACS Krisztian | 3643aca | 2005-09-19 14:50:06 +0000 | [diff] [blame] | 37 | #define PARAM_NEW 0x0001 |
| 38 | #define PARAM_HMODE 0x0002 |
| 39 | #define PARAM_MAC 0x0004 |
| 40 | #define PARAM_TOTALNODE 0x0008 |
| 41 | #define PARAM_LOCALNODE 0x0010 |
| 42 | #define PARAM_HASHINIT 0x0020 |
| 43 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 44 | static const struct option CLUSTERIP_opts[] = { |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 45 | {.name = "new", .has_arg = false, .val = '1'}, |
| 46 | {.name = "hashmode", .has_arg = true, .val = '2'}, |
| 47 | {.name = "clustermac", .has_arg = true, .val = '3'}, |
| 48 | {.name = "total-nodes", .has_arg = true, .val = '4'}, |
| 49 | {.name = "local-node", .has_arg = true, .val = '5'}, |
| 50 | {.name = "hash-init", .has_arg = true, .val = '6'}, |
| 51 | XT_GETOPT_TABLEEND, |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | static void |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 55 | parse_mac(const char *mac, char *macbuf) |
| 56 | { |
| 57 | unsigned int i = 0; |
| 58 | |
| 59 | if (strlen(mac) != ETH_ALEN*3-1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 60 | xtables_error(PARAMETER_PROBLEM, "Bad mac address \"%s\"", mac); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 61 | |
| 62 | for (i = 0; i < ETH_ALEN; i++) { |
| 63 | long number; |
| 64 | char *end; |
| 65 | |
| 66 | number = strtol(mac + i*3, &end, 16); |
| 67 | |
| 68 | if (end == mac + i*3 + 2 |
| 69 | && number >= 0 |
| 70 | && number <= 255) |
| 71 | macbuf[i] = number; |
| 72 | else |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 73 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 74 | "Bad mac address `%s'", mac); |
| 75 | } |
| 76 | } |
| 77 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 78 | static int CLUSTERIP_parse(int c, char **argv, int invert, unsigned int *flags, |
| 79 | const void *entry, struct xt_entry_target **target) |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 80 | { |
| 81 | struct ipt_clusterip_tgt_info *cipinfo |
| 82 | = (struct ipt_clusterip_tgt_info *)(*target)->data; |
| 83 | |
| 84 | switch (c) { |
| 85 | unsigned int num; |
| 86 | case '1': |
| 87 | cipinfo->flags |= CLUSTERIP_FLAG_NEW; |
| 88 | if (*flags & PARAM_NEW) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 89 | xtables_error(PARAMETER_PROBLEM, "Can only specify \"--new\" once\n"); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 90 | *flags |= PARAM_NEW; |
| 91 | break; |
| 92 | case '2': |
| 93 | if (!(*flags & PARAM_NEW)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 94 | xtables_error(PARAMETER_PROBLEM, "Can only specify hashmode combined with \"--new\"\n"); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 95 | if (*flags & PARAM_HMODE) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 96 | xtables_error(PARAMETER_PROBLEM, "Can only specify hashmode once\n"); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 97 | if (!strcmp(optarg, "sourceip")) |
| 98 | cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP; |
| 99 | else if (!strcmp(optarg, "sourceip-sourceport")) |
| 100 | cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT; |
| 101 | else if (!strcmp(optarg, "sourceip-sourceport-destport")) |
| 102 | cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT_DPT; |
| 103 | else |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 104 | xtables_error(PARAMETER_PROBLEM, "Unknown hashmode \"%s\"\n", |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 105 | optarg); |
| 106 | *flags |= PARAM_HMODE; |
| 107 | break; |
| 108 | case '3': |
| 109 | if (!(*flags & PARAM_NEW)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 110 | xtables_error(PARAMETER_PROBLEM, "Can only specify MAC combined with \"--new\"\n"); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 111 | if (*flags & PARAM_MAC) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 112 | xtables_error(PARAMETER_PROBLEM, "Can only specify MAC once\n"); |
Patrick McHardy | 2739cb8 | 2005-11-18 18:00:25 +0000 | [diff] [blame] | 113 | parse_mac(optarg, (char *)cipinfo->clustermac); |
Harald Welte | db986e8 | 2003-11-26 12:50:38 +0000 | [diff] [blame] | 114 | if (!(cipinfo->clustermac[0] & 0x01)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 115 | xtables_error(PARAMETER_PROBLEM, "MAC has to be a multicast ethernet address\n"); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 116 | *flags |= PARAM_MAC; |
| 117 | break; |
| 118 | case '4': |
| 119 | if (!(*flags & PARAM_NEW)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 120 | xtables_error(PARAMETER_PROBLEM, "Can only specify node number combined with \"--new\"\n"); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 121 | if (*flags & PARAM_TOTALNODE) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 122 | xtables_error(PARAMETER_PROBLEM, "Can only specify total node number once\n"); |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 123 | if (!xtables_strtoui(optarg, NULL, &num, 1, CLUSTERIP_MAX_NODES)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 124 | xtables_error(PARAMETER_PROBLEM, "Unable to parse \"%s\"\n", optarg); |
Jan Engelhardt | 213e185 | 2009-01-27 17:24:34 +0100 | [diff] [blame] | 125 | cipinfo->num_total_nodes = num; |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 126 | *flags |= PARAM_TOTALNODE; |
| 127 | break; |
| 128 | case '5': |
| 129 | if (!(*flags & PARAM_NEW)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 130 | xtables_error(PARAMETER_PROBLEM, "Can only specify node number combined with \"--new\"\n"); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 131 | if (*flags & PARAM_LOCALNODE) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 132 | xtables_error(PARAMETER_PROBLEM, "Can only specify local node number once\n"); |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 133 | if (!xtables_strtoui(optarg, NULL, &num, 1, CLUSTERIP_MAX_NODES)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 134 | xtables_error(PARAMETER_PROBLEM, "Unable to parse \"%s\"\n", optarg); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 135 | cipinfo->num_local_nodes = 1; |
Jan Engelhardt | 213e185 | 2009-01-27 17:24:34 +0100 | [diff] [blame] | 136 | cipinfo->local_nodes[0] = num; |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 137 | *flags |= PARAM_LOCALNODE; |
| 138 | break; |
KOVACS Krisztian | 3643aca | 2005-09-19 14:50:06 +0000 | [diff] [blame] | 139 | case '6': |
| 140 | if (!(*flags & PARAM_NEW)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 141 | xtables_error(PARAMETER_PROBLEM, "Can only specify hash init value combined with \"--new\"\n"); |
KOVACS Krisztian | 3643aca | 2005-09-19 14:50:06 +0000 | [diff] [blame] | 142 | if (*flags & PARAM_HASHINIT) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 143 | xtables_error(PARAMETER_PROBLEM, "Can specify hash init value only once\n"); |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 144 | if (!xtables_strtoui(optarg, NULL, &num, 0, UINT_MAX)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 145 | xtables_error(PARAMETER_PROBLEM, "Unable to parse \"%s\"\n", optarg); |
KOVACS Krisztian | 3643aca | 2005-09-19 14:50:06 +0000 | [diff] [blame] | 146 | cipinfo->hash_initval = num; |
| 147 | *flags |= PARAM_HASHINIT; |
| 148 | break; |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | return 1; |
| 152 | } |
| 153 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 154 | static void CLUSTERIP_check(unsigned int flags) |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 155 | { |
| 156 | if (flags == 0) |
| 157 | return; |
| 158 | |
Patrick McHardy | 2739cb8 | 2005-11-18 18:00:25 +0000 | [diff] [blame] | 159 | if ((flags & (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE)) |
KOVACS Krisztian | 3643aca | 2005-09-19 14:50:06 +0000 | [diff] [blame] | 160 | == (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE)) |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 161 | return; |
| 162 | |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 163 | xtables_error(PARAMETER_PROBLEM, "CLUSTERIP target: Invalid parameter combination\n"); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Jan Engelhardt | e814c8b | 2011-01-08 03:16:51 +0100 | [diff] [blame^] | 166 | static const char *hashmode2str(enum clusterip_hashmode mode) |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 167 | { |
Jan Engelhardt | e814c8b | 2011-01-08 03:16:51 +0100 | [diff] [blame^] | 168 | const char *retstr; |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 169 | switch (mode) { |
| 170 | case CLUSTERIP_HASHMODE_SIP: |
| 171 | retstr = "sourceip"; |
| 172 | break; |
| 173 | case CLUSTERIP_HASHMODE_SIP_SPT: |
| 174 | retstr = "sourceip-sourceport"; |
| 175 | break; |
| 176 | case CLUSTERIP_HASHMODE_SIP_SPT_DPT: |
| 177 | retstr = "sourceip-sourceport-destport"; |
| 178 | break; |
| 179 | default: |
| 180 | retstr = "unknown-error"; |
| 181 | break; |
| 182 | } |
| 183 | return retstr; |
| 184 | } |
| 185 | |
Jan Engelhardt | e814c8b | 2011-01-08 03:16:51 +0100 | [diff] [blame^] | 186 | static const char *mac2str(const uint8_t mac[ETH_ALEN]) |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 187 | { |
| 188 | static char buf[ETH_ALEN*3]; |
| 189 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 190 | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
| 191 | return buf; |
| 192 | } |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 193 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 194 | static void CLUSTERIP_print(const void *ip, |
| 195 | const struct xt_entry_target *target, int numeric) |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 196 | { |
| 197 | const struct ipt_clusterip_tgt_info *cipinfo = |
| 198 | (const struct ipt_clusterip_tgt_info *)target->data; |
| 199 | |
| 200 | if (!cipinfo->flags & CLUSTERIP_FLAG_NEW) { |
| 201 | printf("CLUSTERIP"); |
| 202 | return; |
| 203 | } |
| 204 | |
KOVACS Krisztian | 3643aca | 2005-09-19 14:50:06 +0000 | [diff] [blame] | 205 | printf("CLUSTERIP hashmode=%s clustermac=%s total_nodes=%u local_node=%u hash_init=%u", |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 206 | hashmode2str(cipinfo->hash_mode), |
| 207 | mac2str(cipinfo->clustermac), |
| 208 | cipinfo->num_total_nodes, |
KOVACS Krisztian | 3643aca | 2005-09-19 14:50:06 +0000 | [diff] [blame] | 209 | cipinfo->local_nodes[0], |
| 210 | cipinfo->hash_initval); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 213 | static void CLUSTERIP_save(const void *ip, const struct xt_entry_target *target) |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 214 | { |
KOVACS Krisztian | 3643aca | 2005-09-19 14:50:06 +0000 | [diff] [blame] | 215 | const struct ipt_clusterip_tgt_info *cipinfo = |
| 216 | (const struct ipt_clusterip_tgt_info *)target->data; |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 217 | |
KOVACS Krisztian | 3643aca | 2005-09-19 14:50:06 +0000 | [diff] [blame] | 218 | /* if this is not a new entry, we don't need to save target |
| 219 | * parameters */ |
| 220 | if (!cipinfo->flags & CLUSTERIP_FLAG_NEW) |
| 221 | return; |
| 222 | |
| 223 | printf("--new --hashmode %s --clustermac %s --total-nodes %d --local-node %d --hash-init %u", |
| 224 | hashmode2str(cipinfo->hash_mode), |
| 225 | mac2str(cipinfo->clustermac), |
| 226 | cipinfo->num_total_nodes, |
| 227 | cipinfo->local_nodes[0], |
| 228 | cipinfo->hash_initval); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 231 | static struct xtables_target clusterip_tg_reg = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 232 | .name = "CLUSTERIP", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 233 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 234 | .family = NFPROTO_IPV4, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 235 | .size = XT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)), |
Pablo Neira | 800938f | 2005-03-07 14:02:02 +0000 | [diff] [blame] | 236 | .userspacesize = offsetof(struct ipt_clusterip_tgt_info, config), |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 237 | .help = CLUSTERIP_help, |
| 238 | .parse = CLUSTERIP_parse, |
| 239 | .final_check = CLUSTERIP_check, |
| 240 | .print = CLUSTERIP_print, |
| 241 | .save = CLUSTERIP_save, |
| 242 | .extra_opts = CLUSTERIP_opts, |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | void _init(void) |
| 246 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 247 | xtables_register_target(&clusterip_tg_reg); |
Harald Welte | c340f6c | 2003-11-11 18:41:36 +0000 | [diff] [blame] | 248 | } |