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"
40 static struct option opts[] = {
42 { "hashmode", 1, 0, '2' },
43 { "clustermac", 1, 0, '3' },
44 { "total-nodes", 1, 0, '4' },
45 { "local-node", 1, 0, '5' },
50 init(struct ipt_entry_target *t, unsigned int *nfcache)
55 parse_mac(const char *mac, char *macbuf)
59 if (strlen(mac) != ETH_ALEN*3-1)
60 exit_error(PARAMETER_PROBLEM, "Bad mac address `%s'", mac);
62 for (i = 0; i < ETH_ALEN; i++) {
66 number = strtol(mac + i*3, &end, 16);
68 if (end == mac + i*3 + 2
73 exit_error(PARAMETER_PROBLEM,
74 "Bad mac address `%s'", mac);
78 #define PARAM_NEW 0x0001
79 #define PARAM_HMODE 0x0002
80 #define PARAM_MAC 0x0004
81 #define PARAM_TOTALNODE 0x0008
82 #define PARAM_LOCALNODE 0x0010
85 parse(int c, char **argv, int invert, unsigned int *flags,
86 const struct ipt_entry *entry,
87 struct ipt_entry_target **target)
89 struct ipt_clusterip_tgt_info *cipinfo
90 = (struct ipt_clusterip_tgt_info *)(*target)->data;
95 cipinfo->flags |= CLUSTERIP_FLAG_NEW;
96 if (*flags & PARAM_NEW)
97 exit_error(PARAMETER_PROBLEM, "Can only specify `--new' once\n");
101 if (!(*flags & PARAM_NEW))
102 exit_error(PARAMETER_PROBLEM, "Can only specify hashmode combined with `--new'\n");
103 if (*flags & PARAM_HMODE)
104 exit_error(PARAMETER_PROBLEM, "Can only specify hashmode once\n");
105 if (!strcmp(optarg, "sourceip"))
106 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP;
107 else if (!strcmp(optarg, "sourceip-sourceport"))
108 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT;
109 else if (!strcmp(optarg, "sourceip-sourceport-destport"))
110 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT_DPT;
112 exit_error(PARAMETER_PROBLEM, "Unknown hashmode `%s'\n",
114 *flags |= PARAM_HMODE;
117 if (!(*flags & PARAM_NEW))
118 exit_error(PARAMETER_PROBLEM, "Can only specify MAC combined with `--new'\n");
119 if (*flags & PARAM_MAC)
120 exit_error(PARAMETER_PROBLEM, "Can only specify MAC once\n");
121 parse_mac(optarg, cipinfo->clustermac);
122 if (!(cipinfo->clustermac[0] & 0x01))
123 exit_error(PARAMETER_PROBLEM, "MAC has to be a multicast ethernet address\n");
127 if (!(*flags & PARAM_NEW))
128 exit_error(PARAMETER_PROBLEM, "Can only specify node number combined with `--new'\n");
129 if (*flags & PARAM_TOTALNODE)
130 exit_error(PARAMETER_PROBLEM, "Can only specify total node number once\n");
131 if (string_to_number(optarg, 1, CLUSTERIP_MAX_NODES, &num) < 0)
132 exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg);
133 cipinfo->num_total_nodes = (u_int16_t)num;
134 *flags |= PARAM_TOTALNODE;
137 if (!(*flags & PARAM_NEW))
138 exit_error(PARAMETER_PROBLEM, "Can only specify node number combined with `--new'\n");
139 if (*flags & PARAM_LOCALNODE)
140 exit_error(PARAMETER_PROBLEM, "Can only specify local node number once\n");
141 if (string_to_number(optarg, 1, CLUSTERIP_MAX_NODES, &num) < 0)
142 exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg);
143 cipinfo->num_local_nodes = 1;
144 cipinfo->local_nodes[0] = (u_int16_t)num;
145 *flags |= PARAM_LOCALNODE;
155 final_check(unsigned int flags)
160 if (flags == (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE))
163 exit_error(PARAMETER_PROBLEM, "CLUSTERIP target: Invalid parameter combination\n");
166 static char *hashmode2str(enum clusterip_hashmode mode)
170 case CLUSTERIP_HASHMODE_SIP:
173 case CLUSTERIP_HASHMODE_SIP_SPT:
174 retstr = "sourceip-sourceport";
176 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
177 retstr = "sourceip-sourceport-destport";
180 retstr = "unknown-error";
186 static char *mac2str(const u_int8_t mac[ETH_ALEN])
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]);
195 /* Prints out the targinfo. */
197 print(const struct ipt_ip *ip,
198 const struct ipt_entry_target *target,
201 const struct ipt_clusterip_tgt_info *cipinfo =
202 (const struct ipt_clusterip_tgt_info *)target->data;
204 if (!cipinfo->flags & CLUSTERIP_FLAG_NEW) {
209 printf("CLUSTERIP hashmode=%s clustermac=%s total_nodes=%u local_node=%u ",
210 hashmode2str(cipinfo->hash_mode),
211 mac2str(cipinfo->clustermac),
212 cipinfo->num_total_nodes,
213 cipinfo->local_nodes[0]);
216 /* Saves the union ipt_targinfo in parsable form to stdout. */
218 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
221 const struct ipt_connmark_target_info *markinfo =
222 (const struct ipt_connmark_target_info *)target->data;
224 switch (markinfo->mode) {
225 case IPT_CONNMARK_SET:
226 printf("--set-mark 0x%lx ", markinfo->mark);
228 case IPT_CONNMARK_SAVE:
229 printf("--save-mark ");
231 case IPT_CONNMARK_RESTORE:
232 printf("--restore-mark ");
235 printf("ERROR: UNKNOWN CONNMARK MODE ");
241 static struct iptables_target clusterip = {
244 .version = IPTABLES_VERSION,
245 .size = IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)),
246 .userspacesize = offsetof(struct ipt_clusterip_tgt_info, config),
250 .final_check = &final_check,
258 register_target(&clusterip);