unify object freeing
[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 const struct fw3_option fw3_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 };
41
42 #define T(m, t1, t2, t3, r, o) \
43         { FW3_IPSET_METHOD_##m, \
44           FW3_IPSET_TYPE_##t1 | (FW3_IPSET_TYPE_##t2 << 8) | (FW3_IPSET_TYPE_##t3 << 16), \
45           r, o }
46
47 enum ipset_optflag {
48         OPT_IPRANGE   = (1 << 0),
49         OPT_PORTRANGE = (1 << 1),
50         OPT_NETMASK   = (1 << 2),
51         OPT_HASHSIZE  = (1 << 3),
52         OPT_MAXELEM   = (1 << 4),
53         OPT_FAMILY    = (1 << 5),
54 };
55
56 struct ipset_type {
57         enum fw3_ipset_method method;
58         uint32_t types;
59         uint8_t required;
60         uint8_t optional;
61 };
62
63 static struct ipset_type ipset_types[] = {
64         T(BITMAP, IP,   UNSPEC, UNSPEC, OPT_IPRANGE, OPT_NETMASK),
65         T(BITMAP, IP,   MAC,    UNSPEC, OPT_IPRANGE, 0),
66         T(BITMAP, PORT, UNSPEC, UNSPEC, OPT_PORTRANGE, 0),
67
68         T(HASH,   IP,   UNSPEC, UNSPEC, 0,
69           OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM | OPT_NETMASK),
70         T(HASH,   NET,  UNSPEC, UNSPEC, 0,
71           OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
72         T(HASH,   IP,   PORT,   UNSPEC, 0,
73           OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
74         T(HASH,   NET,  PORT,   UNSPEC, 0,
75           OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
76         T(HASH,   IP,   PORT,   IP,     0,
77           OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
78         T(HASH,   IP,   PORT,   NET,    0,
79           OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
80
81         T(LIST,   SET,  UNSPEC, UNSPEC, 0, OPT_MAXELEM),
82 };
83
84
85 static bool
86 check_types(struct uci_element *e, struct fw3_ipset *ipset)
87 {
88         int i = 0;
89         uint32_t typelist = 0;
90         struct fw3_ipset_datatype *type;
91
92         const char *methods[] = {
93                 "(bug)",
94                 "bitmap",
95                 "hash",
96                 "list",
97         };
98
99         typelist = 0;
100
101         list_for_each_entry(type, &ipset->datatypes, list)
102         {
103                 if (i >= 3)
104                 {
105                         warn_elem(e, "must not have more than 3 datatypes assigned");
106                         return false;
107                 }
108
109                 typelist |= (type->type << (i++ * 8));
110         }
111
112         /* find a suitable storage method if none specified */
113         if (ipset->method == FW3_IPSET_METHOD_UNSPEC)
114         {
115                 for (i = 0; i < ARRAY_SIZE(ipset_types); i++)
116                 {
117                         if (ipset_types[i].types == typelist)
118                         {
119                                 ipset->method = ipset_types[i].method;
120
121                                 warn_elem(e, "defines no storage method, assuming '%s'",
122                                           methods[ipset->method]);
123
124                                 break;
125                         }
126                 }
127         }
128
129         //typelist |= ipset->method;
130
131         for (i = 0; i < ARRAY_SIZE(ipset_types); i++)
132         {
133                 if (ipset_types[i].method == ipset->method &&
134                     ipset_types[i].types == typelist)
135                 {
136                         if (!ipset->external || !*ipset->external)
137                         {
138                                 if ((ipset_types[i].required & OPT_IPRANGE) &&
139                                         list_empty(&ipset->iprange))
140                                 {
141                                         warn_elem(e, "requires an ip range");
142                                         return false;
143                                 }
144
145                                 if ((ipset_types[i].required & OPT_PORTRANGE) &&
146                                     !ipset->portrange.set)
147                                 {
148                                         warn_elem(e, "requires a port range");
149                                         return false;
150                                 }
151
152                                 if (!(ipset_types[i].required & OPT_IPRANGE) &&
153                                     !list_empty(&ipset->iprange))
154                                 {
155                                         warn_elem(e, "iprange ignored");
156                                         fw3_free_list(&ipset->iprange);
157                                 }
158
159                                 if (!(ipset_types[i].required & OPT_PORTRANGE) &&
160                                     ipset->portrange.set)
161                                 {
162                                         warn_elem(e, "portrange ignored");
163                                         memset(&ipset->portrange, 0, sizeof(ipset->portrange));
164                                 }
165
166                                 if (!(ipset_types[i].optional & OPT_NETMASK) &&
167                                     ipset->netmask > 0)
168                                 {
169                                         warn_elem(e, "netmask ignored");
170                                         ipset->netmask = 0;
171                                 }
172
173                                 if (!(ipset_types[i].optional & OPT_HASHSIZE) &&
174                                     ipset->hashsize > 0)
175                                 {
176                                         warn_elem(e, "hashsize ignored");
177                                         ipset->hashsize = 0;
178                                 }
179
180                                 if (!(ipset_types[i].optional & OPT_MAXELEM) &&
181                                     ipset->maxelem > 0)
182                                 {
183                                         warn_elem(e, "maxelem ignored");
184                                         ipset->maxelem = 0;
185                                 }
186
187                                 if (!(ipset_types[i].optional & OPT_FAMILY) &&
188                                     ipset->family != FW3_FAMILY_ANY)
189                                 {
190                                         warn_elem(e, "family ignored");
191                                         ipset->family = FW3_FAMILY_ANY;
192                                 }
193                         }
194
195                         return true;
196                 }
197         }
198
199         warn_elem(e, "has an invalid combination of storage method and matches");
200         return false;
201 }
202
203 struct fw3_ipset *
204 fw3_alloc_ipset(void)
205 {
206         struct fw3_ipset *ipset;
207
208         ipset = malloc(sizeof(*ipset));
209
210         if (!ipset)
211                 return NULL;
212
213         memset(ipset, 0, sizeof(*ipset));
214
215         INIT_LIST_HEAD(&ipset->datatypes);
216         INIT_LIST_HEAD(&ipset->iprange);
217
218         return ipset;
219 }
220
221 void
222 fw3_load_ipsets(struct fw3_state *state, struct uci_package *p)
223 {
224         struct uci_section *s;
225         struct uci_element *e;
226         struct fw3_ipset *ipset;
227
228         INIT_LIST_HEAD(&state->ipsets);
229
230         if (state->disable_ipsets)
231                 return;
232
233         uci_foreach_element(&p->sections, e)
234         {
235                 s = uci_to_section(e);
236
237                 if (strcmp(s->type, "ipset"))
238                         continue;
239
240                 ipset = fw3_alloc_ipset();
241
242                 if (!ipset)
243                         continue;
244
245                 fw3_parse_options(ipset, fw3_ipset_opts, s);
246
247                 if (!ipset->name || !*ipset->name)
248                 {
249                         warn_elem(e, "must have a name assigned");
250                 }
251                 //else if (fw3_lookup_ipset(state, ipset->name) != NULL)
252                 //{
253                 //      warn_elem(e, "has duplicated set name '%s'", ipset->name);
254                 //}
255                 else if (list_empty(&ipset->datatypes))
256                 {
257                         warn_elem(e, "has no datatypes assigned");
258                 }
259                 else if (check_types(e, ipset))
260                 {
261                         list_add_tail(&ipset->list, &state->ipsets);
262                         continue;
263                 }
264
265                 fw3_free_ipset(ipset);
266         }
267 }
268
269
270 static void
271 create_ipset(struct fw3_ipset *ipset, struct fw3_state *state)
272 {
273         bool first = true;
274         char s[INET6_ADDRSTRLEN];
275
276         struct fw3_ipset_datatype *type;
277         struct fw3_address *a1, *a2;
278
279         const char *methods[] = {
280                 "(bug)",
281                 "bitmap",
282                 "hash",
283                 "list",
284         };
285
286         const char *types[] = {
287                 "(bug)",
288                 "ip",
289                 "port",
290                 "mac",
291                 "net",
292                 "set",
293         };
294
295         if (ipset->external && *ipset->external)
296                 return;
297
298         info("Creating ipset %s", ipset->name);
299
300         first = true;
301         fw3_pr("create %s %s", ipset->name, methods[ipset->method]);
302
303         list_for_each_entry(type, &ipset->datatypes, list)
304         {
305                 fw3_pr("%c%s", first ? ':' : ',', types[type->type]);
306                 first = false;
307         }
308
309         if (!list_empty(&ipset->iprange))
310         {
311                 a1 = list_first_entry(&ipset->iprange, struct fw3_address, list);
312                 a2 = list_last_entry(&ipset->iprange, struct fw3_address, list);
313
314                 if (a1 == a2)
315                 {
316                         inet_ntop(a1->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
317                                   &a1->address.v6, s, sizeof(s));
318
319                         fw3_pr(" range %s/%u", s, a1->mask);
320                 }
321                 else if (a1->family == a2->family &&
322                          fw3_is_family(ipset, a1->family) &&
323                          fw3_is_family(ipset, a2->family))
324                 {
325                         inet_ntop(a1->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
326                                   &a1->address.v6, s, sizeof(s));
327
328                         fw3_pr(" range %s", s);
329
330                         inet_ntop(a2->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
331                                   &a2->address.v6, s, sizeof(s));
332
333                         fw3_pr("-%s", s);
334                 }
335         }
336         else if (ipset->portrange.set)
337         {
338                 fw3_pr(" range %u-%u",
339                        ipset->portrange.port_min, ipset->portrange.port_max);
340         }
341
342         if (ipset->family != FW3_FAMILY_ANY)
343                 fw3_pr(" family inet%s", (ipset->family == FW3_FAMILY_V4) ? "" : "6");
344
345         if (ipset->timeout > 0)
346                 fw3_pr(" timeout %u", ipset->timeout);
347
348         if (ipset->maxelem > 0)
349                 fw3_pr(" maxelem %u", ipset->maxelem);
350
351         if (ipset->netmask > 0)
352                 fw3_pr(" netmask %u", ipset->netmask);
353
354         if (ipset->hashsize > 0)
355                 fw3_pr(" hashsize %u", ipset->hashsize);
356
357         fw3_pr("\n");
358
359         fw3_set_running(ipset, &state->running_ipsets);
360 }
361
362 void
363 fw3_create_ipsets(struct fw3_state *state)
364 {
365         struct fw3_ipset *ipset;
366
367         if (state->disable_ipsets)
368                 return;
369
370         list_for_each_entry(ipset, &state->ipsets, list)
371                 if (!fw3_lookup_ipset(state, ipset->name, true))
372                         create_ipset(ipset, state);
373
374         fw3_pr("quit\n");
375 }
376
377 void
378 fw3_destroy_ipsets(struct fw3_state *state)
379 {
380         struct fw3_ipset *s, *tmp;
381         int mask = (1 << FW3_FAMILY_V4) | (1 << FW3_FAMILY_V6);
382
383         list_for_each_entry_safe(s, tmp, &state->running_ipsets, running_list)
384         {
385                 if (!hasbit(state->defaults.flags, FW3_FAMILY_V4))
386                         delbit(s->flags, FW3_FAMILY_V4);
387
388                 if (!hasbit(state->defaults.flags, FW3_FAMILY_V6))
389                         delbit(s->flags, FW3_FAMILY_V6);
390
391                 if (!(s->flags & mask))
392                 {
393                         info("Deleting ipset %s", s->name);
394
395                         fw3_pr("flush %s\n", s->name);
396                         fw3_pr("destroy %s\n", s->name);
397
398                         fw3_set_running(s, NULL);
399                 }
400         }
401 }
402
403 struct fw3_ipset *
404 fw3_lookup_ipset(struct fw3_state *state, const char *name, bool running)
405 {
406         struct fw3_ipset *s;
407
408         if (list_empty(&state->ipsets))
409                 return NULL;
410
411         list_for_each_entry(s, &state->ipsets, list)
412         {
413                 if (strcmp(s->name, name))
414                         continue;
415
416                 if (!running || s->running_list.next)
417                         return s;
418
419                 break;
420         }
421
422         return NULL;
423 }