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/bitops.h>
29 #include <asm/types.h>
31 #include <linux/netfilter_ipv4/ip_set_ipporthash.h>
32 #include <linux/netfilter_ipv4/ip_set_jhash.h>
36 #define OPT_CREATE_HASHSIZE 0x01U
37 #define OPT_CREATE_PROBES 0x02U
38 #define OPT_CREATE_RESIZE 0x04U
39 #define OPT_CREATE_NETWORK 0x08U
40 #define OPT_CREATE_FROM 0x10U
41 #define OPT_CREATE_TO 0x20U
43 /* Initialize the create. */
44 void create_init(void *data)
46 struct ip_set_req_ipporthash_create *mydata =
47 (struct ip_set_req_ipporthash_create *) data;
51 /* Default create parameters */
52 mydata->hashsize = 1024;
57 /* Function which parses command options; returns true if it ate an option */
58 int create_parse(int c, char *argv[], void *data, unsigned *flags)
60 struct ip_set_req_ipporthash_create *mydata =
61 (struct ip_set_req_ipporthash_create *) data;
69 if (string_to_number(optarg, 1, UINT_MAX - 1, &mydata->hashsize))
70 exit_error(PARAMETER_PROBLEM, "Invalid hashsize `%s' specified", optarg);
72 *flags |= OPT_CREATE_HASHSIZE;
74 DP("--hashsize %u", mydata->hashsize);
80 if (string_to_number(optarg, 1, 65535, &value))
81 exit_error(PARAMETER_PROBLEM, "Invalid probes `%s' specified", optarg);
83 mydata->probes = value;
84 *flags |= OPT_CREATE_PROBES;
86 DP("--probes %u", mydata->probes);
92 if (string_to_number(optarg, 0, 65535, &value))
93 exit_error(PARAMETER_PROBLEM, "Invalid resize `%s' specified", optarg);
95 mydata->resize = value;
96 *flags |= OPT_CREATE_RESIZE;
98 DP("--resize %u", mydata->resize);
103 parse_ip(optarg, &mydata->from);
105 *flags |= OPT_CREATE_FROM;
107 DP("--from %x (%s)", mydata->from,
108 ip_tostring_numeric(mydata->from));
113 parse_ip(optarg, &mydata->to);
115 *flags |= OPT_CREATE_TO;
117 DP("--to %x (%s)", mydata->to,
118 ip_tostring_numeric(mydata->to));
123 parse_ipandmask(optarg, &mydata->from, &mydata->to);
125 /* Make to the last of from + mask */
127 mydata->to = mydata->from | ~(mydata->to);
129 mydata->from = 0x00000000;
130 mydata->to = 0xFFFFFFFF;
132 *flags |= OPT_CREATE_NETWORK;
134 DP("--network from %x (%s)",
135 mydata->from, ip_tostring_numeric(mydata->from));
136 DP("--network to %x (%s)",
137 mydata->to, ip_tostring_numeric(mydata->to));
148 /* Final check; exit if not ok. */
149 void create_final(void *data, unsigned int flags)
151 struct ip_set_req_ipporthash_create *mydata =
152 (struct ip_set_req_ipporthash_create *) data;
155 DP("hashsize %u probes %u resize %u",
156 mydata->hashsize, mydata->probes, mydata->resize);
159 if (flags & OPT_CREATE_NETWORK) {
161 if ((flags & OPT_CREATE_FROM) || (flags & OPT_CREATE_TO))
162 exit_error(PARAMETER_PROBLEM,
163 "Can't specify --from or --to with --network\n");
164 } else if (flags & (OPT_CREATE_FROM | OPT_CREATE_TO)) {
166 if (!(flags & OPT_CREATE_FROM) || !(flags & OPT_CREATE_TO))
167 exit_error(PARAMETER_PROBLEM,
168 "Need to specify both --from and --to\n");
170 exit_error(PARAMETER_PROBLEM,
171 "Need to specify --from and --to, or --network\n");
175 DP("from : %x to: %x diff: %x",
176 mydata->from, mydata->to,
177 mydata->to - mydata->from);
179 if (mydata->from > mydata->to)
180 exit_error(PARAMETER_PROBLEM,
181 "From can't be higher than to.\n");
183 if (mydata->to - mydata->from > MAX_RANGE)
184 exit_error(PARAMETER_PROBLEM,
185 "Range to large. Max is %d IPs in range\n",
189 /* Create commandline options */
190 static struct option create_opts[] = {
191 {"hashsize", 1, 0, '1'},
192 {"probes", 1, 0, '2'},
193 {"resize", 1, 0, '3'},
196 {"network", 1, 0, '6'},
200 /* Add, del, test parser */
201 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
203 struct ip_set_req_ipporthash *mydata =
204 (struct ip_set_req_ipporthash *) data;
205 char *saved = ipset_strdup(optarg);
206 char *ptr, *tmp = saved;
208 DP("ipporthash: %p %p", optarg, data);
210 ptr = strsep(&tmp, "%");
211 parse_ip(ptr, &mydata->ip);
214 parse_port(tmp, &mydata->port);
216 exit_error(PARAMETER_PROBLEM,
217 "IP address and port must be specified: ip%%port");
226 void initheader(struct set *set, const void *data)
228 struct ip_set_req_ipporthash_create *header =
229 (struct ip_set_req_ipporthash_create *) data;
230 struct ip_set_ipporthash *map =
231 (struct ip_set_ipporthash *) set->settype->header;
233 memset(map, 0, sizeof(struct ip_set_ipporthash));
234 map->hashsize = header->hashsize;
235 map->probes = header->probes;
236 map->resize = header->resize;
237 map->first_ip = header->from;
238 map->last_ip = header->to;
241 void printheader(struct set *set, unsigned options)
243 struct ip_set_ipporthash *mysetdata =
244 (struct ip_set_ipporthash *) set->settype->header;
246 printf(" from: %s", ip_tostring(mysetdata->first_ip, options));
247 printf(" to: %s", ip_tostring(mysetdata->last_ip, options));
248 printf(" hashsize: %u", mysetdata->hashsize);
249 printf(" probes: %u", mysetdata->probes);
250 printf(" resize: %u\n", mysetdata->resize);
253 void printips(struct set *set, void *data, size_t len, unsigned options)
255 struct ip_set_ipporthash *mysetdata =
256 (struct ip_set_ipporthash *) set->settype->header;
258 ip_set_ip_t *ipptr, ip;
261 while (offset < len) {
262 ipptr = data + offset;
264 ip = (*ipptr>>16) + mysetdata->first_ip;
265 port = (uint16_t) *ipptr;
267 ip_tostring(ip, options),
268 port_tostring(port, options));
270 offset += sizeof(ip_set_ip_t);
274 void saveheader(struct set *set, unsigned options)
276 struct ip_set_ipporthash *mysetdata =
277 (struct ip_set_ipporthash *) set->settype->header;
279 printf("-N %s %s --from %s",
280 set->name, set->settype->typename,
281 ip_tostring(mysetdata->first_ip, options));
283 ip_tostring(mysetdata->last_ip, options));
284 printf(" --hashsize %u --probes %u --resize %u\n",
285 mysetdata->hashsize, mysetdata->probes, mysetdata->resize);
288 /* Print save for an IP */
289 void saveips(struct set *set, void *data, size_t len, unsigned options)
291 struct ip_set_ipporthash *mysetdata =
292 (struct ip_set_ipporthash *) set->settype->header;
294 ip_set_ip_t *ipptr, ip;
297 while (offset < len) {
298 ipptr = data + offset;
300 ip = (*ipptr>>16) + mysetdata->first_ip;
301 port = (uint16_t) *ipptr;
302 printf("-A %s %s%%%s\n", set->name,
303 ip_tostring(ip, options),
304 port_tostring(port, options));
306 offset += sizeof(ip_set_ip_t);
310 static char buffer[22];
312 static char * unpack_ipport_tostring(struct set *set, ip_set_ip_t bip, unsigned options)
314 struct ip_set_ipporthash *mysetdata =
315 (struct ip_set_ipporthash *) set->settype->header;
316 ip_set_ip_t ip, port;
318 ip = (bip>>16) + mysetdata->first_ip;
319 port = (uint16_t) bip;
320 sprintf(buffer, "%s%%%s",
321 ip_tostring(ip, options), port_tostring(port, options));
329 ("-N set ipporthash --from IP --to IP\n"
330 " [--hashsize hashsize] [--probes probes ] [--resize resize]\n"
331 "-N set ipporthash --network IP/mask\n"
332 " [--hashsize hashsize] [--probes probes ] [--resize resize]\n"
335 "-T set IP%%port\n");
338 static struct settype settype_ipporthash = {
339 .typename = SETTYPE_NAME,
340 .protocol_version = IP_SET_PROTOCOL_VERSION,
343 .create_size = sizeof(struct ip_set_req_ipporthash_create),
344 .create_init = &create_init,
345 .create_parse = &create_parse,
346 .create_final = &create_final,
347 .create_opts = create_opts,
350 .adt_size = sizeof(struct ip_set_req_ipporthash),
351 .adt_parser = &adt_parser,
354 .header_size = sizeof(struct ip_set_ipporthash),
355 .initheader = &initheader,
356 .printheader = &printheader,
357 .printips = &printips, /* We only have the unsorted version */
358 .printips_sorted = &printips,
359 .saveheader = &saveheader,
363 .bindip_tostring = &unpack_ipport_tostring,
364 .bindip_parse = &parse_ip,
371 settype_register(&settype_ipporthash);