1 /* Copyright 2004 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <asm/types.h>
30 #include <linux/netfilter_ipv4/ip_set_ipporthash.h>
31 #include <linux/netfilter_ipv4/ip_set_jhash.h>
35 #define OPT_CREATE_HASHSIZE 0x01U
36 #define OPT_CREATE_PROBES 0x02U
37 #define OPT_CREATE_RESIZE 0x04U
38 #define OPT_CREATE_NETWORK 0x08U
39 #define OPT_CREATE_FROM 0x10U
40 #define OPT_CREATE_TO 0x20U
42 /* Initialize the create. */
43 void create_init(void *data)
45 struct ip_set_req_ipporthash_create *mydata =
46 (struct ip_set_req_ipporthash_create *) data;
50 /* Default create parameters */
51 mydata->hashsize = 1024;
56 /* Function which parses command options; returns true if it ate an option */
57 int create_parse(int c, char *argv[], void *data, unsigned *flags)
59 struct ip_set_req_ipporthash_create *mydata =
60 (struct ip_set_req_ipporthash_create *) data;
68 if (string_to_number(optarg, 1, UINT_MAX - 1, &mydata->hashsize))
69 exit_error(PARAMETER_PROBLEM, "Invalid hashsize `%s' specified", optarg);
71 *flags |= OPT_CREATE_HASHSIZE;
73 DP("--hashsize %u", mydata->hashsize);
79 if (string_to_number(optarg, 1, 65535, &value))
80 exit_error(PARAMETER_PROBLEM, "Invalid probes `%s' specified", optarg);
82 mydata->probes = value;
83 *flags |= OPT_CREATE_PROBES;
85 DP("--probes %u", mydata->probes);
91 if (string_to_number(optarg, 0, 65535, &value))
92 exit_error(PARAMETER_PROBLEM, "Invalid resize `%s' specified", optarg);
94 mydata->resize = value;
95 *flags |= OPT_CREATE_RESIZE;
97 DP("--resize %u", mydata->resize);
102 parse_ip(optarg, &mydata->from);
104 *flags |= OPT_CREATE_FROM;
106 DP("--from %x (%s)", mydata->from,
107 ip_tostring_numeric(mydata->from));
112 parse_ip(optarg, &mydata->to);
114 *flags |= OPT_CREATE_TO;
116 DP("--to %x (%s)", mydata->to,
117 ip_tostring_numeric(mydata->to));
122 parse_ipandmask(optarg, &mydata->from, &mydata->to);
124 /* Make to the last of from + mask */
126 mydata->to = mydata->from | ~(mydata->to);
128 mydata->from = 0x00000000;
129 mydata->to = 0xFFFFFFFF;
131 *flags |= OPT_CREATE_NETWORK;
133 DP("--network from %x (%s)",
134 mydata->from, ip_tostring_numeric(mydata->from));
135 DP("--network to %x (%s)",
136 mydata->to, ip_tostring_numeric(mydata->to));
147 /* Final check; exit if not ok. */
148 void create_final(void *data, unsigned int flags)
150 struct ip_set_req_ipporthash_create *mydata =
151 (struct ip_set_req_ipporthash_create *) data;
154 DP("hashsize %u probes %u resize %u",
155 mydata->hashsize, mydata->probes, mydata->resize);
158 if (flags & OPT_CREATE_NETWORK) {
160 if ((flags & OPT_CREATE_FROM) || (flags & OPT_CREATE_TO))
161 exit_error(PARAMETER_PROBLEM,
162 "Can't specify --from or --to with --network\n");
163 } else if (flags & (OPT_CREATE_FROM | OPT_CREATE_TO)) {
165 if (!(flags & OPT_CREATE_FROM) || !(flags & OPT_CREATE_TO))
166 exit_error(PARAMETER_PROBLEM,
167 "Need to specify both --from and --to\n");
169 exit_error(PARAMETER_PROBLEM,
170 "Need to specify --from and --to, or --network\n");
174 DP("from : %x to: %x diff: %x",
175 mydata->from, mydata->to,
176 mydata->to - mydata->from);
178 if (mydata->from > mydata->to)
179 exit_error(PARAMETER_PROBLEM,
180 "From can't be higher than to.\n");
182 if (mydata->to - mydata->from > MAX_RANGE)
183 exit_error(PARAMETER_PROBLEM,
184 "Range too large. Max is %d IPs in range\n",
188 /* Create commandline options */
189 static struct option create_opts[] = {
190 {"hashsize", 1, 0, '1'},
191 {"probes", 1, 0, '2'},
192 {"resize", 1, 0, '3'},
195 {"network", 1, 0, '6'},
199 /* Add, del, test parser */
200 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
202 struct ip_set_req_ipporthash *mydata =
203 (struct ip_set_req_ipporthash *) data;
204 char *saved = ipset_strdup(optarg);
205 char *ptr, *tmp = saved;
207 DP("ipporthash: %p %p", optarg, data);
209 ptr = strsep(&tmp, ":%");
210 parse_ip(ptr, &mydata->ip);
213 parse_port(tmp, &mydata->port);
215 exit_error(PARAMETER_PROBLEM,
216 "IP address and port must be specified: ip%%port");
225 void initheader(struct set *set, const void *data)
227 struct ip_set_req_ipporthash_create *header =
228 (struct ip_set_req_ipporthash_create *) data;
229 struct ip_set_ipporthash *map =
230 (struct ip_set_ipporthash *) set->settype->header;
232 memset(map, 0, sizeof(struct ip_set_ipporthash));
233 map->hashsize = header->hashsize;
234 map->probes = header->probes;
235 map->resize = header->resize;
236 map->first_ip = header->from;
237 map->last_ip = header->to;
240 void printheader(struct set *set, unsigned options)
242 struct ip_set_ipporthash *mysetdata =
243 (struct ip_set_ipporthash *) set->settype->header;
245 printf(" from: %s", ip_tostring(mysetdata->first_ip, options));
246 printf(" to: %s", ip_tostring(mysetdata->last_ip, options));
247 printf(" hashsize: %u", mysetdata->hashsize);
248 printf(" probes: %u", mysetdata->probes);
249 printf(" resize: %u\n", mysetdata->resize);
252 void printips(struct set *set, void *data, size_t len, unsigned options)
254 struct ip_set_ipporthash *mysetdata =
255 (struct ip_set_ipporthash *) set->settype->header;
257 ip_set_ip_t *ipptr, ip;
260 while (offset < len) {
261 ipptr = data + offset;
263 ip = (*ipptr>>16) + mysetdata->first_ip;
264 port = (uint16_t) *ipptr;
266 ip_tostring(ip, options),
267 port_tostring(port, options));
269 offset += sizeof(ip_set_ip_t);
273 void saveheader(struct set *set, unsigned options)
275 struct ip_set_ipporthash *mysetdata =
276 (struct ip_set_ipporthash *) set->settype->header;
278 printf("-N %s %s --from %s",
279 set->name, set->settype->typename,
280 ip_tostring(mysetdata->first_ip, options));
282 ip_tostring(mysetdata->last_ip, options));
283 printf(" --hashsize %u --probes %u --resize %u\n",
284 mysetdata->hashsize, mysetdata->probes, mysetdata->resize);
287 /* Print save for an IP */
288 void saveips(struct set *set, void *data, size_t len, unsigned options)
290 struct ip_set_ipporthash *mysetdata =
291 (struct ip_set_ipporthash *) set->settype->header;
293 ip_set_ip_t *ipptr, ip;
296 while (offset < len) {
297 ipptr = data + offset;
299 ip = (*ipptr>>16) + mysetdata->first_ip;
300 port = (uint16_t) *ipptr;
301 printf("-A %s %s:%s\n", set->name,
302 ip_tostring(ip, options),
303 port_tostring(port, options));
305 offset += sizeof(ip_set_ip_t);
309 static char buffer[22];
311 static char * unpack_ipport_tostring(struct set *set, ip_set_ip_t bip, unsigned options)
313 struct ip_set_ipporthash *mysetdata =
314 (struct ip_set_ipporthash *) set->settype->header;
315 ip_set_ip_t ip, port;
317 ip = (bip>>16) + mysetdata->first_ip;
318 port = (uint16_t) bip;
319 sprintf(buffer, "%s:%s",
320 ip_tostring(ip, options), port_tostring(port, options));
328 ("-N set ipporthash --from IP --to IP\n"
329 " [--hashsize hashsize] [--probes probes ] [--resize resize]\n"
330 "-N set ipporthash --network IP/mask\n"
331 " [--hashsize hashsize] [--probes probes ] [--resize resize]\n"
337 static struct settype settype_ipporthash = {
338 .typename = SETTYPE_NAME,
339 .protocol_version = IP_SET_PROTOCOL_VERSION,
342 .create_size = sizeof(struct ip_set_req_ipporthash_create),
343 .create_init = &create_init,
344 .create_parse = &create_parse,
345 .create_final = &create_final,
346 .create_opts = create_opts,
349 .adt_size = sizeof(struct ip_set_req_ipporthash),
350 .adt_parser = &adt_parser,
353 .header_size = sizeof(struct ip_set_ipporthash),
354 .initheader = &initheader,
355 .printheader = &printheader,
356 .printips = &printips, /* We only have the unsorted version */
357 .printips_sorted = &printips,
358 .saveheader = &saveheader,
362 .bindip_tostring = &unpack_ipport_tostring,
363 .bindip_parse = &parse_ip,
370 settype_register(&settype_ipporthash);