Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / iptables / extensions / libipt_CLUSTERIP.c
blob3268ac5395b01504c660dd208f295d4d1778700e
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/
5 */
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <getopt.h>
10 #include <stddef.h>
12 #if defined(__GLIBC__) && __GLIBC__ == 2
13 #include <net/ethernet.h>
14 #else
15 #include <linux/if_ether.h>
16 #endif
18 #include <iptables.h>
19 #include <linux/netfilter_ipv4/ip_tables.h>
20 #include "../include/linux/netfilter_ipv4/ipt_CLUSTERIP.h"
22 static void
23 help(void)
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",
37 IPTABLES_VERSION);
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
47 static struct option opts[] = {
48 { "new", 0, 0, '1' },
49 { "hashmode", 1, 0, '2' },
50 { "clustermac", 1, 0, '3' },
51 { "total-nodes", 1, 0, '4' },
52 { "local-node", 1, 0, '5' },
53 { "hash-init", 1, 0, '6' },
54 { 0 }
57 static void
58 init(struct ipt_entry_target *t, unsigned int *nfcache)
62 static void
63 parse_mac(const char *mac, char *macbuf)
65 unsigned int i = 0;
67 if (strlen(mac) != ETH_ALEN*3-1)
68 exit_error(PARAMETER_PROBLEM, "Bad mac address `%s'", mac);
70 for (i = 0; i < ETH_ALEN; i++) {
71 long number;
72 char *end;
74 number = strtol(mac + i*3, &end, 16);
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);
86 static int
87 parse(int c, char **argv, int invert, unsigned int *flags,
88 const struct ipt_entry *entry,
89 struct ipt_entry_target **target)
91 struct ipt_clusterip_tgt_info *cipinfo
92 = (struct ipt_clusterip_tgt_info *)(*target)->data;
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;
163 return 1;
166 static void
167 final_check(unsigned int flags)
169 if (flags == 0)
170 return;
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;
176 exit_error(PARAMETER_PROBLEM, "CLUSTERIP target: Invalid parameter combination\n");
179 static char *hashmode2str(enum clusterip_hashmode mode)
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;
196 return retstr;
199 static char *mac2str(const u_int8_t mac[ETH_ALEN])
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;
208 /* Prints out the targinfo. */
209 static void
210 print(const struct ipt_ip *ip,
211 const struct ipt_entry_target *target,
212 int numeric)
214 const struct ipt_clusterip_tgt_info *cipinfo =
215 (const struct ipt_clusterip_tgt_info *)target->data;
217 if (!cipinfo->flags & CLUSTERIP_FLAG_NEW) {
218 printf("CLUSTERIP");
219 return;
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);
230 /* Saves the union ipt_targinfo in parsable form to stdout. */
231 static void
232 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
234 const struct ipt_clusterip_tgt_info *cipinfo =
235 (const struct ipt_clusterip_tgt_info *)target->data;
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;
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);
250 static struct iptables_target clusterip = {
251 .next = NULL,
252 .name = "CLUSTERIP",
253 .version = IPTABLES_VERSION,
254 .size = IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)),
255 .userspacesize = offsetof(struct ipt_clusterip_tgt_info, config),
256 .help = &help,
257 .init = &init,
258 .parse = &parse,
259 .final_check = &final_check,
260 .print = &print,
261 .save = &save,
262 .extra_opts = opts
265 void _init(void)
267 register_target(&clusterip);