New routers supported
[tomato.git] / release / src / router / ipset / ipset_ipporthash.c
blob5c114d0db7d1ef9cb221fe29b4c8a41f946c5478
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.
7 *
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
18 #include <limits.h> /* UINT_MAX */
19 #include <stdio.h> /* *printf */
20 #include <string.h> /* mem*, str* */
22 #include "ipset.h"
24 #include <linux/netfilter_ipv4/ip_set_ipporthash.h>
26 #define OPT_CREATE_HASHSIZE 0x01U
27 #define OPT_CREATE_PROBES 0x02U
28 #define OPT_CREATE_RESIZE 0x04U
29 #define OPT_CREATE_NETWORK 0x08U
30 #define OPT_CREATE_FROM 0x10U
31 #define OPT_CREATE_TO 0x20U
33 /* Initialize the create. */
34 static void
35 ipporthash_create_init(void *data)
37 struct ip_set_req_ipporthash_create *mydata = data;
39 DP("create INIT");
41 /* Default create parameters */
42 mydata->hashsize = IP_NF_SET_HASHSIZE;
43 mydata->probes = 8;
44 mydata->resize = 50;
47 /* Function which parses command options; returns true if it ate an option */
48 static int
49 ipporthash_create_parse(int c, char *argv[] UNUSED, void *data,
50 unsigned *flags)
52 struct ip_set_req_ipporthash_create *mydata = data;
53 ip_set_ip_t value;
55 DP("create_parse");
57 switch (c) {
58 case '1':
60 if (string_to_number(optarg, 1, UINT_MAX - 1, &mydata->hashsize))
61 exit_error(PARAMETER_PROBLEM, "Invalid hashsize `%s' specified", optarg);
63 *flags |= OPT_CREATE_HASHSIZE;
65 DP("--hashsize %u", mydata->hashsize);
67 break;
69 case '2':
71 if (string_to_number(optarg, 1, 65535, &value))
72 exit_error(PARAMETER_PROBLEM, "Invalid probes `%s' specified", optarg);
74 mydata->probes = value;
75 *flags |= OPT_CREATE_PROBES;
77 DP("--probes %u", mydata->probes);
79 break;
81 case '3':
83 if (string_to_number(optarg, 0, 65535, &value))
84 exit_error(PARAMETER_PROBLEM, "Invalid resize `%s' specified", optarg);
86 mydata->resize = value;
87 *flags |= OPT_CREATE_RESIZE;
89 DP("--resize %u", mydata->resize);
91 break;
93 case '4':
94 parse_ip(optarg, &mydata->from);
96 *flags |= OPT_CREATE_FROM;
98 DP("--from %x (%s)", mydata->from,
99 ip_tostring_numeric(mydata->from));
101 break;
103 case '5':
104 parse_ip(optarg, &mydata->to);
106 *flags |= OPT_CREATE_TO;
108 DP("--to %x (%s)", mydata->to,
109 ip_tostring_numeric(mydata->to));
111 break;
113 case '6':
114 parse_ipandmask(optarg, &mydata->from, &mydata->to);
116 /* Make to the last of from + mask */
117 if (mydata->to)
118 mydata->to = mydata->from | ~(mydata->to);
119 else {
120 mydata->from = 0x00000000;
121 mydata->to = 0xFFFFFFFF;
123 *flags |= OPT_CREATE_NETWORK;
125 DP("--network from %x (%s)",
126 mydata->from, ip_tostring_numeric(mydata->from));
127 DP("--network to %x (%s)",
128 mydata->to, ip_tostring_numeric(mydata->to));
130 break;
132 default:
133 return 0;
136 return 1;
139 /* Final check; exit if not ok. */
140 static void
141 ipporthash_create_final(void *data, unsigned int flags)
143 struct ip_set_req_ipporthash_create *mydata = data;
145 #ifdef IPSET_DEBUG
146 DP("hashsize %u probes %u resize %u",
147 mydata->hashsize, mydata->probes, mydata->resize);
148 #endif
150 if (flags & OPT_CREATE_NETWORK) {
151 /* --network */
152 if ((flags & OPT_CREATE_FROM) || (flags & OPT_CREATE_TO))
153 exit_error(PARAMETER_PROBLEM,
154 "Can't specify --from or --to with --network\n");
155 } else if (flags & (OPT_CREATE_FROM | OPT_CREATE_TO)) {
156 /* --from --to */
157 if (!(flags & OPT_CREATE_FROM) || !(flags & OPT_CREATE_TO))
158 exit_error(PARAMETER_PROBLEM,
159 "Need to specify both --from and --to\n");
160 } else {
161 exit_error(PARAMETER_PROBLEM,
162 "Need to specify --from and --to, or --network\n");
166 DP("from : %x to: %x diff: %x",
167 mydata->from, mydata->to,
168 mydata->to - mydata->from);
170 if (mydata->from > mydata->to)
171 exit_error(PARAMETER_PROBLEM,
172 "From can't be higher than to.\n");
174 if (mydata->to - mydata->from > MAX_RANGE)
175 exit_error(PARAMETER_PROBLEM,
176 "Range too large. Max is %d IPs in range\n",
177 MAX_RANGE+1);
180 /* Create commandline options */
181 static const struct option create_opts[] = {
182 {.name = "hashsize", .has_arg = required_argument, .val = '1'},
183 {.name = "probes", .has_arg = required_argument, .val = '2'},
184 {.name = "resize", .has_arg = required_argument, .val = '3'},
185 {.name = "from", .has_arg = required_argument, .val = '4'},
186 {.name = "to", .has_arg = required_argument, .val = '5'},
187 {.name = "network", .has_arg = required_argument, .val = '6'},
188 {NULL},
191 /* Add, del, test parser */
192 static ip_set_ip_t
193 ipporthash_adt_parser(int cmd UNUSED, const char *arg, void *data)
195 struct ip_set_req_ipporthash *mydata = data;
196 char *saved = ipset_strdup(arg);
197 char *ptr, *tmp = saved;
199 DP("ipporthash: %p %p", arg, data);
201 if (((ptr = strchr(tmp, ':')) || (ptr = strchr(tmp, '%'))) && ++warn_once == 1)
202 fprintf(stderr, "Warning: please use ',' separator token between ip,port.\n"
203 "Next release won't support old separator tokens.\n");
205 ptr = strsep(&tmp, ":%,");
206 parse_ip(ptr, &mydata->ip);
208 if (tmp)
209 parse_port(tmp, &mydata->port);
210 else
211 exit_error(PARAMETER_PROBLEM,
212 "IP address and port must be specified: ip,port");
214 if (!(mydata->ip || mydata->port))
215 exit_error(PARAMETER_PROBLEM,
216 "Zero valued IP address and port `%s' specified", arg);
217 ipset_free(saved);
218 return 1;
222 * Print and save
225 static void
226 ipporthash_initheader(struct set *set, const void *data)
228 const struct ip_set_req_ipporthash_create *header = data;
229 struct ip_set_ipporthash *map = set->settype->header;
231 memset(map, 0, sizeof(struct ip_set_ipporthash));
232 map->hashsize = header->hashsize;
233 map->probes = header->probes;
234 map->resize = header->resize;
235 map->first_ip = header->from;
236 map->last_ip = header->to;
239 static void
240 ipporthash_printheader(struct set *set, unsigned options)
242 struct ip_set_ipporthash *mysetdata = set->settype->header;
244 printf(" from: %s", ip_tostring(mysetdata->first_ip, options));
245 printf(" to: %s", ip_tostring(mysetdata->last_ip, options));
246 printf(" hashsize: %u", mysetdata->hashsize);
247 printf(" probes: %u", mysetdata->probes);
248 printf(" resize: %u\n", mysetdata->resize);
251 static void
252 ipporthash_printips(struct set *set, void *data, u_int32_t len,
253 unsigned options, char dont_align)
255 struct ip_set_ipporthash *mysetdata = set->settype->header;
256 size_t offset = 0;
257 ip_set_ip_t *ipptr, ip;
258 uint16_t port;
260 while (offset < len) {
261 ipptr = data + offset;
262 ip = (*ipptr>>16) + mysetdata->first_ip;
263 port = (uint16_t) *ipptr;
264 printf("%s,%s\n",
265 ip_tostring(ip, options),
266 port_tostring(port, options));
267 offset += IPSET_VALIGN(sizeof(ip_set_ip_t), dont_align);
271 static void
272 ipporthash_saveheader(struct set *set, unsigned options)
274 struct ip_set_ipporthash *mysetdata = set->settype->header;
276 printf("-N %s %s --from %s",
277 set->name, set->settype->typename,
278 ip_tostring(mysetdata->first_ip, options));
279 printf(" --to %s",
280 ip_tostring(mysetdata->last_ip, options));
281 printf(" --hashsize %u --probes %u --resize %u\n",
282 mysetdata->hashsize, mysetdata->probes, mysetdata->resize);
285 /* Print save for an IP */
286 static void
287 ipporthash_saveips(struct set *set, void *data, u_int32_t len,
288 unsigned options, char dont_align)
290 struct ip_set_ipporthash *mysetdata = set->settype->header;
291 size_t offset = 0;
292 ip_set_ip_t *ipptr, ip;
293 uint16_t port;
295 while (offset < len) {
296 ipptr = data + offset;
297 ip = (*ipptr>>16) + mysetdata->first_ip;
298 port = (uint16_t) *ipptr;
299 printf("-A %s %s,%s\n", set->name,
300 ip_tostring(ip, options),
301 port_tostring(port, options));
302 offset += IPSET_VALIGN(sizeof(ip_set_ip_t), dont_align);
306 static void
307 ipporthash_usage(void)
309 printf
310 ("-N set ipporthash --from IP --to IP\n"
311 " [--hashsize hashsize] [--probes probes ] [--resize resize]\n"
312 "-N set ipporthash --network IP/mask\n"
313 " [--hashsize hashsize] [--probes probes ] [--resize resize]\n"
314 "-A set IP,port\n"
315 "-D set IP,port\n"
316 "-T set IP,port\n");
319 static struct settype settype_ipporthash = {
320 .typename = SETTYPE_NAME,
321 .protocol_version = IP_SET_PROTOCOL_VERSION,
323 /* Create */
324 .create_size = sizeof(struct ip_set_req_ipporthash_create),
325 .create_init = ipporthash_create_init,
326 .create_parse = ipporthash_create_parse,
327 .create_final = ipporthash_create_final,
328 .create_opts = create_opts,
330 /* Add/del/test */
331 .adt_size = sizeof(struct ip_set_req_ipporthash),
332 .adt_parser = ipporthash_adt_parser,
334 /* Printing */
335 .header_size = sizeof(struct ip_set_ipporthash),
336 .initheader = ipporthash_initheader,
337 .printheader = ipporthash_printheader,
338 .printips = ipporthash_printips,
339 .printips_sorted = ipporthash_printips,
340 .saveheader = ipporthash_saveheader,
341 .saveips = ipporthash_saveips,
343 .usage = ipporthash_usage,
346 CONSTRUCTOR(ipporthash)
348 settype_register(&settype_ipporthash);