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