ipset-2.2.8-20051203
[iptables.git] / ipset / ipset_nethash.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/bitops.h>
29 #include <asm/types.h>
30
31 #include <linux/netfilter_ipv4/ip_set_nethash.h>
32 #include <linux/netfilter_ipv4/ip_set_jhash.h>
33
34 #include "ipset.h"
35
36 #define BUFLEN 30;
37
38 #define OPT_CREATE_HASHSIZE     0x01U
39 #define OPT_CREATE_PROBES       0x02U
40 #define OPT_CREATE_RESIZE       0x04U
41
42 /* Initialize the create. */
43 void create_init(void *data)
44 {
45         struct ip_set_req_nethash_create *mydata =
46             (struct ip_set_req_nethash_create *) data;
47
48         DP("create INIT");
49
50         /* Default create parameters */ 
51         mydata->hashsize = 1024;
52         mydata->probes = 4;
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_nethash_create *mydata =
60             (struct ip_set_req_nethash_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         default:
102                 return 0;
103         }
104
105         return 1;
106 }
107
108 /* Final check; exit if not ok. */
109 void create_final(void *data, unsigned int flags)
110 {
111 #ifdef IPSET_DEBUG
112         struct ip_set_req_nethash_create *mydata =
113             (struct ip_set_req_nethash_create *) data;
114
115         DP("hashsize %u probes %u resize %u",
116            mydata->hashsize, mydata->probes, mydata->resize);
117 #endif
118 }
119
120 /* Create commandline options */
121 static struct option create_opts[] = {
122         {"hashsize", 1, 0, '1'},
123         {"probes", 1, 0, '2'},
124         {"resize", 1, 0, '3'},
125         {0}
126 };
127
128 /* Add, del, test parser */
129 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
130 {
131         struct ip_set_req_nethash *mydata =
132             (struct ip_set_req_nethash *) data;
133         char *saved = ipset_strdup(optarg);
134         char *ptr, *tmp = saved;
135         ip_set_ip_t cidr;
136
137         ptr = strsep(&tmp, "/");
138         
139         if (tmp == NULL) {
140                 if (cmd == CMD_TEST)
141                         cidr = 32;
142                 else
143                         exit_error(PARAMETER_PROBLEM,
144                                    "Missing cidr from `%s'", optarg);
145         } else
146                 if (string_to_number(tmp, 1, 31, &cidr))
147                         exit_error(PARAMETER_PROBLEM,
148                                    "Out of range cidr `%s' specified", optarg);
149         
150         mydata->cidr = cidr;
151         parse_ip(ptr, &mydata->ip);
152         if (!mydata->ip)
153                 exit_error(PARAMETER_PROBLEM,
154                           "Zero valued IP address `%s' specified", ptr);
155         free(saved);
156
157         return mydata->ip;      
158 };
159
160 /*
161  * Print and save
162  */
163
164 void initheader(struct set *set, const void *data)
165 {
166         struct ip_set_req_nethash_create *header =
167             (struct ip_set_req_nethash_create *) data;
168         struct ip_set_nethash *map =
169                 (struct ip_set_nethash *) set->settype->header;
170
171         memset(map, 0, sizeof(struct ip_set_nethash));
172         map->hashsize = header->hashsize;
173         map->probes = header->probes;
174         map->resize = header->resize;
175 }
176
177 unsigned int
178 mask_to_bits(ip_set_ip_t mask)
179 {
180         unsigned int bits = 32;
181         ip_set_ip_t maskaddr;
182         
183         if (mask == 0xFFFFFFFF)
184                 return bits;
185         
186         maskaddr = 0xFFFFFFFE;
187         while (--bits >= 0 && maskaddr != mask)
188                 maskaddr <<= 1;
189         
190         return bits;
191 }
192         
193 void printheader(struct set *set, unsigned options)
194 {
195         struct ip_set_nethash *mysetdata =
196             (struct ip_set_nethash *) set->settype->header;
197
198         printf(" hashsize: %u", mysetdata->hashsize);
199         printf(" probes: %u", mysetdata->probes);
200         printf(" resize: %u\n", mysetdata->resize);
201 }
202
203 static char buf[20];
204
205 static char * unpack_ip_tostring(ip_set_ip_t ip, unsigned options)
206 {
207         int i, j = 3;
208         unsigned char a, b;
209
210         ip = htonl(ip); 
211         for (i = 3; i >= 0; i--)
212                 if (((unsigned char *)&ip)[i] != 0) {
213                         j = i;
214                         break;
215                 }
216                         
217         a = ((unsigned char *)&ip)[j];
218         if (a <= 128) {
219                 a = (a - 1) * 2;
220                 b = 7;
221         } else if (a <= 192) {
222                 a = (a - 129) * 4;
223                 b = 6;
224         } else if (a <= 224) {
225                 a = (a - 193) * 8;
226                 b = 5;
227         } else if (a <= 240) {
228                 a = (a - 225) * 16;
229                 b = 4;
230         } else if (a <= 248) {
231                 a = (a - 241) * 32;
232                 b = 3;
233         } else if (a <= 252) {
234                 a = (a - 249) * 64;
235                 b = 2;
236         } else if (a <= 254) {
237                 a = (a - 253) * 128;
238                 b = 1;
239         } else {
240                 a = b = 0;
241         }
242         ((unsigned char *)&ip)[j] = a;
243         b += j * 8;
244         
245         sprintf(buf, "%u.%u.%u.%u/%u",
246                 ((unsigned char *)&ip)[0],
247                 ((unsigned char *)&ip)[1],
248                 ((unsigned char *)&ip)[2],
249                 ((unsigned char *)&ip)[3],
250                 b);
251
252         DP("%s %s", ip_tostring(ntohl(ip), options), buf);
253         return buf;
254 }
255
256 void printips(struct set *set, void *data, size_t len, unsigned options)
257 {
258         size_t offset = 0;
259         ip_set_ip_t *ip;
260
261         while (offset < len) {
262                 ip = data + offset;
263                 if (*ip)
264                         printf("%s\n", unpack_ip_tostring(*ip, options));
265                 offset += sizeof(ip_set_ip_t);
266         }
267 }
268
269 void saveheader(struct set *set, unsigned options)
270 {
271         struct ip_set_nethash *mysetdata =
272             (struct ip_set_nethash *) set->settype->header;
273
274         printf("-N %s %s --hashsize %u --probes %u --resize %u\n",
275                set->name, set->settype->typename,
276                mysetdata->hashsize, mysetdata->probes, mysetdata->resize);
277 }
278
279 /* Print save for an IP */
280 void saveips(struct set *set, void *data, size_t len, unsigned options)
281 {
282         size_t offset = 0;
283         ip_set_ip_t *ip;
284
285         while (offset < len) {
286                 ip = data + offset;
287                 if (*ip)
288                         printf("-A %s %s\n", set->name, 
289                                unpack_ip_tostring(*ip, options));
290                 offset += sizeof(ip_set_ip_t);
291         }
292 }
293
294 static char * net_tostring(struct set *set, ip_set_ip_t ip, unsigned options)
295 {
296         return unpack_ip_tostring(ip, options);
297 }
298
299 static void parse_net(const char *str, ip_set_ip_t *ip)
300 {
301         char *saved = strdup(str);
302         char *ptr, *tmp = saved;
303         ip_set_ip_t cidr;
304
305         ptr = strsep(&tmp, "/");
306         
307         if (tmp == NULL)
308                 exit_error(PARAMETER_PROBLEM,
309                            "Missing cidr from `%s'", str);
310
311         if (string_to_number(tmp, 1, 31, &cidr))
312                 exit_error(PARAMETER_PROBLEM,
313                            "Out of range cidr `%s' specified", str);
314         
315         parse_ip(ptr, ip);
316         free(saved);
317         
318         *ip = pack(*ip, cidr);
319 }
320
321 void usage(void)
322 {
323         printf
324             ("-N set nethash [--hashsize hashsize] [--probes probes ]\n"
325              "               [--resize resize]\n"
326              "-A set IP/cidr\n"
327              "-D set IP/cidr\n"
328              "-T set IP/cidr\n");
329 }
330
331 static struct settype settype_nethash = {
332         .typename = SETTYPE_NAME,
333         .protocol_version = IP_SET_PROTOCOL_VERSION,
334
335         /* Create */
336         .create_size = sizeof(struct ip_set_req_nethash_create),
337         .create_init = &create_init,
338         .create_parse = &create_parse,
339         .create_final = &create_final,
340         .create_opts = create_opts,
341
342         /* Add/del/test */
343         .adt_size = sizeof(struct ip_set_req_nethash),
344         .adt_parser = &adt_parser,
345
346         /* Printing */
347         .header_size = sizeof(struct ip_set_nethash),
348         .initheader = &initheader,
349         .printheader = &printheader,
350         .printips = &printips,          /* We only have the unsorted version */
351         .printips_sorted = &printips,
352         .saveheader = &saveheader,
353         .saveips = &saveips,
354         
355         /* Bindings */
356         .bindip_tostring = &net_tostring,
357         .bindip_parse = &parse_net,
358
359         .usage = &usage,
360 };
361
362 void _init(void)
363 {
364         settype_register(&settype_nethash);
365
366 }