blob: 2bb2292a56e5c9a8037287c74f292dd063bfe7e1 [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
Jan Engelhardt5d9678a2008-11-20 10:15:35 +010018#include <xtables.h>
Harald Weltec340f6c2003-11-11 18:41:36 +000019#include <linux/netfilter_ipv4/ip_tables.h>
Jan Engelhardta2a7f2b2008-09-01 14:20:13 +020020#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(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020025"CLUSTERIP target options:\n"
Harald Weltec340f6c2003-11-11 18:41:36 +000026" --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 Engelhardt8b7c64d2008-04-15 11:48:25 +020034" --hash-init <num> Set init value of the Jenkins hash\n");
Harald Weltec340f6c2003-11-11 18:41:36 +000035}
36
KOVACS Krisztian3643aca2005-09-19 14:50:06 +000037#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 Engelhardt1d5b63d2007-10-04 16:29:00 +000044static const struct option CLUSTERIP_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000045 { "new", 0, NULL, '1' },
46 { "hashmode", 1, NULL, '2' },
47 { "clustermac", 1, NULL, '3' },
48 { "total-nodes", 1, NULL, '4' },
49 { "local-node", 1, NULL, '5' },
50 { "hash-init", 1, NULL, '6' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000051 { .name = NULL }
Harald Weltec340f6c2003-11-11 18:41:36 +000052};
53
54static void
Harald Weltec340f6c2003-11-11 18:41:36 +000055parse_mac(const char *mac, char *macbuf)
56{
57 unsigned int i = 0;
58
59 if (strlen(mac) != ETH_ALEN*3-1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010060 xtables_error(PARAMETER_PROBLEM, "Bad mac address \"%s\"", mac);
Harald Weltec340f6c2003-11-11 18:41:36 +000061
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 Engelhardt1829ed42009-02-21 03:29:44 +010073 xtables_error(PARAMETER_PROBLEM,
Harald Weltec340f6c2003-11-11 18:41:36 +000074 "Bad mac address `%s'", mac);
75 }
76}
77
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000078static int CLUSTERIP_parse(int c, char **argv, int invert, unsigned int *flags,
79 const void *entry, struct xt_entry_target **target)
Harald Weltec340f6c2003-11-11 18:41:36 +000080{
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 Engelhardt1829ed42009-02-21 03:29:44 +010089 xtables_error(PARAMETER_PROBLEM, "Can only specify \"--new\" once\n");
Harald Weltec340f6c2003-11-11 18:41:36 +000090 *flags |= PARAM_NEW;
91 break;
92 case '2':
93 if (!(*flags & PARAM_NEW))
Jan Engelhardt1829ed42009-02-21 03:29:44 +010094 xtables_error(PARAMETER_PROBLEM, "Can only specify hashmode combined with \"--new\"\n");
Harald Weltec340f6c2003-11-11 18:41:36 +000095 if (*flags & PARAM_HMODE)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010096 xtables_error(PARAMETER_PROBLEM, "Can only specify hashmode once\n");
Harald Weltec340f6c2003-11-11 18:41:36 +000097 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 Engelhardt1829ed42009-02-21 03:29:44 +0100104 xtables_error(PARAMETER_PROBLEM, "Unknown hashmode \"%s\"\n",
Harald Weltec340f6c2003-11-11 18:41:36 +0000105 optarg);
106 *flags |= PARAM_HMODE;
107 break;
108 case '3':
109 if (!(*flags & PARAM_NEW))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100110 xtables_error(PARAMETER_PROBLEM, "Can only specify MAC combined with \"--new\"\n");
Harald Weltec340f6c2003-11-11 18:41:36 +0000111 if (*flags & PARAM_MAC)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100112 xtables_error(PARAMETER_PROBLEM, "Can only specify MAC once\n");
Patrick McHardy2739cb82005-11-18 18:00:25 +0000113 parse_mac(optarg, (char *)cipinfo->clustermac);
Harald Weltedb986e82003-11-26 12:50:38 +0000114 if (!(cipinfo->clustermac[0] & 0x01))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100115 xtables_error(PARAMETER_PROBLEM, "MAC has to be a multicast ethernet address\n");
Harald Weltec340f6c2003-11-11 18:41:36 +0000116 *flags |= PARAM_MAC;
117 break;
118 case '4':
119 if (!(*flags & PARAM_NEW))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100120 xtables_error(PARAMETER_PROBLEM, "Can only specify node number combined with \"--new\"\n");
Harald Weltec340f6c2003-11-11 18:41:36 +0000121 if (*flags & PARAM_TOTALNODE)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100122 xtables_error(PARAMETER_PROBLEM, "Can only specify total node number once\n");
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100123 if (!xtables_strtoui(optarg, NULL, &num, 1, CLUSTERIP_MAX_NODES))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100124 xtables_error(PARAMETER_PROBLEM, "Unable to parse \"%s\"\n", optarg);
Jan Engelhardt213e1852009-01-27 17:24:34 +0100125 cipinfo->num_total_nodes = num;
Harald Weltec340f6c2003-11-11 18:41:36 +0000126 *flags |= PARAM_TOTALNODE;
127 break;
128 case '5':
129 if (!(*flags & PARAM_NEW))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100130 xtables_error(PARAMETER_PROBLEM, "Can only specify node number combined with \"--new\"\n");
Harald Weltec340f6c2003-11-11 18:41:36 +0000131 if (*flags & PARAM_LOCALNODE)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100132 xtables_error(PARAMETER_PROBLEM, "Can only specify local node number once\n");
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100133 if (!xtables_strtoui(optarg, NULL, &num, 1, CLUSTERIP_MAX_NODES))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100134 xtables_error(PARAMETER_PROBLEM, "Unable to parse \"%s\"\n", optarg);
Harald Weltec340f6c2003-11-11 18:41:36 +0000135 cipinfo->num_local_nodes = 1;
Jan Engelhardt213e1852009-01-27 17:24:34 +0100136 cipinfo->local_nodes[0] = num;
Harald Weltec340f6c2003-11-11 18:41:36 +0000137 *flags |= PARAM_LOCALNODE;
138 break;
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000139 case '6':
140 if (!(*flags & PARAM_NEW))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100141 xtables_error(PARAMETER_PROBLEM, "Can only specify hash init value combined with \"--new\"\n");
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000142 if (*flags & PARAM_HASHINIT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100143 xtables_error(PARAMETER_PROBLEM, "Can specify hash init value only once\n");
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100144 if (!xtables_strtoui(optarg, NULL, &num, 0, UINT_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100145 xtables_error(PARAMETER_PROBLEM, "Unable to parse \"%s\"\n", optarg);
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000146 cipinfo->hash_initval = num;
147 *flags |= PARAM_HASHINIT;
148 break;
Harald Weltec340f6c2003-11-11 18:41:36 +0000149 default:
150 return 0;
151 }
152
153 return 1;
154}
155
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000156static void CLUSTERIP_check(unsigned int flags)
Harald Weltec340f6c2003-11-11 18:41:36 +0000157{
158 if (flags == 0)
159 return;
160
Patrick McHardy2739cb82005-11-18 18:00:25 +0000161 if ((flags & (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE))
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000162 == (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE))
Harald Weltec340f6c2003-11-11 18:41:36 +0000163 return;
164
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100165 xtables_error(PARAMETER_PROBLEM, "CLUSTERIP target: Invalid parameter combination\n");
Harald Weltec340f6c2003-11-11 18:41:36 +0000166}
167
168static char *hashmode2str(enum clusterip_hashmode mode)
169{
170 char *retstr;
171 switch (mode) {
172 case CLUSTERIP_HASHMODE_SIP:
173 retstr = "sourceip";
174 break;
175 case CLUSTERIP_HASHMODE_SIP_SPT:
176 retstr = "sourceip-sourceport";
177 break;
178 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
179 retstr = "sourceip-sourceport-destport";
180 break;
181 default:
182 retstr = "unknown-error";
183 break;
184 }
185 return retstr;
186}
187
Harald Welte37963e02005-02-01 15:53:07 +0000188static char *mac2str(const u_int8_t mac[ETH_ALEN])
Harald Weltec340f6c2003-11-11 18:41:36 +0000189{
190 static char buf[ETH_ALEN*3];
191 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
192 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
193 return buf;
194}
Harald Weltec340f6c2003-11-11 18:41:36 +0000195
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000196static void CLUSTERIP_print(const void *ip,
197 const struct xt_entry_target *target, int numeric)
Harald Weltec340f6c2003-11-11 18:41:36 +0000198{
199 const struct ipt_clusterip_tgt_info *cipinfo =
200 (const struct ipt_clusterip_tgt_info *)target->data;
201
202 if (!cipinfo->flags & CLUSTERIP_FLAG_NEW) {
203 printf("CLUSTERIP");
204 return;
205 }
206
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000207 printf("CLUSTERIP hashmode=%s clustermac=%s total_nodes=%u local_node=%u hash_init=%u",
Harald Weltec340f6c2003-11-11 18:41:36 +0000208 hashmode2str(cipinfo->hash_mode),
209 mac2str(cipinfo->clustermac),
210 cipinfo->num_total_nodes,
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000211 cipinfo->local_nodes[0],
212 cipinfo->hash_initval);
Harald Weltec340f6c2003-11-11 18:41:36 +0000213}
214
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000215static void CLUSTERIP_save(const void *ip, const struct xt_entry_target *target)
Harald Weltec340f6c2003-11-11 18:41:36 +0000216{
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000217 const struct ipt_clusterip_tgt_info *cipinfo =
218 (const struct ipt_clusterip_tgt_info *)target->data;
Harald Weltec340f6c2003-11-11 18:41:36 +0000219
KOVACS Krisztian3643aca2005-09-19 14:50:06 +0000220 /* if this is not a new entry, we don't need to save target
221 * parameters */
222 if (!cipinfo->flags & CLUSTERIP_FLAG_NEW)
223 return;
224
225 printf("--new --hashmode %s --clustermac %s --total-nodes %d --local-node %d --hash-init %u",
226 hashmode2str(cipinfo->hash_mode),
227 mac2str(cipinfo->clustermac),
228 cipinfo->num_total_nodes,
229 cipinfo->local_nodes[0],
230 cipinfo->hash_initval);
Harald Weltec340f6c2003-11-11 18:41:36 +0000231}
232
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200233static struct xtables_target clusterip_tg_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000234 .name = "CLUSTERIP",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200235 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100236 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200237 .size = XT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)),
Pablo Neira800938f2005-03-07 14:02:02 +0000238 .userspacesize = offsetof(struct ipt_clusterip_tgt_info, config),
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000239 .help = CLUSTERIP_help,
240 .parse = CLUSTERIP_parse,
241 .final_check = CLUSTERIP_check,
242 .print = CLUSTERIP_print,
243 .save = CLUSTERIP_save,
244 .extra_opts = CLUSTERIP_opts,
Harald Weltec340f6c2003-11-11 18:41:36 +0000245};
246
247void _init(void)
248{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200249 xtables_register_target(&clusterip_tg_reg);
Harald Weltec340f6c2003-11-11 18:41:36 +0000250}