blob: 6741fd70e75e8ae7ec865821edcb2453018e391c [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>
10
11#if defined(__GLIBC__) && __GLIBC__ == 2
12#include <net/ethernet.h>
13#else
14#include <linux/if_ether.h>
15#endif
16
17#include <iptables.h>
18#include <linux/netfilter_ipv4/ip_tables.h>
19#include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
20
21static void
22help(void)
23{
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"
34" --hash-init\n"
35"\n",
36IPTABLES_VERSION);
37}
38
39static struct option opts[] = {
40 { "new", 0, 0, '1' },
41 { "hashmode", 1, 0, '2' },
42 { "clustermac", 1, 0, '3' },
43 { "total-nodes", 1, 0, '4' },
44 { "local-node", 1, 0, '5' },
45 { 0 }
46};
47
48static void
49init(struct ipt_entry_target *t, unsigned int *nfcache)
50{
51}
52
53static void
54parse_mac(const char *mac, char *macbuf)
55{
56 unsigned int i = 0;
57
58 if (strlen(mac) != ETH_ALEN*3-1)
59 exit_error(PARAMETER_PROBLEM, "Bad mac address `%s'", mac);
60
61 for (i = 0; i < ETH_ALEN; i++) {
62 long number;
63 char *end;
64
65 number = strtol(mac + i*3, &end, 16);
66
67 if (end == mac + i*3 + 2
68 && number >= 0
69 && number <= 255)
70 macbuf[i] = number;
71 else
72 exit_error(PARAMETER_PROBLEM,
73 "Bad mac address `%s'", mac);
74 }
75}
76
77#define PARAM_NEW 0x0001
78#define PARAM_HMODE 0x0002
79#define PARAM_MAC 0x0004
80#define PARAM_TOTALNODE 0x0008
81#define PARAM_LOCALNODE 0x0010
82
83static int
84parse(int c, char **argv, int invert, unsigned int *flags,
85 const struct ipt_entry *entry,
86 struct ipt_entry_target **target)
87{
88 struct ipt_clusterip_tgt_info *cipinfo
89 = (struct ipt_clusterip_tgt_info *)(*target)->data;
90
91 switch (c) {
92 unsigned int num;
93 case '1':
94 cipinfo->flags |= CLUSTERIP_FLAG_NEW;
95 if (*flags & PARAM_NEW)
96 exit_error(PARAMETER_PROBLEM, "Can only specify `--new' once\n");
97 *flags |= PARAM_NEW;
98 break;
99 case '2':
100 if (!(*flags & PARAM_NEW))
101 exit_error(PARAMETER_PROBLEM, "Can only specify hashmode combined with `--new'\n");
102 if (*flags & PARAM_HMODE)
103 exit_error(PARAMETER_PROBLEM, "Can only specify hashmode once\n");
104 if (!strcmp(optarg, "sourceip"))
105 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP;
106 else if (!strcmp(optarg, "sourceip-sourceport"))
107 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT;
108 else if (!strcmp(optarg, "sourceip-sourceport-destport"))
109 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT_DPT;
110 else
111 exit_error(PARAMETER_PROBLEM, "Unknown hashmode `%s'\n",
112 optarg);
113 *flags |= PARAM_HMODE;
114 break;
115 case '3':
116 if (!(*flags & PARAM_NEW))
117 exit_error(PARAMETER_PROBLEM, "Can only specify MAC combined with `--new'\n");
118 if (*flags & PARAM_MAC)
119 exit_error(PARAMETER_PROBLEM, "Can only specify MAC once\n");
120 parse_mac(optarg, cipinfo->clustermac);
121 *flags |= PARAM_MAC;
122 break;
123 case '4':
124 if (!(*flags & PARAM_NEW))
125 exit_error(PARAMETER_PROBLEM, "Can only specify node number combined with `--new'\n");
126 if (*flags & PARAM_TOTALNODE)
127 exit_error(PARAMETER_PROBLEM, "Can only specify total node number once\n");
128 if (string_to_number(optarg, 1, CLUSTERIP_MAX_NODES, &num) < 0)
129 exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg);
130 cipinfo->num_total_nodes = (u_int16_t)num;
131 *flags |= PARAM_TOTALNODE;
132 break;
133 case '5':
134 if (!(*flags & PARAM_NEW))
135 exit_error(PARAMETER_PROBLEM, "Can only specify node number combined with `--new'\n");
136 if (*flags & PARAM_LOCALNODE)
137 exit_error(PARAMETER_PROBLEM, "Can only specify local node number once\n");
138 if (string_to_number(optarg, 1, CLUSTERIP_MAX_NODES, &num) < 0)
139 exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg);
140 cipinfo->num_local_nodes = 1;
141 cipinfo->local_nodes[0] = (u_int16_t)num;
142 *flags |= PARAM_LOCALNODE;
143 break;
144 default:
145 return 0;
146 }
147
148 return 1;
149}
150
151static void
152final_check(unsigned int flags)
153{
154 if (flags == 0)
155 return;
156
157 if (flags == (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE))
158 return;
159
160 exit_error(PARAMETER_PROBLEM, "CLUSTERIP target: Invalid parameter combination\n");
161}
162
163static char *hashmode2str(enum clusterip_hashmode mode)
164{
165 char *retstr;
166 switch (mode) {
167 case CLUSTERIP_HASHMODE_SIP:
168 retstr = "sourceip";
169 break;
170 case CLUSTERIP_HASHMODE_SIP_SPT:
171 retstr = "sourceip-sourceport";
172 break;
173 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
174 retstr = "sourceip-sourceport-destport";
175 break;
176 default:
177 retstr = "unknown-error";
178 break;
179 }
180 return retstr;
181}
182
183static char *mac2str(u_int8_t mac[ETH_ALEN])
184{
185 static char buf[ETH_ALEN*3];
186 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
187 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
188 return buf;
189}
190
191
192/* Prints out the targinfo. */
193static void
194print(const struct ipt_ip *ip,
195 const struct ipt_entry_target *target,
196 int numeric)
197{
198 const struct ipt_clusterip_tgt_info *cipinfo =
199 (const struct ipt_clusterip_tgt_info *)target->data;
200
201 if (!cipinfo->flags & CLUSTERIP_FLAG_NEW) {
202 printf("CLUSTERIP");
203 return;
204 }
205
206 printf("CLUSTERIP hashmode=%s clustermac=%s total_nodes=%u local_node=%u ",
207 hashmode2str(cipinfo->hash_mode),
208 mac2str(cipinfo->clustermac),
209 cipinfo->num_total_nodes,
210 cipinfo->local_nodes[0]);
211}
212
213/* Saves the union ipt_targinfo in parsable form to stdout. */
214static void
215save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
216{
217 /*
218 const struct ipt_connmark_target_info *markinfo =
219 (const struct ipt_connmark_target_info *)target->data;
220
221 switch (markinfo->mode) {
222 case IPT_CONNMARK_SET:
223 printf("--set-mark 0x%lx ", markinfo->mark);
224 break;
225 case IPT_CONNMARK_SAVE:
226 printf("--save-mark ");
227 break;
228 case IPT_CONNMARK_RESTORE:
229 printf("--restore-mark ");
230 break;
231 default:
232 printf("ERROR: UNKNOWN CONNMARK MODE ");
233 break;
234 }
235 */
236}
237
238static
239struct iptables_target clusterip
240= { NULL,
241 "CLUSTERIP",
242 IPTABLES_VERSION,
243 IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)),
244 IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)),
245 &help,
246 &init,
247 &parse,
248 &final_check,
249 &print,
250 &save,
251 opts
252};
253
254void _init(void)
255{
256 register_target(&clusterip);
257}