rework ipset removal logic to only purge sets that are not in use by any family
[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 if (ipset->external && *ipset->external)
267 return;
268
269 info("Creating ipset %s", ipset->name);
270
271 first = true;
272 fw3_pr("create %s %s", ipset->name, methods[ipset->method]);
273
274 list_for_each_entry(type, &ipset->datatypes, list)
275 {
276 fw3_pr("%c%s", first ? ':' : ',', types[type->type]);
277 first = false;
278 }
279
280 if (!list_empty(&ipset->iprange))
281 {
282 a1 = list_first_entry(&ipset->iprange, struct fw3_address, list);
283 a2 = list_last_entry(&ipset->iprange, struct fw3_address, list);
284
285 if (a1 == a2)
286 {
287 inet_ntop(a1->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
288 &a1->address.v6, s, sizeof(s));
289
290 fw3_pr(" range %s/%u", s, a1->mask);
291 }
292 else if (a1->family == a2->family &&
293 fw3_is_family(ipset, a1->family) &&
294 fw3_is_family(ipset, a2->family))
295 {
296 inet_ntop(a1->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
297 &a1->address.v6, s, sizeof(s));
298
299 fw3_pr(" range %s", s);
300
301 inet_ntop(a2->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
302 &a2->address.v6, s, sizeof(s));
303
304 fw3_pr("-%s", s);
305 }
306 }
307 else if (ipset->portrange.set)
308 {
309 fw3_pr(" range %u-%u",
310 ipset->portrange.port_min, ipset->portrange.port_max);
311 }
312
313 if (ipset->family != FW3_FAMILY_ANY)
314 fw3_pr(" family inet%s", (ipset->family == FW3_FAMILY_V4) ? "" : "6");
315
316 if (ipset->timeout > 0)
317 fw3_pr(" timeout %u", ipset->timeout);
318
319 if (ipset->maxelem > 0)
320 fw3_pr(" maxelem %u", ipset->maxelem);
321
322 if (ipset->netmask > 0)
323 fw3_pr(" netmask %u", ipset->netmask);
324
325 if (ipset->hashsize > 0)
326 fw3_pr(" hashsize %u", ipset->hashsize);
327
328 fw3_pr("\n");
329 }
330
331 static bool
332 ipset_loaded(struct list_head *statefile, const char *name)
333 {
334 struct fw3_statefile_entry *e;
335 int mask = (1 << FW3_FAMILY_V4) | (1 << FW3_FAMILY_V6);
336
337 if (!statefile)
338 return false;
339
340 list_for_each_entry(e, statefile, list)
341 {
342 if (e->type != FW3_TYPE_IPSET)
343 continue;
344
345 if (!strcmp(e->name, name) && (e->flags[0] & mask))
346 return true;
347 }
348
349 return false;
350 }
351
352 void
353 fw3_create_ipsets(struct fw3_state *state, struct list_head *statefile)
354 {
355 struct fw3_ipset *ipset;
356
357 if (state->disable_ipsets)
358 return;
359
360 list_for_each_entry(ipset, &state->ipsets, list)
361 if (!ipset_loaded(statefile, ipset->name))
362 create_ipset(ipset);
363
364 fw3_pr("quit\n");
365 }
366
367 void
368 fw3_destroy_ipsets(struct fw3_state *state, struct list_head *statefile)
369 {
370 struct fw3_ipset *s;
371 struct fw3_statefile_entry *e;
372 int mask = (1 << FW3_FAMILY_V4) | (1 << FW3_FAMILY_V6);
373
374 if (!statefile)
375 return;
376
377 list_for_each_entry(e, statefile, list)
378 {
379 if (e->type != FW3_TYPE_IPSET)
380 continue;
381
382 if (!hasbit(state->defaults.flags, FW3_FAMILY_V4))
383 delbit(e->flags[0], FW3_FAMILY_V4);
384
385 if (!hasbit(state->defaults.flags, FW3_FAMILY_V6))
386 delbit(e->flags[0], FW3_FAMILY_V6);
387
388 if ((s = fw3_lookup_ipset(state, e->name)) != NULL)
389 s->flags = e->flags[0];
390
391 if (!(e->flags[0] & mask))
392 {
393 info("Deleting ipset %s", e->name);
394
395 fw3_pr("flush %s\n", e->name);
396 fw3_pr("destroy %s\n", e->name);
397 }
398 }
399 }
400
401 void
402 fw3_free_ipset(struct fw3_ipset *ipset)
403 {
404 fw3_free_list(&ipset->datatypes);
405 fw3_free_list(&ipset->iprange);
406
407 free(ipset);
408 }
409
410 struct fw3_ipset *
411 fw3_lookup_ipset(struct fw3_state *state, const char *name)
412 {
413 struct fw3_ipset *ipset;
414
415 if (list_empty(&state->ipsets))
416 return NULL;
417
418 list_for_each_entry(ipset, &state->ipsets, list)
419 if (!strcmp(ipset->name, name))
420 return ipset;
421
422 return NULL;
423 }