libipt_CLUSTERIP.c revision 500f483fff529dcd88ec96b9d5054be6cd6363a0
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 */
6#include <stdio.h>
7#include <string.h>
8#include <stdlib.h>
9#include <getopt.h>
10#include <stddef.h>
11
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>
20#include "../include/linux/netfilter_ipv4/ipt_CLUSTERIP.h"
21
22static void
23help(void)
24{
25	printf(
26"CLUSTERIP target v%s options:\n"
27"  --new			 Create a new ClusterIP\n"
28"  --hashmode <mode>		 Specify hashing mode\n"
29"					sourceip\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"
36"\n",
37IPTABLES_VERSION);
38}
39
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
46
47static const struct option opts[] = {
48	{ "new", 0, NULL, '1' },
49	{ "hashmode", 1, NULL, '2' },
50	{ "clustermac", 1, NULL, '3' },
51	{ "total-nodes", 1, NULL, '4' },
52	{ "local-node", 1, NULL, '5' },
53	{ "hash-init", 1, NULL, '6' },
54	{ }
55};
56
57static void
58init(struct xt_entry_target *t)
59{
60}
61
62static void
63parse_mac(const char *mac, char *macbuf)
64{
65	unsigned int i = 0;
66
67	if (strlen(mac) != ETH_ALEN*3-1)
68		exit_error(PARAMETER_PROBLEM, "Bad mac address `%s'", mac);
69
70	for (i = 0; i < ETH_ALEN; i++) {
71		long number;
72		char *end;
73
74		number = strtol(mac + i*3, &end, 16);
75
76		if (end == mac + i*3 + 2
77		    && number >= 0
78		    && number <= 255)
79			macbuf[i] = number;
80		else
81			exit_error(PARAMETER_PROBLEM,
82				   "Bad mac address `%s'", mac);
83	}
84}
85
86static int
87parse(int c, char **argv, int invert, unsigned int *flags,
88      const void *entry,
89      struct xt_entry_target **target)
90{
91	struct ipt_clusterip_tgt_info *cipinfo
92		= (struct ipt_clusterip_tgt_info *)(*target)->data;
93
94	switch (c) {
95		unsigned int num;
96	case '1':
97		cipinfo->flags |= CLUSTERIP_FLAG_NEW;
98		if (*flags & PARAM_NEW)
99			exit_error(PARAMETER_PROBLEM, "Can only specify `--new' once\n");
100		*flags |= PARAM_NEW;
101		break;
102	case '2':
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;
113		else
114			exit_error(PARAMETER_PROBLEM, "Unknown hashmode `%s'\n",
115				   optarg);
116		*flags |= PARAM_HMODE;
117		break;
118	case '3':
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");
126		*flags |= PARAM_MAC;
127		break;
128	case '4':
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;
137		break;
138	case '5':
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;
148		break;
149	case '6':
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;
158		break;
159	default:
160		return 0;
161	}
162
163	return 1;
164}
165
166static void
167final_check(unsigned int flags)
168{
169	if (flags == 0)
170		return;
171
172	if ((flags & (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE))
173		== (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE))
174		return;
175
176	exit_error(PARAMETER_PROBLEM, "CLUSTERIP target: Invalid parameter combination\n");
177}
178
179static char *hashmode2str(enum clusterip_hashmode mode)
180{
181	char *retstr;
182	switch (mode) {
183		case CLUSTERIP_HASHMODE_SIP:
184			retstr = "sourceip";
185			break;
186		case CLUSTERIP_HASHMODE_SIP_SPT:
187			retstr = "sourceip-sourceport";
188			break;
189		case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
190			retstr = "sourceip-sourceport-destport";
191			break;
192		default:
193			retstr = "unknown-error";
194			break;
195	}
196	return retstr;
197}
198
199static char *mac2str(const u_int8_t mac[ETH_ALEN])
200{
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]);
204	return buf;
205}
206
207
208/* Prints out the targinfo. */
209static void
210print(const void *ip,
211      const struct xt_entry_target *target,
212      int numeric)
213{
214	const struct ipt_clusterip_tgt_info *cipinfo =
215		(const struct ipt_clusterip_tgt_info *)target->data;
216
217	if (!cipinfo->flags & CLUSTERIP_FLAG_NEW) {
218		printf("CLUSTERIP");
219		return;
220	}
221
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);
228}
229
230/* Saves the union ipt_targinfo in parsable form to stdout. */
231static void
232save(const void *ip, const struct xt_entry_target *target)
233{
234	const struct ipt_clusterip_tgt_info *cipinfo =
235		(const struct ipt_clusterip_tgt_info *)target->data;
236
237	/* if this is not a new entry, we don't need to save target
238	 * parameters */
239	if (!cipinfo->flags & CLUSTERIP_FLAG_NEW)
240		return;
241
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);
248}
249
250static struct iptables_target clusterip = {
251	.name		= "CLUSTERIP",
252	.version	= IPTABLES_VERSION,
253	.size		= IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)),
254	.userspacesize	= offsetof(struct ipt_clusterip_tgt_info, config),
255 	.help		= &help,
256	.init		= &init,
257	.parse		= &parse,
258	.final_check	= &final_check,
259	.print		= &print,
260	.save		= &save,
261	.extra_opts	= opts
262};
263
264void _init(void)
265{
266	register_target(&clusterip);
267}
268