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 #ifndef __always_inline
29 #define __always_inline inline
31 #include <asm/bitops.h>
32 #include <asm/types.h>
34 #include <linux/netfilter_ipv4/ip_set_ipporthash.h>
35 #include <linux/netfilter_ipv4/ip_set_jhash.h>
39 #define OPT_CREATE_HASHSIZE 0x01U
40 #define OPT_CREATE_PROBES 0x02U
41 #define OPT_CREATE_RESIZE 0x04U
42 #define OPT_CREATE_NETWORK 0x08U
43 #define OPT_CREATE_FROM 0x10U
44 #define OPT_CREATE_TO 0x20U
46 /* Initialize the create. */
47 void create_init(void *data)
49 struct ip_set_req_ipporthash_create *mydata =
50 (struct ip_set_req_ipporthash_create *) data;
54 /* Default create parameters */
55 mydata->hashsize = 1024;
60 /* Function which parses command options; returns true if it ate an option */
61 int create_parse(int c, char *argv[], void *data, unsigned *flags)
63 struct ip_set_req_ipporthash_create *mydata =
64 (struct ip_set_req_ipporthash_create *) data;
72 if (string_to_number(optarg, 1, UINT_MAX - 1, &mydata->hashsize))
73 exit_error(PARAMETER_PROBLEM, "Invalid hashsize `%s' specified", optarg);
75 *flags |= OPT_CREATE_HASHSIZE;
77 DP("--hashsize %u", mydata->hashsize);
83 if (string_to_number(optarg, 1, 65535, &value))
84 exit_error(PARAMETER_PROBLEM, "Invalid probes `%s' specified", optarg);
86 mydata->probes = value;
87 *flags |= OPT_CREATE_PROBES;
89 DP("--probes %u", mydata->probes);
95 if (string_to_number(optarg, 0, 65535, &value))
96 exit_error(PARAMETER_PROBLEM, "Invalid resize `%s' specified", optarg);
98 mydata->resize = value;
99 *flags |= OPT_CREATE_RESIZE;
101 DP("--resize %u", mydata->resize);
106 parse_ip(optarg, &mydata->from);
108 *flags |= OPT_CREATE_FROM;
110 DP("--from %x (%s)", mydata->from,
111 ip_tostring_numeric(mydata->from));
116 parse_ip(optarg, &mydata->to);
118 *flags |= OPT_CREATE_TO;
120 DP("--to %x (%s)", mydata->to,
121 ip_tostring_numeric(mydata->to));
126 parse_ipandmask(optarg, &mydata->from, &mydata->to);
128 /* Make to the last of from + mask */
130 mydata->to = mydata->from | ~(mydata->to);
132 mydata->from = 0x00000000;
133 mydata->to = 0xFFFFFFFF;
135 *flags |= OPT_CREATE_NETWORK;
137 DP("--network from %x (%s)",
138 mydata->from, ip_tostring_numeric(mydata->from));
139 DP("--network to %x (%s)",
140 mydata->to, ip_tostring_numeric(mydata->to));
151 /* Final check; exit if not ok. */
152 void create_final(void *data, unsigned int flags)
154 struct ip_set_req_ipporthash_create *mydata =
155 (struct ip_set_req_ipporthash_create *) data;
158 DP("hashsize %u probes %u resize %u",
159 mydata->hashsize, mydata->probes, mydata->resize);
162 if (flags & OPT_CREATE_NETWORK) {
164 if ((flags & OPT_CREATE_FROM) || (flags & OPT_CREATE_TO))
165 exit_error(PARAMETER_PROBLEM,
166 "Can't specify --from or --to with --network\n");
167 } else if (flags & (OPT_CREATE_FROM | OPT_CREATE_TO)) {
169 if (!(flags & OPT_CREATE_FROM) || !(flags & OPT_CREATE_TO))
170 exit_error(PARAMETER_PROBLEM,
171 "Need to specify both --from and --to\n");
173 exit_error(PARAMETER_PROBLEM,
174 "Need to specify --from and --to, or --network\n");
178 DP("from : %x to: %x diff: %x",
179 mydata->from, mydata->to,
180 mydata->to - mydata->from);
182 if (mydata->from > mydata->to)
183 exit_error(PARAMETER_PROBLEM,
184 "From can't be higher than to.\n");
186 if (mydata->to - mydata->from > MAX_RANGE)
187 exit_error(PARAMETER_PROBLEM,
188 "Range to large. Max is %d IPs in range\n",
192 /* Create commandline options */
193 static struct option create_opts[] = {
194 {"hashsize", 1, 0, '1'},
195 {"probes", 1, 0, '2'},
196 {"resize", 1, 0, '3'},
199 {"network", 1, 0, '6'},
203 /* Add, del, test parser */
204 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
206 struct ip_set_req_ipporthash *mydata =
207 (struct ip_set_req_ipporthash *) data;
208 char *saved = ipset_strdup(optarg);
209 char *ptr, *tmp = saved;
211 DP("ipporthash: %p %p", optarg, data);
213 ptr = strsep(&tmp, "%");
214 parse_ip(ptr, &mydata->ip);
217 parse_port(tmp, &mydata->port);
219 exit_error(PARAMETER_PROBLEM,
220 "IP address and port must be specified: ip%%port");
229 void initheader(struct set *set, const void *data)
231 struct ip_set_req_ipporthash_create *header =
232 (struct ip_set_req_ipporthash_create *) data;
233 struct ip_set_ipporthash *map =
234 (struct ip_set_ipporthash *) set->settype->header;
236 memset(map, 0, sizeof(struct ip_set_ipporthash));
237 map->hashsize = header->hashsize;
238 map->probes = header->probes;
239 map->resize = header->resize;
240 map->first_ip = header->from;
241 map->last_ip = header->to;
244 void printheader(struct set *set, unsigned options)
246 struct ip_set_ipporthash *mysetdata =
247 (struct ip_set_ipporthash *) set->settype->header;
249 printf(" from: %s", ip_tostring(mysetdata->first_ip, options));
250 printf(" to: %s", ip_tostring(mysetdata->last_ip, options));
251 printf(" hashsize: %u", mysetdata->hashsize);
252 printf(" probes: %u", mysetdata->probes);
253 printf(" resize: %u\n", mysetdata->resize);
256 void printips(struct set *set, void *data, size_t len, unsigned options)
258 struct ip_set_ipporthash *mysetdata =
259 (struct ip_set_ipporthash *) set->settype->header;
261 ip_set_ip_t *ipptr, ip;
264 while (offset < len) {
265 ipptr = data + offset;
267 ip = (*ipptr>>16) + mysetdata->first_ip;
268 port = (uint16_t) *ipptr;
270 ip_tostring(ip, options),
271 port_tostring(port, options));
273 offset += sizeof(ip_set_ip_t);
277 void saveheader(struct set *set, unsigned options)
279 struct ip_set_ipporthash *mysetdata =
280 (struct ip_set_ipporthash *) set->settype->header;
282 printf("-N %s %s --from %s",
283 set->name, set->settype->typename,
284 ip_tostring(mysetdata->first_ip, options));
286 ip_tostring(mysetdata->last_ip, options));
287 printf(" --hashsize %u --probes %u --resize %u\n",
288 mysetdata->hashsize, mysetdata->probes, mysetdata->resize);
291 /* Print save for an IP */
292 void saveips(struct set *set, void *data, size_t len, unsigned options)
294 struct ip_set_ipporthash *mysetdata =
295 (struct ip_set_ipporthash *) set->settype->header;
297 ip_set_ip_t *ipptr, ip;
300 while (offset < len) {
301 ipptr = data + offset;
303 ip = (*ipptr>>16) + mysetdata->first_ip;
304 port = (uint16_t) *ipptr;
305 printf("-A %s %s%%%s\n", set->name,
306 ip_tostring(ip, options),
307 port_tostring(port, options));
309 offset += sizeof(ip_set_ip_t);
313 static char buffer[22];
315 static char * unpack_ipport_tostring(struct set *set, ip_set_ip_t bip, unsigned options)
317 struct ip_set_ipporthash *mysetdata =
318 (struct ip_set_ipporthash *) set->settype->header;
319 ip_set_ip_t ip, port;
321 ip = (bip>>16) + mysetdata->first_ip;
322 port = (uint16_t) bip;
323 sprintf(buffer, "%s%%%s",
324 ip_tostring(ip, options), port_tostring(port, options));
332 ("-N set ipporthash --from IP --to IP\n"
333 " [--hashsize hashsize] [--probes probes ] [--resize resize]\n"
334 "-N set ipporthash --network IP/mask\n"
335 " [--hashsize hashsize] [--probes probes ] [--resize resize]\n"
338 "-T set IP%%port\n");
341 static struct settype settype_ipporthash = {
342 .typename = SETTYPE_NAME,
343 .protocol_version = IP_SET_PROTOCOL_VERSION,
346 .create_size = sizeof(struct ip_set_req_ipporthash_create),
347 .create_init = &create_init,
348 .create_parse = &create_parse,
349 .create_final = &create_final,
350 .create_opts = create_opts,
353 .adt_size = sizeof(struct ip_set_req_ipporthash),
354 .adt_parser = &adt_parser,
357 .header_size = sizeof(struct ip_set_ipporthash),
358 .initheader = &initheader,
359 .printheader = &printheader,
360 .printips = &printips, /* We only have the unsorted version */
361 .printips_sorted = &printips,
362 .saveheader = &saveheader,
366 .bindip_tostring = &unpack_ipport_tostring,
367 .bindip_parse = &parse_ip,
374 settype_register(&settype_ipporthash);