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