destroy ipsets on explicit stop and flush, but not on restart
[project/firewall3.git] / ipsets.c
1 /*
2  * firewall3 - 3rd OpenWrt UCI firewall implementation
3  *
4  *   Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #include "ipsets.h"
20
21
22 static struct fw3_option ipset_opts[] = {
23         FW3_OPT("name",          string,         ipset,     name),
24         FW3_OPT("family",        family,         ipset,     family),
25
26         FW3_OPT("storage",       ipset_method,   ipset,     method),
27         FW3_LIST("match",        ipset_datatype, ipset,     datatypes),
28
29         FW3_LIST("iprange",      address,        ipset,     iprange),
30         FW3_OPT("portrange",     port,           ipset,     portrange),
31
32         FW3_OPT("netmask",       int,            ipset,     netmask),
33         FW3_OPT("maxelem",       int,            ipset,     maxelem),
34         FW3_OPT("hashsize",      int,            ipset,     hashsize),
35         FW3_OPT("timeout",       int,            ipset,     timeout),
36
37         FW3_OPT("external",      string,         ipset,     external),
38 };
39
40 #define T(m, t1, t2, t3, r, o) \
41         { FW3_IPSET_METHOD_##m, \
42           FW3_IPSET_TYPE_##t1 | (FW3_IPSET_TYPE_##t2 << 8) | (FW3_IPSET_TYPE_##t3 << 16), \
43           r, o }
44
45 static struct fw3_ipset_settype ipset_types[] = {
46         T(BITMAP, IP,   UNSPEC, UNSPEC, FW3_IPSET_OPT_IPRANGE,
47           FW3_IPSET_OPT_NETMASK),
48         T(BITMAP, IP,   MAC,    UNSPEC, FW3_IPSET_OPT_IPRANGE, 0),
49         T(BITMAP, PORT, UNSPEC, UNSPEC, FW3_IPSET_OPT_PORTRANGE, 0),
50
51         T(HASH,   IP,   UNSPEC, UNSPEC, 0,
52           FW3_IPSET_OPT_FAMILY | FW3_IPSET_OPT_HASHSIZE | FW3_IPSET_OPT_MAXELEM |
53           FW3_IPSET_OPT_NETMASK),
54         T(HASH,   NET,  UNSPEC, UNSPEC, 0,
55           FW3_IPSET_OPT_FAMILY | FW3_IPSET_OPT_HASHSIZE | FW3_IPSET_OPT_MAXELEM),
56         T(HASH,   IP,   PORT,   UNSPEC, 0,
57           FW3_IPSET_OPT_FAMILY | FW3_IPSET_OPT_HASHSIZE | FW3_IPSET_OPT_MAXELEM),
58         T(HASH,   NET,  PORT,   UNSPEC, 0,
59           FW3_IPSET_OPT_FAMILY | FW3_IPSET_OPT_HASHSIZE | FW3_IPSET_OPT_MAXELEM),
60         T(HASH,   IP,   PORT,   IP,     0,
61           FW3_IPSET_OPT_FAMILY | FW3_IPSET_OPT_HASHSIZE | FW3_IPSET_OPT_MAXELEM),
62         T(HASH,   IP,   PORT,   NET,    0,
63           FW3_IPSET_OPT_FAMILY | FW3_IPSET_OPT_HASHSIZE | FW3_IPSET_OPT_MAXELEM),
64
65         T(LIST,   SET,  UNSPEC, UNSPEC, 0, FW3_IPSET_OPT_MAXELEM),
66 };
67
68
69 static bool
70 check_types(struct uci_element *e, struct fw3_ipset *ipset)
71 {
72         int i = 0;
73         uint32_t typelist = 0;
74         struct fw3_ipset_datatype *type;
75
76         const char *methods[] = {
77                 "(bug)",
78                 "bitmap",
79                 "hash",
80                 "list",
81         };
82
83         typelist = 0;
84
85         list_for_each_entry(type, &ipset->datatypes, list)
86         {
87                 if (i >= 3)
88                 {
89                         warn_elem(e, "must not have more than 3 datatypes assigned");
90                         return false;
91                 }
92
93                 typelist |= (type->type << (i++ * 8));
94         }
95
96         /* find a suitable storage method if none specified */
97         if (ipset->method == FW3_IPSET_METHOD_UNSPEC)
98         {
99                 for (i = 0; i < ARRAY_SIZE(ipset_types); i++)
100                 {
101                         if (ipset_types[i].types == typelist)
102                         {
103                                 ipset->method = ipset_types[i].method;
104
105                                 warn_elem(e, "defines no storage method, assuming '%s'",
106                                           methods[ipset->method]);
107
108                                 break;
109                         }
110                 }
111         }
112
113         //typelist |= ipset->method;
114
115         for (i = 0; i < ARRAY_SIZE(ipset_types); i++)
116         {
117                 if (ipset_types[i].method == ipset->method &&
118                     ipset_types[i].types == typelist)
119                 {
120                         if (!ipset->external || !*ipset->external)
121                         {
122                                 if ((ipset_types[i].required & FW3_IPSET_OPT_IPRANGE) &&
123                                         list_empty(&ipset->iprange))
124                                 {
125                                         warn_elem(e, "requires an ip range");
126                                         return false;
127                                 }
128
129                                 if ((ipset_types[i].required & FW3_IPSET_OPT_PORTRANGE) &&
130                                     !ipset->portrange.set)
131                                 {
132                                         warn_elem(e, "requires a port range");
133                                         return false;
134                                 }
135
136                                 if (!(ipset_types[i].required & FW3_IPSET_OPT_IPRANGE) &&
137                                     !list_empty(&ipset->iprange))
138                                 {
139                                         warn_elem(e, "iprange ignored");
140                                         fw3_free_list(&ipset->iprange);
141                                 }
142
143                                 if (!(ipset_types[i].required & FW3_IPSET_OPT_PORTRANGE) &&
144                                     ipset->portrange.set)
145                                 {
146                                         warn_elem(e, "portrange ignored");
147                                         memset(&ipset->portrange, 0, sizeof(ipset->portrange));
148                                 }
149
150                                 if (!(ipset_types[i].optional & FW3_IPSET_OPT_NETMASK) &&
151                                     ipset->netmask > 0)
152                                 {
153                                         warn_elem(e, "netmask ignored");
154                                         ipset->netmask = 0;
155                                 }
156
157                                 if (!(ipset_types[i].optional & FW3_IPSET_OPT_HASHSIZE) &&
158                                     ipset->hashsize > 0)
159                                 {
160                                         warn_elem(e, "hashsize ignored");
161                                         ipset->hashsize = 0;
162                                 }
163
164                                 if (!(ipset_types[i].optional & FW3_IPSET_OPT_MAXELEM) &&
165                                     ipset->maxelem > 0)
166                                 {
167                                         warn_elem(e, "maxelem ignored");
168                                         ipset->maxelem = 0;
169                                 }
170
171                                 if (!(ipset_types[i].optional & FW3_IPSET_OPT_FAMILY) &&
172                                     ipset->family != FW3_FAMILY_ANY)
173                                 {
174                                         warn_elem(e, "family ignored");
175                                         ipset->family = FW3_FAMILY_ANY;
176                                 }
177                         }
178
179                         return true;
180                 }
181         }
182
183         warn_elem(e, "has an invalid combination of storage method and matches");
184         return false;
185 }
186
187 void
188 fw3_load_ipsets(struct fw3_state *state, struct uci_package *p)
189 {
190         struct uci_section *s;
191         struct uci_element *e;
192         struct fw3_ipset *ipset;
193
194         INIT_LIST_HEAD(&state->ipsets);
195
196         if (state->disable_ipsets)
197                 return;
198
199         uci_foreach_element(&p->sections, e)
200         {
201                 s = uci_to_section(e);
202
203                 if (strcmp(s->type, "ipset"))
204                         continue;
205
206                 ipset = malloc(sizeof(*ipset));
207
208                 if (!ipset)
209                         continue;
210
211                 memset(ipset, 0, sizeof(*ipset));
212
213                 INIT_LIST_HEAD(&ipset->datatypes);
214                 INIT_LIST_HEAD(&ipset->iprange);
215
216                 fw3_parse_options(ipset, ipset_opts, ARRAY_SIZE(ipset_opts), s);
217
218                 if (!ipset->name || !*ipset->name)
219                 {
220                         warn_elem(e, "must have a name assigned");
221                 }
222                 //else if (fw3_lookup_ipset(state, ipset->name) != NULL)
223                 //{
224                 //      warn_elem(e, "has duplicated set name '%s'", ipset->name);
225                 //}
226                 else if (list_empty(&ipset->datatypes))
227                 {
228                         warn_elem(e, "has no datatypes assigned");
229                 }
230                 else if (check_types(e, ipset))
231                 {
232                         list_add_tail(&ipset->list, &state->ipsets);
233                         continue;
234                 }
235
236                 fw3_free_ipset(ipset);
237         }
238 }
239
240
241 static void
242 create_ipset(struct fw3_ipset *ipset)
243 {
244         bool first = true;
245         char s[INET6_ADDRSTRLEN];
246
247         struct fw3_ipset_datatype *type;
248         struct fw3_address *a1, *a2;
249
250         const char *methods[] = {
251                 "(bug)",
252                 "bitmap",
253                 "hash",
254                 "list",
255         };
256
257         const char *types[] = {
258                 "(bug)",
259                 "ip",
260                 "port",
261                 "mac",
262                 "net",
263                 "set",
264         };
265
266         const char *families[] = {
267                 "(bug)",
268                 "inet",
269                 "inet6",
270         };
271
272         if (ipset->external && *ipset->external)
273                 return;
274
275         info(" * %s", ipset->name);
276
277         first = true;
278         fw3_pr("create %s %s", ipset->name, methods[ipset->method]);
279
280         list_for_each_entry(type, &ipset->datatypes, list)
281         {
282                 fw3_pr("%c%s", first ? ':' : ',', types[type->type]);
283                 first = false;
284         }
285
286         if (!list_empty(&ipset->iprange))
287         {
288                 a1 = list_first_entry(&ipset->iprange, struct fw3_address, list);
289                 a2 = list_last_entry(&ipset->iprange, struct fw3_address, list);
290
291                 if (a1 == a2)
292                 {
293                         inet_ntop(a1->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
294                                   &a1->address.v6, s, sizeof(s));
295
296                         fw3_pr(" range %s/%u", s, a1->mask);
297                 }
298                 else if (a1->family == a2->family &&
299                          fw3_is_family(ipset, a1->family) &&
300                          fw3_is_family(ipset, a2->family))
301                 {
302                         inet_ntop(a1->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
303                                   &a1->address.v6, s, sizeof(s));
304
305                         fw3_pr(" range %s", s);
306
307                         inet_ntop(a2->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
308                                   &a2->address.v6, s, sizeof(s));
309
310                         fw3_pr("-%s", s);
311                 }
312         }
313         else if (ipset->portrange.set)
314         {
315                 fw3_pr(" range %u-%u",
316                        ipset->portrange.port_min, ipset->portrange.port_max);
317         }
318
319         if (ipset->family != FW3_FAMILY_ANY)
320                 fw3_pr(" family %s", families[ipset->family]);
321
322         if (ipset->timeout > 0)
323                 fw3_pr(" timeout %u", ipset->timeout);
324
325         if (ipset->maxelem > 0)
326                 fw3_pr(" maxelem %u", ipset->maxelem);
327
328         if (ipset->netmask > 0)
329                 fw3_pr(" netmask %u", ipset->netmask);
330
331         if (ipset->hashsize > 0)
332                 fw3_pr(" hashsize %u", ipset->hashsize);
333
334         fw3_pr("\n");
335 }
336
337 void
338 fw3_create_ipsets(struct fw3_state *state)
339 {
340         struct fw3_ipset *ipset;
341
342         if (state->disable_ipsets)
343                 return;
344
345         info("Initializing ipsets ...");
346
347         list_for_each_entry(ipset, &state->ipsets, list)
348                 create_ipset(ipset);
349
350         fw3_pr("quit\n");
351 }
352
353 void
354 fw3_destroy_ipsets(struct fw3_state *state)
355 {
356         FILE *sf;
357
358         char *p;
359         char line[128];
360
361         sf = fopen(FW3_STATEFILE, "r");
362
363         if (!sf)
364                 return;
365
366         info("Destroying ipsets ...");
367
368         while (fgets(line, sizeof(line), sf))
369         {
370                 if (strncmp(line, "ipset ", 6))
371                         continue;
372
373                 p = strtok(line+6, " \t\n");
374
375                 if (!p || !strlen(p))
376                         continue;
377
378                 info(" * %s", p);
379
380                 fw3_pr("flush %s\n", p);
381                 fw3_pr("destroy %s\n", p);
382         }
383
384         fw3_pr("quit\n");
385
386         fclose(sf);
387 }
388
389 void
390 fw3_free_ipset(struct fw3_ipset *ipset)
391 {
392         fw3_free_list(&ipset->datatypes);
393         fw3_free_list(&ipset->iprange);
394
395         free(ipset);
396 }
397
398 struct fw3_ipset *
399 fw3_lookup_ipset(struct fw3_state *state, const char *name)
400 {
401         struct fw3_ipset *ipset;
402
403         if (list_empty(&state->ipsets))
404                 return NULL;
405
406         list_for_each_entry(ipset, &state->ipsets, list)
407                 if (!strcmp(ipset->name, name))
408                         return ipset;
409
410         return NULL;
411 }