1 /* Shared library add-on to iptables to add CLUSTERIP target support.
2 * (C) 2003 by Harald Welte <laforge@gnumonks.org>
4 * Development of this code was funded by SuSE AG, http://www.suse.com/
12 #if defined(__GLIBC__) && __GLIBC__ == 2
13 #include <net/ethernet.h>
15 #include <linux/if_ether.h>
19 #include <linux/netfilter_ipv4/ip_tables.h>
20 #include "../include/linux/netfilter_ipv4/ipt_CLUSTERIP.h"
26 "CLUSTERIP target v%s options:\n"
27 " --new Create a new ClusterIP\n"
28 " --hashmode <mode> Specify hashing mode\n"
30 " sourceip-sourceport\n"
31 " sourceip-sourceport-destport\n"
32 " --clustermac <mac> Set clusterIP MAC address\n"
33 " --total-nodes <num> Set number of total nodes in cluster\n"
34 " --local-node <num> Set the local node number\n"
35 " --hash-init <num> Set init value of the Jenkins hash\n"
40 #define PARAM_NEW 0x0001
41 #define PARAM_HMODE 0x0002
42 #define PARAM_MAC 0x0004
43 #define PARAM_TOTALNODE 0x0008
44 #define PARAM_LOCALNODE 0x0010
45 #define PARAM_HASHINIT 0x0020
47 static struct option opts[] = {
49 { "hashmode", 1, 0, '2' },
50 { "clustermac", 1, 0, '3' },
51 { "total-nodes", 1, 0, '4' },
52 { "local-node", 1, 0, '5' },
53 { "hash-init", 1, 0, '6' },
58 init(struct ipt_entry_target *t, unsigned int *nfcache)
63 parse_mac(const char *mac, char *macbuf)
67 if (strlen(mac) != ETH_ALEN*3-1)
68 exit_error(PARAMETER_PROBLEM, "Bad mac address `%s'", mac);
70 for (i = 0; i < ETH_ALEN; i++) {
74 number = strtol(mac + i*3, &end, 16);
76 if (end == mac + i*3 + 2
81 exit_error(PARAMETER_PROBLEM,
82 "Bad mac address `%s'", mac);
87 parse(int c, char **argv, int invert, unsigned int *flags,
88 const struct ipt_entry *entry,
89 struct ipt_entry_target **target)
91 struct ipt_clusterip_tgt_info *cipinfo
92 = (struct ipt_clusterip_tgt_info *)(*target)->data;
97 cipinfo->flags |= CLUSTERIP_FLAG_NEW;
98 if (*flags & PARAM_NEW)
99 exit_error(PARAMETER_PROBLEM, "Can only specify `--new' once\n");
103 if (!(*flags & PARAM_NEW))
104 exit_error(PARAMETER_PROBLEM, "Can only specify hashmode combined with `--new'\n");
105 if (*flags & PARAM_HMODE)
106 exit_error(PARAMETER_PROBLEM, "Can only specify hashmode once\n");
107 if (!strcmp(optarg, "sourceip"))
108 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP;
109 else if (!strcmp(optarg, "sourceip-sourceport"))
110 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT;
111 else if (!strcmp(optarg, "sourceip-sourceport-destport"))
112 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT_DPT;
114 exit_error(PARAMETER_PROBLEM, "Unknown hashmode `%s'\n",
116 *flags |= PARAM_HMODE;
119 if (!(*flags & PARAM_NEW))
120 exit_error(PARAMETER_PROBLEM, "Can only specify MAC combined with `--new'\n");
121 if (*flags & PARAM_MAC)
122 exit_error(PARAMETER_PROBLEM, "Can only specify MAC once\n");
123 parse_mac(optarg, (char *)cipinfo->clustermac);
124 if (!(cipinfo->clustermac[0] & 0x01))
125 exit_error(PARAMETER_PROBLEM, "MAC has to be a multicast ethernet address\n");
129 if (!(*flags & PARAM_NEW))
130 exit_error(PARAMETER_PROBLEM, "Can only specify node number combined with `--new'\n");
131 if (*flags & PARAM_TOTALNODE)
132 exit_error(PARAMETER_PROBLEM, "Can only specify total node number once\n");
133 if (string_to_number(optarg, 1, CLUSTERIP_MAX_NODES, &num) < 0)
134 exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg);
135 cipinfo->num_total_nodes = (u_int16_t)num;
136 *flags |= PARAM_TOTALNODE;
139 if (!(*flags & PARAM_NEW))
140 exit_error(PARAMETER_PROBLEM, "Can only specify node number combined with `--new'\n");
141 if (*flags & PARAM_LOCALNODE)
142 exit_error(PARAMETER_PROBLEM, "Can only specify local node number once\n");
143 if (string_to_number(optarg, 1, CLUSTERIP_MAX_NODES, &num) < 0)
144 exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg);
145 cipinfo->num_local_nodes = 1;
146 cipinfo->local_nodes[0] = (u_int16_t)num;
147 *flags |= PARAM_LOCALNODE;
150 if (!(*flags & PARAM_NEW))
151 exit_error(PARAMETER_PROBLEM, "Can only specify hash init value combined with `--new'\n");
152 if (*flags & PARAM_HASHINIT)
153 exit_error(PARAMETER_PROBLEM, "Can specify hash init value only once\n");
154 if (string_to_number(optarg, 0, UINT_MAX, &num) < 0)
155 exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg);
156 cipinfo->hash_initval = num;
157 *flags |= PARAM_HASHINIT;
167 final_check(unsigned int flags)
172 if ((flags & (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE))
173 == (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE))
176 exit_error(PARAMETER_PROBLEM, "CLUSTERIP target: Invalid parameter combination\n");
179 static char *hashmode2str(enum clusterip_hashmode mode)
183 case CLUSTERIP_HASHMODE_SIP:
186 case CLUSTERIP_HASHMODE_SIP_SPT:
187 retstr = "sourceip-sourceport";
189 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
190 retstr = "sourceip-sourceport-destport";
193 retstr = "unknown-error";
199 static char *mac2str(const u_int8_t mac[ETH_ALEN])
201 static char buf[ETH_ALEN*3];
202 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
203 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
208 /* Prints out the targinfo. */
210 print(const struct ipt_ip *ip,
211 const struct ipt_entry_target *target,
214 const struct ipt_clusterip_tgt_info *cipinfo =
215 (const struct ipt_clusterip_tgt_info *)target->data;
217 if (!cipinfo->flags & CLUSTERIP_FLAG_NEW) {
222 printf("CLUSTERIP hashmode=%s clustermac=%s total_nodes=%u local_node=%u hash_init=%u",
223 hashmode2str(cipinfo->hash_mode),
224 mac2str(cipinfo->clustermac),
225 cipinfo->num_total_nodes,
226 cipinfo->local_nodes[0],
227 cipinfo->hash_initval);
230 /* Saves the union ipt_targinfo in parsable form to stdout. */
232 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
234 const struct ipt_clusterip_tgt_info *cipinfo =
235 (const struct ipt_clusterip_tgt_info *)target->data;
237 /* if this is not a new entry, we don't need to save target
239 if (!cipinfo->flags & CLUSTERIP_FLAG_NEW)
242 printf("--new --hashmode %s --clustermac %s --total-nodes %d --local-node %d --hash-init %u",
243 hashmode2str(cipinfo->hash_mode),
244 mac2str(cipinfo->clustermac),
245 cipinfo->num_total_nodes,
246 cipinfo->local_nodes[0],
247 cipinfo->hash_initval);
250 static struct iptables_target clusterip = {
253 .version = IPTABLES_VERSION,
254 .size = IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)),
255 .userspacesize = offsetof(struct ipt_clusterip_tgt_info, config),
259 .final_check = &final_check,
267 register_target(&clusterip);