ipset-2.2.8-20051203
[iptables.git] / ipset / ipset_macipmap.c
1 /* Copyright 2000, 2001, 2002 Joakim Axelsson (gozem@linux.nu)
2  *                            Patrick Schaaf (bof@bof.de)
3  *                            Martin Josefsson (gandalf@wlug.westbo.se)
4  *
5  * This program is free software; you can redistribute it and/or modify   
6  * it under the terms of the GNU General Public License as published by   
7  * the Free Software Foundation; either version 2 of the License, or      
8  * (at your option) any later version.                                    
9  *                                                                         
10  * This program is distributed in the hope that it will be useful,        
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of         
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
13  * GNU General Public License for more details.                           
14  *                                                                         
15  * You should have received a copy of the GNU General Public License      
16  * along with this program; if not, write to the Free Software            
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <asm/bitops.h>
28 #include <linux/if_ether.h>
29
30 #include <linux/netfilter_ipv4/ip_set_macipmap.h>
31 #include "ipset.h"
32
33 #define BUFLEN 30;
34
35 #define OPT_CREATE_FROM    0x01U
36 #define OPT_CREATE_TO      0x02U
37 #define OPT_CREATE_NETWORK 0x04U
38 #define OPT_CREATE_MATCHUNSET   0x08U
39
40 #define OPT_ADDDEL_IP      0x01U
41 #define OPT_ADDDEL_MAC     0x02U
42
43 /* Initialize the create. */
44 void create_init(void *data)
45 {
46         DP("create INIT");
47         /* Nothing */
48 }
49
50 /* Function which parses command options; returns true if it ate an option */
51 int create_parse(int c, char *argv[], void *data, unsigned *flags)
52 {
53         struct ip_set_req_macipmap_create *mydata =
54             (struct ip_set_req_macipmap_create *) data;
55
56         DP("create_parse");
57
58         switch (c) {
59         case '1':
60                 parse_ip(optarg, &mydata->from);
61
62                 *flags |= OPT_CREATE_FROM;
63
64                 DP("--from %x (%s)", mydata->from,
65                    ip_tostring_numeric(mydata->from));
66
67                 break;
68
69         case '2':
70                 parse_ip(optarg, &mydata->to);
71
72                 *flags |= OPT_CREATE_TO;
73
74                 DP("--to %x (%s)", mydata->to,
75                    ip_tostring_numeric(mydata->to));
76
77                 break;
78
79         case '3':
80                 parse_ipandmask(optarg, &mydata->from, &mydata->to);
81
82                 /* Make to the last of from + mask */
83                 mydata->to = mydata->from | (~mydata->to);
84
85                 *flags |= OPT_CREATE_NETWORK;
86
87                 DP("--network from %x (%s)", 
88                    mydata->from, ip_tostring_numeric(mydata->from));
89                 DP("--network to %x (%s)", 
90                    mydata->to, ip_tostring_numeric(mydata->to));
91
92                 break;
93
94         case '4':
95                 mydata->flags |= IPSET_MACIP_MATCHUNSET;
96
97                 *flags |= OPT_CREATE_MATCHUNSET;
98
99                 DP("--matchunset");
100
101                 break;
102
103         default:
104                 return 0;
105         }
106
107         return 1;
108 }
109
110 /* Final check; exit if not ok. */
111 void create_final(void *data, unsigned int flags)
112 {
113         struct ip_set_req_macipmap_create *mydata =
114             (struct ip_set_req_macipmap_create *) data;
115
116         if (flags == 0)
117                 exit_error(PARAMETER_PROBLEM,
118                            "Need to specify --from and --to, or --network\n");
119
120         if (flags & OPT_CREATE_NETWORK) {
121                 /* --network */
122                 if ((flags & OPT_CREATE_FROM) || (flags & OPT_CREATE_TO))
123                         exit_error(PARAMETER_PROBLEM,
124                                    "Can't specify --from or --to with --network\n");
125         } else {
126                 /* --from --to */
127                 if ((flags & OPT_CREATE_FROM) == 0
128                     || (flags & OPT_CREATE_TO) == 0)
129                         exit_error(PARAMETER_PROBLEM,
130                                    "Need to specify both --from and --to\n");
131         }
132
133
134         DP("from : %x to: %x  diff: %d  match unset: %d", mydata->from,
135            mydata->to, mydata->to - mydata->from,
136            flags & OPT_CREATE_MATCHUNSET);
137
138         if (mydata->from > mydata->to)
139                 exit_error(PARAMETER_PROBLEM,
140                            "From can't be lower than to.\n");
141
142         if (mydata->to - mydata->from > MAX_RANGE)
143                 exit_error(PARAMETER_PROBLEM,
144                            "Range too large. Max is %d IPs in range\n",
145                            MAX_RANGE+1);
146 }
147
148 /* Create commandline options */
149 static struct option create_opts[] = {
150         {"from", 1, 0, '1'},
151         {"to", 1, 0, '2'},
152         {"network", 1, 0, '3'},
153         {"matchunset", 0, 0, '4'},
154         {0}
155 };
156
157 static void parse_mac(const char *mac, unsigned char *ethernet)
158 {
159         unsigned int i = 0;
160
161         if (strlen(mac) != ETH_ALEN * 3 - 1)
162                 exit_error(PARAMETER_PROBLEM, "Bad mac address `%s'", mac);
163
164         for (i = 0; i < ETH_ALEN; i++) {
165                 long number;
166                 char *end;
167
168                 number = strtol(mac + i * 3, &end, 16);
169
170                 if (end == mac + i * 3 + 2 && number >= 0 && number <= 255)
171                         ethernet[i] = number;
172                 else
173                         exit_error(PARAMETER_PROBLEM,
174                                    "Bad mac address `%s'", mac);
175         }
176 }
177
178 /* Add, del, test parser */
179 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
180 {
181         struct ip_set_req_macipmap *mydata =
182             (struct ip_set_req_macipmap *) data;
183         char *saved = ipset_strdup(optarg);
184         char *ptr, *tmp = saved;
185
186         DP("macipmap: %p %p", optarg, data);
187
188         ptr = strsep(&tmp, "%");
189         parse_ip(ptr, &mydata->ip);
190
191         if (tmp)
192                 parse_mac(tmp, mydata->ethernet);
193         else
194                 memset(mydata->ethernet, 0, ETH_ALEN);  
195
196         free(saved);
197         return 1;       
198 }
199
200 /*
201  * Print and save
202  */
203
204 void initheader(struct set *set, const void *data)
205 {
206         struct ip_set_req_macipmap_create *header =
207             (struct ip_set_req_macipmap_create *) data;
208         struct ip_set_macipmap *map =
209                 (struct ip_set_macipmap *) set->settype->header;
210
211         memset(map, 0, sizeof(struct ip_set_macipmap));
212         map->first_ip = header->from;
213         map->last_ip = header->to;
214         map->flags = header->flags;
215 }
216
217 void printheader(struct set *set, unsigned options)
218 {
219         struct ip_set_macipmap *mysetdata =
220             (struct ip_set_macipmap *) set->settype->header;
221
222         printf(" from: %s", ip_tostring(mysetdata->first_ip, options));
223         printf(" to: %s", ip_tostring(mysetdata->last_ip, options));
224
225         if (mysetdata->flags & IPSET_MACIP_MATCHUNSET)
226                 printf(" matchunset");
227         printf("\n");
228 }
229
230 static void print_mac(unsigned char macaddress[ETH_ALEN])
231 {
232         unsigned int i;
233
234         printf("%02X", macaddress[0]);
235         for (i = 1; i < ETH_ALEN; i++)
236                 printf(":%02X", macaddress[i]);
237 }
238
239 void printips_sorted(struct set *set, void *data, size_t len, unsigned options)
240 {
241         struct ip_set_macipmap *mysetdata =
242             (struct ip_set_macipmap *) set->settype->header;
243         struct ip_set_macip *table =
244             (struct ip_set_macip *) data;
245         u_int32_t addr = mysetdata->first_ip;
246
247         while (addr <= mysetdata->last_ip) {
248                 if (test_bit(IPSET_MACIP_ISSET,
249                              (void *)&table[addr - mysetdata->first_ip].flags)) {
250                         printf("%s%%", ip_tostring(addr, options));
251                         print_mac(table[addr - mysetdata->first_ip].
252                                   ethernet);
253                         printf("\n");
254                 }
255                 addr++;
256         }
257 }
258
259 void saveheader(struct set *set, unsigned options)
260 {
261         struct ip_set_macipmap *mysetdata =
262             (struct ip_set_macipmap *) set->settype->header;
263
264         printf("-N %s %s --from %s",
265                set->name, set->settype->typename,
266                ip_tostring(mysetdata->first_ip, options));
267         printf(" --to %s", ip_tostring(mysetdata->last_ip, options));
268
269         if (mysetdata->flags & IPSET_MACIP_MATCHUNSET)
270                 printf(" --matchunset");
271         printf("\n");
272 }
273
274 void saveips(struct set *set, void *data, size_t len, unsigned options)
275 {
276         struct ip_set_macipmap *mysetdata =
277             (struct ip_set_macipmap *) set->settype->header;
278         struct ip_set_macip *table =
279             (struct ip_set_macip *) data;
280         u_int32_t addr = mysetdata->first_ip;
281
282         while (addr <= mysetdata->last_ip) {
283                 if (test_bit(IPSET_MACIP_ISSET,
284                              (void *)&table[addr - mysetdata->first_ip].flags)) {
285                         printf("-A %s %s%%",
286                                set->name, ip_tostring(addr, options));
287                         print_mac(table[addr - mysetdata->first_ip].
288                                   ethernet);
289                         printf("\n");
290                 }
291                 addr++;
292         }
293 }
294
295 void usage(void)
296 {
297         printf
298             ("-N set macipmap --from IP --to IP [--matchunset]\n"
299              "-N set macipmap --network IP/mask [--matchunset]\n"
300              "-A set IP%%MAC\n"
301              "-D set IP[%%MAC]\n"
302              "-T set IP[%%MAC]\n");
303 }
304
305 static struct settype settype_macipmap = {
306         .typename = SETTYPE_NAME,
307         .protocol_version = IP_SET_PROTOCOL_VERSION,
308
309         /* Create */
310         .create_size = sizeof(struct ip_set_req_macipmap_create),
311         .create_init = &create_init,
312         .create_parse = &create_parse,
313         .create_final = &create_final,
314         .create_opts = create_opts,
315
316         /* Add/del/test */
317         .adt_size = sizeof(struct ip_set_req_macipmap),
318         .adt_parser = &adt_parser,
319
320         /* Printing */
321         .header_size = sizeof(struct ip_set_macipmap),
322         .initheader = &initheader,
323         .printheader = &printheader,
324         .printips = &printips_sorted,   /* We only have sorted version */
325         .printips_sorted = &printips_sorted,
326         .saveheader = &saveheader,
327         .saveips = &saveips,
328
329         /* Bindings */
330         .bindip_tostring = &binding_ip_tostring,
331         .bindip_parse = &parse_ip,
332
333         .usage = &usage,
334 };
335
336 void _init(void)
337 {
338         settype_register(&settype_macipmap);
339
340 }