ipset-2.2.8-20051203
[iptables.git] / ipset / ipset_portmap.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
19 #include <stdio.h>
20 #include <string.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <asm/bitops.h>
25
26 #include <linux/netfilter_ipv4/ip_set_portmap.h>
27 #include "ipset.h"
28
29
30 #define BUFLEN 30;
31
32 #define OPT_CREATE_FROM    0x01U
33 #define OPT_CREATE_TO      0x02U
34
35 #define OPT_ADDDEL_PORT      0x01U
36
37 /* Initialize the create. */
38 void create_init(void *data)
39 {
40         DP("create INIT");
41         /* Nothing */
42 }
43
44 /* Function which parses command options; returns true if it ate an option */
45 int create_parse(int c, char *argv[], void *data, unsigned *flags)
46 {
47         struct ip_set_req_portmap_create *mydata =
48             (struct ip_set_req_portmap_create *) data;
49
50         DP("create_parse");
51
52         switch (c) {
53         case '1':
54                 parse_port(optarg, &mydata->from);
55
56                 *flags |= OPT_CREATE_FROM;
57
58                 DP("--from %x (%s)", mydata->from,
59                    port_tostring(mydata->from, 0));
60
61                 break;
62
63         case '2':
64                 parse_port(optarg, &mydata->to);
65
66                 *flags |= OPT_CREATE_TO;
67
68                 DP("--to %x (%s)", mydata->to,
69                    port_tostring(mydata->to, 0));
70
71                 break;
72
73         default:
74                 return 0;
75         }
76
77         return 1;
78 }
79
80 /* Final check; exit if not ok. */
81 void create_final(void *data, unsigned int flags)
82 {
83         struct ip_set_req_portmap_create *mydata =
84             (struct ip_set_req_portmap_create *) data;
85
86         if (flags == 0) {
87                 exit_error(PARAMETER_PROBLEM,
88                            "Need to specify --from and --to\n");
89         } else {
90                 /* --from --to */
91                 if ((flags & OPT_CREATE_FROM) == 0
92                     || (flags & OPT_CREATE_TO) == 0)
93                         exit_error(PARAMETER_PROBLEM,
94                                    "Need to specify both --from and --to\n");
95         }
96
97         DP("from : %x to: %x  diff: %d", mydata->from, mydata->to,
98            mydata->to - mydata->from);
99
100         if (mydata->from > mydata->to)
101                 exit_error(PARAMETER_PROBLEM,
102                            "From can't be lower than to.\n");
103
104         if (mydata->to - mydata->from > MAX_RANGE)
105                 exit_error(PARAMETER_PROBLEM,
106                            "Range too large. Max is %d ports in range\n",
107                            MAX_RANGE+1);
108 }
109
110 /* Create commandline options */
111 static struct option create_opts[] = {
112         {"from", 1, 0, '1'},
113         {"to", 1, 0, '2'},
114         {0}
115 };
116
117 /* Add, del, test parser */
118 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
119 {
120         struct ip_set_req_portmap *mydata =
121             (struct ip_set_req_portmap *) data;
122
123         parse_port(optarg, &mydata->port);
124         DP("%s", port_tostring(mydata->port, 0));
125
126         return 1;       
127 }
128
129 /*
130  * Print and save
131  */
132
133 void initheader(struct set *set, const void *data)
134 {
135         struct ip_set_req_portmap_create *header =
136             (struct ip_set_req_portmap_create *) data;
137         struct ip_set_portmap *map =
138                 (struct ip_set_portmap *) set->settype->header;
139
140         memset(map, 0, sizeof(struct ip_set_portmap));
141         map->first_port = header->from;
142         map->last_port = header->to;
143 }
144
145 void printheader(struct set *set, unsigned options)
146 {
147         struct ip_set_portmap *mysetdata =
148             (struct ip_set_portmap *) set->settype->header;
149
150         printf(" from: %s", port_tostring(mysetdata->first_port, options));
151         printf(" to: %s\n", port_tostring(mysetdata->last_port, options));
152 }
153
154 void printports_sorted(struct set *set, void *data, size_t len, unsigned options)
155 {
156         struct ip_set_portmap *mysetdata =
157             (struct ip_set_portmap *) set->settype->header;
158         u_int32_t addr = mysetdata->first_port;
159
160         DP("%u -- %u", mysetdata->first_port, mysetdata->last_port);
161         while (addr <= mysetdata->last_port) {
162                 if (test_bit(addr - mysetdata->first_port, data))
163                         printf("%s\n", port_tostring(addr, options));
164                 addr++;
165         }
166 }
167
168 char * binding_port_tostring(struct set *set, ip_set_ip_t ip, unsigned options)
169 {
170         return port_tostring(ip, options);
171 }
172
173 void saveheader(struct set *set, unsigned options)
174 {
175         struct ip_set_portmap *mysetdata =
176             (struct ip_set_portmap *) set->settype->header;
177
178         printf("-N %s %s --from %s", 
179                set->name,
180                set->settype->typename,
181                port_tostring(mysetdata->first_port, options));
182         printf(" --to %s\n", 
183                port_tostring(mysetdata->last_port, options));
184 }
185
186 void saveports(struct set *set, void *data, size_t len, unsigned options)
187 {
188         struct ip_set_portmap *mysetdata =
189             (struct ip_set_portmap *) set->settype->header;
190         u_int32_t addr = mysetdata->first_port;
191
192         while (addr <= mysetdata->last_port) {
193                 if (test_bit(addr - mysetdata->first_port, data))
194                         printf("-A %s %s\n",
195                                set->name,
196                                port_tostring(addr, options));
197                 addr++;
198         }
199 }
200
201 void usage(void)
202 {
203         printf
204             ("-N set portmap --from PORT --to PORT\n"
205              "-A set PORT\n"
206              "-D set PORT\n"
207              "-T set PORT\n");
208 }
209
210 static struct settype settype_portmap = {
211         .typename = SETTYPE_NAME,
212         .protocol_version = IP_SET_PROTOCOL_VERSION,
213
214         /* Create */
215         .create_size = sizeof(struct ip_set_req_portmap_create),
216         .create_init = &create_init,
217         .create_parse = &create_parse,
218         .create_final = &create_final,
219         .create_opts = create_opts,
220
221         /* Add/del/test */
222         .adt_size = sizeof(struct ip_set_req_portmap),
223         .adt_parser = &adt_parser,
224
225         /* Printing */
226         .header_size = sizeof(struct ip_set_portmap),
227         .initheader = &initheader,
228         .printheader = &printheader,
229         .printips = &printports_sorted, /* We only have sorted version */
230         .printips_sorted = &printports_sorted,
231         .saveheader = &saveheader,
232         .saveips = &saveports,
233         
234         /* Bindings */
235         .bindip_tostring = &binding_port_tostring,
236         .bindip_parse = &parse_port,
237
238         .usage = &usage,
239 };
240
241 void _init(void)
242 {
243         settype_register(&settype_portmap);
244
245 }