rework runtime state tracking
[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 struct fw3_ipset *
188 fw3_alloc_ipset(void)
189 {
190         struct fw3_ipset *ipset;
191
192         ipset = malloc(sizeof(*ipset));
193
194         if (!ipset)
195                 return NULL;
196
197         memset(ipset, 0, sizeof(*ipset));
198
199         INIT_LIST_HEAD(&ipset->datatypes);
200         INIT_LIST_HEAD(&ipset->iprange);
201
202         return ipset;
203 }
204
205 void
206 fw3_load_ipsets(struct fw3_state *state, struct uci_package *p)
207 {
208         struct uci_section *s;
209         struct uci_element *e;
210         struct fw3_ipset *ipset;
211
212         INIT_LIST_HEAD(&state->ipsets);
213
214         if (state->disable_ipsets)
215                 return;
216
217         uci_foreach_element(&p->sections, e)
218         {
219                 s = uci_to_section(e);
220
221                 if (strcmp(s->type, "ipset"))
222                         continue;
223
224                 ipset = fw3_alloc_ipset();
225
226                 if (!ipset)
227                         continue;
228
229                 fw3_parse_options(ipset, ipset_opts, ARRAY_SIZE(ipset_opts), s);
230
231                 if (!ipset->name || !*ipset->name)
232                 {
233                         warn_elem(e, "must have a name assigned");
234                 }
235                 //else if (fw3_lookup_ipset(state, ipset->name) != NULL)
236                 //{
237                 //      warn_elem(e, "has duplicated set name '%s'", ipset->name);
238                 //}
239                 else if (list_empty(&ipset->datatypes))
240                 {
241                         warn_elem(e, "has no datatypes assigned");
242                 }
243                 else if (check_types(e, ipset))
244                 {
245                         list_add_tail(&ipset->list, &state->ipsets);
246                         continue;
247                 }
248
249                 fw3_free_ipset(ipset);
250         }
251 }
252
253
254 static void
255 create_ipset(struct fw3_ipset *ipset, struct fw3_state *state)
256 {
257         bool first = true;
258         char s[INET6_ADDRSTRLEN];
259
260         struct fw3_ipset_datatype *type;
261         struct fw3_address *a1, *a2;
262
263         const char *methods[] = {
264                 "(bug)",
265                 "bitmap",
266                 "hash",
267                 "list",
268         };
269
270         const char *types[] = {
271                 "(bug)",
272                 "ip",
273                 "port",
274                 "mac",
275                 "net",
276                 "set",
277         };
278
279         if (ipset->external && *ipset->external)
280                 return;
281
282         info("Creating ipset %s", ipset->name);
283
284         first = true;
285         fw3_pr("create %s %s", ipset->name, methods[ipset->method]);
286
287         list_for_each_entry(type, &ipset->datatypes, list)
288         {
289                 fw3_pr("%c%s", first ? ':' : ',', types[type->type]);
290                 first = false;
291         }
292
293         if (!list_empty(&ipset->iprange))
294         {
295                 a1 = list_first_entry(&ipset->iprange, struct fw3_address, list);
296                 a2 = list_last_entry(&ipset->iprange, struct fw3_address, list);
297
298                 if (a1 == a2)
299                 {
300                         inet_ntop(a1->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
301                                   &a1->address.v6, s, sizeof(s));
302
303                         fw3_pr(" range %s/%u", s, a1->mask);
304                 }
305                 else if (a1->family == a2->family &&
306                          fw3_is_family(ipset, a1->family) &&
307                          fw3_is_family(ipset, a2->family))
308                 {
309                         inet_ntop(a1->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
310                                   &a1->address.v6, s, sizeof(s));
311
312                         fw3_pr(" range %s", s);
313
314                         inet_ntop(a2->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
315                                   &a2->address.v6, s, sizeof(s));
316
317                         fw3_pr("-%s", s);
318                 }
319         }
320         else if (ipset->portrange.set)
321         {
322                 fw3_pr(" range %u-%u",
323                        ipset->portrange.port_min, ipset->portrange.port_max);
324         }
325
326         if (ipset->family != FW3_FAMILY_ANY)
327                 fw3_pr(" family inet%s", (ipset->family == FW3_FAMILY_V4) ? "" : "6");
328
329         if (ipset->timeout > 0)
330                 fw3_pr(" timeout %u", ipset->timeout);
331
332         if (ipset->maxelem > 0)
333                 fw3_pr(" maxelem %u", ipset->maxelem);
334
335         if (ipset->netmask > 0)
336                 fw3_pr(" netmask %u", ipset->netmask);
337
338         if (ipset->hashsize > 0)
339                 fw3_pr(" hashsize %u", ipset->hashsize);
340
341         fw3_pr("\n");
342
343         fw3_set_running(ipset, &state->running_ipsets);
344 }
345
346 void
347 fw3_create_ipsets(struct fw3_state *state)
348 {
349         struct fw3_ipset *ipset;
350
351         if (state->disable_ipsets)
352                 return;
353
354         list_for_each_entry(ipset, &state->ipsets, list)
355                 if (!fw3_lookup_ipset(state, ipset->name, true))
356                         create_ipset(ipset, state);
357
358         fw3_pr("quit\n");
359 }
360
361 void
362 fw3_destroy_ipsets(struct fw3_state *state)
363 {
364         struct fw3_ipset *s, *tmp;
365         int mask = (1 << FW3_FAMILY_V4) | (1 << FW3_FAMILY_V6);
366
367         list_for_each_entry_safe(s, tmp, &state->running_ipsets, running_list)
368         {
369                 if (!hasbit(state->defaults.flags, FW3_FAMILY_V4))
370                         delbit(s->flags, FW3_FAMILY_V4);
371
372                 if (!hasbit(state->defaults.flags, FW3_FAMILY_V6))
373                         delbit(s->flags, FW3_FAMILY_V6);
374
375                 if (!(s->flags & mask))
376                 {
377                         info("Deleting ipset %s", s->name);
378
379                         fw3_pr("flush %s\n", s->name);
380                         fw3_pr("destroy %s\n", s->name);
381
382                         fw3_set_running(s, NULL);
383                 }
384         }
385 }
386
387 void
388 fw3_free_ipset(struct fw3_ipset *ipset)
389 {
390         fw3_free_list(&ipset->datatypes);
391         fw3_free_list(&ipset->iprange);
392
393         free(ipset);
394 }
395
396 struct fw3_ipset *
397 fw3_lookup_ipset(struct fw3_state *state, const char *name, bool running)
398 {
399         struct fw3_ipset *s;
400
401         if (list_empty(&state->ipsets))
402                 return NULL;
403
404         list_for_each_entry(s, &state->ipsets, list)
405         {
406                 if (strcmp(s->name, name))
407                         continue;
408
409                 if (!running || s->running_list.next)
410                         return s;
411
412                 break;
413         }
414
415         return NULL;
416 }