blob: 74b434f949ac82c77f722e76746005503846011d [file] [log] [blame]
Harald Weltec340f6c2003-11-11 18:41:36 +00001/* 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 */
6#include <stdio.h>
7#include <string.h>
8#include <stdlib.h>
9#include <getopt.h>
Pablo Neira800938f2005-03-07 14:02:02 +000010#include <stddef.h>
Harald Weltec340f6c2003-11-11 18:41:36 +000011
12#if defined(__GLIBC__) && __GLIBC__ == 2
13#include <net/ethernet.h>
14#else
15#include <linux/if_ether.h>
16#endif
17
18#include <iptables.h>
19#include <linux/netfilter_ipv4/ip_tables.h>
Pablo Neira800938f2005-03-07 14:02:02 +000020#include "../include/linux/netfilter_ipv4/ipt_CLUSTERIP.h"
Harald Weltec340f6c2003-11-11 18:41:36 +000021
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000022static void CLUSTERIP_help(void)
Harald Weltec340f6c2003-11-11 18:41:36 +000023{
24 printf(
25"CLUSTERIP target v%s options:\n"
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"
KOVACS Krisztian3643aca2005-09-19 14:50:06 +000034" --hash-init <num> Set init value of the Jenkins hash\n"
Harald Weltec340f6c2003-11-11 18:41:36 +000035"\n",
36IPTABLES_VERSION);
37}
38
KOVACS Krisztian3643aca2005-09-19 14:50:06 +000039#define PARAM_NEW 0x0001
40#define PARAM_HMODE 0x0002
41#define PARAM_MAC 0x0004
42#define PARAM_TOTALNODE 0x0008
43#define PARAM_LOCALNODE 0x0010
44#define PARAM_HASHINIT 0x0020
45
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000046static const struct option CLUSTERIP_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000047 { "new", 0, NULL, '1' },
48 { "hashmode", 1, NULL, '2' },
49 { "clustermac", 1, NULL, '3' },
50 { "total-nodes", 1, NULL, '4' },
51 { "local-node", 1, NULL, '5' },
52 { "hash-init", 1, NULL, '6' },
53 { }
Harald Weltec340f6c2003-11-11 18:41:36 +000054};
55
56static void
Harald Weltec340f6c2003-11-11 18:41:36 +000057parse_mac(const char *mac, char *macbuf)
58{
59 unsigned int i = 0;
60
61 if (strlen(mac) != ETH_ALEN*3-1)
62 exit_error(PARAMETER_PROBLEM, "Bad mac address `%s'", mac);
63
64 for (i = 0; i < ETH_ALEN; i++) {
65 long number;
66 char *end;
67
68 number = strtol(mac + i*3, &end, 16);
69
70 if (end == mac + i*3 + 2
71 && number >= 0
72 && number <= 255)
73 macbuf[i] = number;
74 else
75 exit_error(PARAMETER_PROBLEM,
76 "Bad mac address `%s'", mac);
77 }
78}
79
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000080static int CLUSTERIP_parse(int c, char **argv, int invert, unsigned int *flags,
81 const void *entry, struct xt_entry_target **target)
Harald Weltec340f6c2003-11-11 18:41:36 +000082{
83 struct ipt_clusterip_tgt_info *cipinfo
84 = (struct ipt_clusterip_tgt_info *)(*target)->data;
85
86 switch (c) {
87 unsigned int num;
88 case '1':
89 cipinfo->flags |= CLUSTERIP_FLAG_NEW;
90 if (*flags & PARAM_NEW)
91 exit_error(PARAMETER_PROBLEM, "Can only specify `--new' once\n");
92 *flags |= PARAM_NEW;
93 break;
94 case '2':
95 if (!(*flags & PARAM_NEW))
96 exit_error(PARAMETER_PROBLEM, "Can only specify hashmode combined with `--new'\n");
97 if (*flags & PARAM_HMODE)
98 exit_error(PARAMETER_PROBLEM, "Can only specify hashmode once\n");
99 if (!strcmp(optarg, "sourceip"))
100 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP;
101 else if (!strcmp(optarg, "sourceip-sourceport"))
102 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT;
103 else if (!strcmp(optarg, "sourceip-sourceport-destport"))
104 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT_DPT;
105 else
106 exit_error(PARAMETER_PROBLEM, "Unknown hashmode `%s'\n",
107 optarg);
108 *flags |= PARAM_HMODE;
109 break;
110 case '3':
111 if (!(*flags & PARAM_NEW))
112 exit_error(PARAMETER_PROBLEM, "Can only specify MAC combined with `--new'\n");
113 if (*flags & PARAM_MAC)
114 exit_error(PARAMETER_PROBLEM, "Can only specify MAC once\n");
Patrick McHardy2739cb82005-11-18 18:00:25 +0000115 parse_mac(optarg, (char *)cipinfo->clustermac);
Harald Weltedb986e82003-11-26 12:50:38 +0000116 if (!(cipinfo->clustermac[0] & 0x01))
117 exit_error(PARAMETER_PROBLEM, "MAC has to be a multicast ethernet address\n");
Harald Weltec340f6c2003-11-11 18:41:36 +0000118 *flags |= PARAM_MAC;
119 break;
120 case '4':
121 if (!(*flags & PARAM_NEW))
122 exit_error(PARAMETER_PROBLEM, "Can only specify node number combined with `--new'\n");
123 if (*flags & PARAM_TOTALNODE)
124 exit_error(PARAMETER_PROBLEM, "Can only specify total node number once\n");
125 if (string_to_number(optarg, 1, CLUSTERIP_MAX_NODES, &num) < 0)
126 exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg);
127 cipinfo->num_total_nodes = (u_int16_t)num;
128 *flags |= PARAM_TOTALNODE;
129 break;
130 case '5':
131 if (!(*flags & PARAM_NEW))
132 exit_error(PARAMETER_PROBLEM, "Can only specify node number combined with `--new'\n");
133 if (*flags & PARAM_LOCALNODE)
134 exit_error(PARAMETER_PROBLEM, "Can only specify local node number once\n");
135 if (string_to_number(optarg, 1, CLUSTERIP_MAX_NODES, &num) < 0)
136 exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg);
137 cipinfo->num_local_nodes = 1;
138 cipinfo->local_nodes[0] = (u_int16_t)num;
139 *flags |= PARAM_LOCALNODE;
140 break;
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000141 case '6':
142 if (!(*flags & PARAM_NEW))
143 exit_error(PARAMETER_PROBLEM, "Can only specify hash init value combined with `--new'\n");
144 if (*flags & PARAM_HASHINIT)
145 exit_error(PARAMETER_PROBLEM, "Can specify hash init value only once\n");
146 if (string_to_number(optarg, 0, UINT_MAX, &num) < 0)
147 exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg);
148 cipinfo->hash_initval = num;
149 *flags |= PARAM_HASHINIT;
150 break;
Harald Weltec340f6c2003-11-11 18:41:36 +0000151 default:
152 return 0;
153 }
154
155 return 1;
156}
157
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000158static void CLUSTERIP_check(unsigned int flags)
Harald Weltec340f6c2003-11-11 18:41:36 +0000159{
160 if (flags == 0)
161 return;
162
Patrick McHardy2739cb82005-11-18 18:00:25 +0000163 if ((flags & (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE))
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000164 == (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE))
Harald Weltec340f6c2003-11-11 18:41:36 +0000165 return;
166
167 exit_error(PARAMETER_PROBLEM, "CLUSTERIP target: Invalid parameter combination\n");
168}
169
170static char *hashmode2str(enum clusterip_hashmode mode)
171{
172 char *retstr;
173 switch (mode) {
174 case CLUSTERIP_HASHMODE_SIP:
175 retstr = "sourceip";
176 break;
177 case CLUSTERIP_HASHMODE_SIP_SPT:
178 retstr = "sourceip-sourceport";
179 break;
180 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
181 retstr = "sourceip-sourceport-destport";
182 break;
183 default:
184 retstr = "unknown-error";
185 break;
186 }
187 return retstr;
188}
189
Harald Welte37963e02005-02-01 15:53:07 +0000190static char *mac2str(const u_int8_t mac[ETH_ALEN])
Harald Weltec340f6c2003-11-11 18:41:36 +0000191{
192 static char buf[ETH_ALEN*3];
193 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
194 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
195 return buf;
196}
197
198
199/* Prints out the targinfo. */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000200static void CLUSTERIP_print(const void *ip,
201 const struct xt_entry_target *target, int numeric)
Harald Weltec340f6c2003-11-11 18:41:36 +0000202{
203 const struct ipt_clusterip_tgt_info *cipinfo =
204 (const struct ipt_clusterip_tgt_info *)target->data;
205
206 if (!cipinfo->flags & CLUSTERIP_FLAG_NEW) {
207 printf("CLUSTERIP");
208 return;
209 }
210
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000211 printf("CLUSTERIP hashmode=%s clustermac=%s total_nodes=%u local_node=%u hash_init=%u",
Harald Weltec340f6c2003-11-11 18:41:36 +0000212 hashmode2str(cipinfo->hash_mode),
213 mac2str(cipinfo->clustermac),
214 cipinfo->num_total_nodes,
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000215 cipinfo->local_nodes[0],
216 cipinfo->hash_initval);
Harald Weltec340f6c2003-11-11 18:41:36 +0000217}
218
219/* Saves the union ipt_targinfo in parsable form to stdout. */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000220static void CLUSTERIP_save(const void *ip, const struct xt_entry_target *target)
Harald Weltec340f6c2003-11-11 18:41:36 +0000221{
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000222 const struct ipt_clusterip_tgt_info *cipinfo =
223 (const struct ipt_clusterip_tgt_info *)target->data;
Harald Weltec340f6c2003-11-11 18:41:36 +0000224
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000225 /* if this is not a new entry, we don't need to save target
226 * parameters */
227 if (!cipinfo->flags & CLUSTERIP_FLAG_NEW)
228 return;
229
230 printf("--new --hashmode %s --clustermac %s --total-nodes %d --local-node %d --hash-init %u",
231 hashmode2str(cipinfo->hash_mode),
232 mac2str(cipinfo->clustermac),
233 cipinfo->num_total_nodes,
234 cipinfo->local_nodes[0],
235 cipinfo->hash_initval);
Harald Weltec340f6c2003-11-11 18:41:36 +0000236}
237
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000238static struct iptables_target clusterip_target = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000239 .name = "CLUSTERIP",
240 .version = IPTABLES_VERSION,
241 .size = IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)),
Pablo Neira800938f2005-03-07 14:02:02 +0000242 .userspacesize = offsetof(struct ipt_clusterip_tgt_info, config),
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000243 .help = CLUSTERIP_help,
244 .parse = CLUSTERIP_parse,
245 .final_check = CLUSTERIP_check,
246 .print = CLUSTERIP_print,
247 .save = CLUSTERIP_save,
248 .extra_opts = CLUSTERIP_opts,
Harald Weltec340f6c2003-11-11 18:41:36 +0000249};
250
251void _init(void)
252{
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000253 register_target(&clusterip_target);
Harald Weltec340f6c2003-11-11 18:41:36 +0000254}