update to iptables-1.3.8
[iptables.git] / ipset / ipset_ipporthash.c
1 /* Copyright 2004 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
2  *
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.                           
12  *                                                                         
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
16  */
17
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <time.h>
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>
29
30 #include <linux/netfilter_ipv4/ip_set_ipporthash.h>
31 #include <linux/netfilter_ipv4/ip_set_jhash.h>
32
33 #include "ipset.h"
34
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
41
42 /* Initialize the create. */
43 void create_init(void *data)
44 {
45         struct ip_set_req_ipporthash_create *mydata =
46             (struct ip_set_req_ipporthash_create *) data;
47
48         DP("create INIT");
49
50         /* Default create parameters */ 
51         mydata->hashsize = 1024;
52         mydata->probes = 8;
53         mydata->resize = 50;
54 }
55
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)
58 {
59         struct ip_set_req_ipporthash_create *mydata =
60             (struct ip_set_req_ipporthash_create *) data;
61         ip_set_ip_t value;
62
63         DP("create_parse");
64
65         switch (c) {
66         case '1':
67
68                 if (string_to_number(optarg, 1, UINT_MAX - 1, &mydata->hashsize))
69                         exit_error(PARAMETER_PROBLEM, "Invalid hashsize `%s' specified", optarg);
70
71                 *flags |= OPT_CREATE_HASHSIZE;
72
73                 DP("--hashsize %u", mydata->hashsize);
74                 
75                 break;
76
77         case '2':
78
79                 if (string_to_number(optarg, 1, 65535, &value))
80                         exit_error(PARAMETER_PROBLEM, "Invalid probes `%s' specified", optarg);
81
82                 mydata->probes = value;
83                 *flags |= OPT_CREATE_PROBES;
84
85                 DP("--probes %u", mydata->probes);
86                 
87                 break;
88
89         case '3':
90
91                 if (string_to_number(optarg, 0, 65535, &value))
92                         exit_error(PARAMETER_PROBLEM, "Invalid resize `%s' specified", optarg);
93
94                 mydata->resize = value;
95                 *flags |= OPT_CREATE_RESIZE;
96
97                 DP("--resize %u", mydata->resize);
98                 
99                 break;
100
101         case '4':
102                 parse_ip(optarg, &mydata->from);
103
104                 *flags |= OPT_CREATE_FROM;
105
106                 DP("--from %x (%s)", mydata->from,
107                    ip_tostring_numeric(mydata->from));
108
109                 break;
110
111         case '5':
112                 parse_ip(optarg, &mydata->to);
113
114                 *flags |= OPT_CREATE_TO;
115
116                 DP("--to %x (%s)", mydata->to,
117                    ip_tostring_numeric(mydata->to));
118
119                 break;
120
121         case '6':
122                 parse_ipandmask(optarg, &mydata->from, &mydata->to);
123
124                 /* Make to the last of from + mask */
125                 if (mydata->to)
126                         mydata->to = mydata->from | ~(mydata->to);
127                 else {
128                         mydata->from = 0x00000000;
129                         mydata->to = 0xFFFFFFFF;
130                 }
131                 *flags |= OPT_CREATE_NETWORK;
132
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));
137
138                 break;
139
140         default:
141                 return 0;
142         }
143
144         return 1;
145 }
146
147 /* Final check; exit if not ok. */
148 void create_final(void *data, unsigned int flags)
149 {
150         struct ip_set_req_ipporthash_create *mydata =
151             (struct ip_set_req_ipporthash_create *) data;
152
153 #ifdef IPSET_DEBUG
154         DP("hashsize %u probes %u resize %u",
155            mydata->hashsize, mydata->probes, mydata->resize);
156 #endif
157
158         if (flags & OPT_CREATE_NETWORK) {
159                 /* --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)) {
164                 /* --from --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");
168         } else {
169                 exit_error(PARAMETER_PROBLEM,
170                            "Need to specify --from and --to, or --network\n");
171
172         }
173
174         DP("from : %x to: %x diff: %x", 
175            mydata->from, mydata->to,
176            mydata->to - mydata->from);
177
178         if (mydata->from > mydata->to)
179                 exit_error(PARAMETER_PROBLEM,
180                            "From can't be higher than to.\n");
181
182         if (mydata->to - mydata->from > MAX_RANGE)
183                 exit_error(PARAMETER_PROBLEM,
184                            "Range too large. Max is %d IPs in range\n",
185                            MAX_RANGE+1);
186 }
187
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'},
193         {"from", 1, 0, '4'},
194         {"to", 1, 0, '5'},
195         {"network", 1, 0, '6'},
196         {0}
197 };
198
199 /* Add, del, test parser */
200 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
201 {
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;
206
207         DP("ipporthash: %p %p", optarg, data);
208
209         ptr = strsep(&tmp, ":%");
210         parse_ip(ptr, &mydata->ip);
211
212         if (tmp)
213                 parse_port(tmp, &mydata->port);
214         else
215                 exit_error(PARAMETER_PROBLEM,
216                            "IP address and port must be specified: ip%%port");
217         free(saved);
218         return 1;       
219 };
220
221 /*
222  * Print and save
223  */
224
225 void initheader(struct set *set, const void *data)
226 {
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;
231
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;
238 }
239
240 void printheader(struct set *set, unsigned options)
241 {
242         struct ip_set_ipporthash *mysetdata =
243             (struct ip_set_ipporthash *) set->settype->header;
244
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);
250 }
251
252 void printips(struct set *set, void *data, size_t len, unsigned options)
253 {
254         struct ip_set_ipporthash *mysetdata =
255             (struct ip_set_ipporthash *) set->settype->header;
256         size_t offset = 0;
257         ip_set_ip_t *ipptr, ip;
258         uint16_t port;
259
260         while (offset < len) {
261                 ipptr = data + offset;
262                 if (*ipptr) {
263                         ip = (*ipptr>>16) + mysetdata->first_ip;
264                         port = (uint16_t) *ipptr;
265                         printf("%s:%s\n", 
266                                ip_tostring(ip, options),
267                                port_tostring(port, options));
268                 }
269                 offset += sizeof(ip_set_ip_t);
270         }
271 }
272
273 void saveheader(struct set *set, unsigned options)
274 {
275         struct ip_set_ipporthash *mysetdata =
276             (struct ip_set_ipporthash *) set->settype->header;
277
278         printf("-N %s %s --from %s",
279                set->name, set->settype->typename,
280                ip_tostring(mysetdata->first_ip, options));
281         printf(" --to %s",
282                ip_tostring(mysetdata->last_ip, options));
283         printf(" --hashsize %u --probes %u --resize %u\n",
284                mysetdata->hashsize, mysetdata->probes, mysetdata->resize);
285 }
286
287 /* Print save for an IP */
288 void saveips(struct set *set, void *data, size_t len, unsigned options)
289 {
290         struct ip_set_ipporthash *mysetdata =
291             (struct ip_set_ipporthash *) set->settype->header;
292         size_t offset = 0;
293         ip_set_ip_t *ipptr, ip;
294         uint16_t port;
295
296         while (offset < len) {
297                 ipptr = data + offset;
298                 if (*ipptr) {
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));
304                 }
305                 offset += sizeof(ip_set_ip_t);
306         }
307 }
308
309 static char buffer[22];
310
311 static char * unpack_ipport_tostring(struct set *set, ip_set_ip_t bip, unsigned options)
312 {
313         struct ip_set_ipporthash *mysetdata =
314             (struct ip_set_ipporthash *) set->settype->header;
315         ip_set_ip_t ip, port;
316         
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));
321                 
322         return buffer;
323 }
324
325 void usage(void)
326 {
327         printf
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"
332              "-A set IP:port\n"
333              "-D set IP:port\n"
334              "-T set IP:port\n");
335 }
336
337 static struct settype settype_ipporthash = {
338         .typename = SETTYPE_NAME,
339         .protocol_version = IP_SET_PROTOCOL_VERSION,
340
341         /* Create */
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,
347
348         /* Add/del/test */
349         .adt_size = sizeof(struct ip_set_req_ipporthash),
350         .adt_parser = &adt_parser,
351
352         /* Printing */
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,
359         .saveips = &saveips,
360         
361         /* Bindings */
362         .bindip_tostring = &unpack_ipport_tostring,
363         .bindip_parse = &parse_ip,
364         
365         .usage = &usage,
366 };
367
368 void _init(void)
369 {
370         settype_register(&settype_ipporthash);
371
372 }