options: fix logic flaw when parsing ipaddr/mask notation
[project/firewall3.git] / zones.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 "zones.h"
20 #include "ubus.h"
21
22
23 #define C(f, tbl, tgt, fmt) \
24         { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_FLAG_##tgt, fmt }
25
26 static const struct fw3_chain_spec zone_chains[] = {
27         C(ANY, FILTER, UNSPEC,        "zone_%s_input"),
28         C(ANY, FILTER, UNSPEC,        "zone_%s_output"),
29         C(ANY, FILTER, UNSPEC,        "zone_%s_forward"),
30
31         C(ANY, FILTER, SRC_ACCEPT,    "zone_%s_src_ACCEPT"),
32         C(ANY, FILTER, SRC_REJECT,    "zone_%s_src_REJECT"),
33         C(ANY, FILTER, SRC_DROP,      "zone_%s_src_DROP"),
34
35         C(ANY, FILTER, ACCEPT,        "zone_%s_dest_ACCEPT"),
36         C(ANY, FILTER, REJECT,        "zone_%s_dest_REJECT"),
37         C(ANY, FILTER, DROP,          "zone_%s_dest_DROP"),
38
39         C(V4,  NAT,    SNAT,          "zone_%s_postrouting"),
40         C(V4,  NAT,    DNAT,          "zone_%s_prerouting"),
41
42         C(ANY, RAW,    NOTRACK,       "zone_%s_notrack"),
43
44         C(ANY, FILTER, CUSTOM_CHAINS, "input_%s_rule"),
45         C(ANY, FILTER, CUSTOM_CHAINS, "output_%s_rule"),
46         C(ANY, FILTER, CUSTOM_CHAINS, "forwarding_%s_rule"),
47
48         C(V4,  NAT,    CUSTOM_CHAINS, "prerouting_%s_rule"),
49         C(V4,  NAT,    CUSTOM_CHAINS, "postrouting_%s_rule"),
50
51         { }
52 };
53
54 const struct fw3_option fw3_zone_opts[] = {
55         FW3_OPT("enabled",             bool,     zone,     enabled),
56
57         FW3_OPT("name",                string,   zone,     name),
58         FW3_OPT("family",              family,   zone,     family),
59
60         FW3_LIST("network",            device,   zone,     networks),
61         FW3_LIST("device",             device,   zone,     devices),
62         FW3_LIST("subnet",             network,  zone,     subnets),
63
64         FW3_OPT("input",               target,   zone,     policy_input),
65         FW3_OPT("forward",             target,   zone,     policy_forward),
66         FW3_OPT("output",              target,   zone,     policy_output),
67
68         FW3_OPT("masq",                bool,     zone,     masq),
69         FW3_LIST("masq_src",           network,  zone,     masq_src),
70         FW3_LIST("masq_dest",          network,  zone,     masq_dest),
71
72         FW3_OPT("extra",               string,   zone,     extra_src),
73         FW3_OPT("extra_src",           string,   zone,     extra_src),
74         FW3_OPT("extra_dest",          string,   zone,     extra_dest),
75
76         FW3_OPT("conntrack",           bool,     zone,     conntrack),
77         FW3_OPT("mtu_fix",             bool,     zone,     mtu_fix),
78         FW3_OPT("custom_chains",       bool,     zone,     custom_chains),
79
80         FW3_OPT("log",                 bool,     zone,     log),
81         FW3_OPT("log_limit",           limit,    zone,     log_limit),
82
83         FW3_OPT("__flags_v4",          int,      zone,     flags[0]),
84         FW3_OPT("__flags_v6",          int,      zone,     flags[1]),
85
86         { }
87 };
88
89
90 static void
91 check_policy(struct uci_element *e, enum fw3_flag *pol, enum fw3_flag def,
92              const char *name)
93 {
94         if (*pol == FW3_FLAG_UNSPEC)
95         {
96                 warn_elem(e, "has no %s policy specified, using default", name);
97                 *pol = def;
98         }
99         else if (*pol > FW3_FLAG_DROP)
100         {
101                 warn_elem(e, "has invalid %s policy, using default", name);
102                 *pol = def;
103         }
104 }
105
106 static void
107 resolve_networks(struct uci_element *e, struct fw3_zone *zone)
108 {
109         struct fw3_device *net, *tmp;
110
111         list_for_each_entry(net, &zone->networks, list)
112         {
113                 tmp = fw3_ubus_device(net->name);
114
115                 if (!tmp)
116                 {
117                         warn_elem(e, "cannot resolve device of network '%s'", net->name);
118                         continue;
119                 }
120
121                 snprintf(tmp->network, sizeof(tmp->network), "%s", net->name);
122                 list_add_tail(&tmp->list, &zone->devices);
123         }
124 }
125
126 struct fw3_zone *
127 fw3_alloc_zone(void)
128 {
129         struct fw3_zone *zone;
130
131         zone = calloc(1, sizeof(*zone));
132         if (!zone)
133                 return NULL;
134
135         INIT_LIST_HEAD(&zone->networks);
136         INIT_LIST_HEAD(&zone->devices);
137         INIT_LIST_HEAD(&zone->subnets);
138         INIT_LIST_HEAD(&zone->masq_src);
139         INIT_LIST_HEAD(&zone->masq_dest);
140
141         zone->enabled = true;
142         zone->custom_chains = true;
143         zone->log_limit.rate = 10;
144
145         return zone;
146 }
147
148 void
149 fw3_load_zones(struct fw3_state *state, struct uci_package *p)
150 {
151         struct uci_section *s;
152         struct uci_element *e;
153         struct fw3_zone *zone;
154         struct fw3_defaults *defs = &state->defaults;
155
156         INIT_LIST_HEAD(&state->zones);
157
158         uci_foreach_element(&p->sections, e)
159         {
160                 s = uci_to_section(e);
161
162                 if (strcmp(s->type, "zone"))
163                         continue;
164
165                 zone = fw3_alloc_zone();
166
167                 if (!zone)
168                         continue;
169
170                 fw3_parse_options(zone, fw3_zone_opts, s);
171
172                 if (!zone->enabled)
173                 {
174                         fw3_free_zone(zone);
175                         continue;
176                 }
177
178                 if (!zone->extra_dest)
179                         zone->extra_dest = zone->extra_src;
180
181                 if (!defs->custom_chains && zone->custom_chains)
182                         zone->custom_chains = false;
183
184                 if (!zone->name || !*zone->name)
185                 {
186                         warn_elem(e, "has no name - ignoring");
187                         fw3_free_zone(zone);
188                         continue;
189                 }
190
191                 if (strlen(zone->name) > FW3_ZONE_MAXNAMELEN)
192                 {
193                         warn_elem(e, "must not have a name longer than %u characters",
194                                      FW3_ZONE_MAXNAMELEN);
195                         fw3_free_zone(zone);
196                         continue;
197                 }
198
199                 fw3_ubus_zone_devices(zone);
200
201                 if (list_empty(&zone->networks) && list_empty(&zone->devices) &&
202                     list_empty(&zone->subnets) && !zone->extra_src)
203                 {
204                         warn_elem(e, "has no device, network, subnet or extra options");
205                 }
206
207                 check_policy(e, &zone->policy_input, defs->policy_input, "input");
208                 check_policy(e, &zone->policy_output, defs->policy_output, "output");
209                 check_policy(e, &zone->policy_forward, defs->policy_forward, "forward");
210
211                 resolve_networks(e, zone);
212
213                 if (zone->masq)
214                 {
215                         setbit(zone->flags[0], FW3_FLAG_SNAT);
216                         zone->conntrack = true;
217                 }
218
219                 if (zone->custom_chains)
220                 {
221                         setbit(zone->flags[0], FW3_FLAG_SNAT);
222                         setbit(zone->flags[0], FW3_FLAG_DNAT);
223                 }
224
225                 setbit(zone->flags[0], fw3_to_src_target(zone->policy_input));
226                 setbit(zone->flags[0], fw3_to_src_target(zone->policy_forward));
227                 setbit(zone->flags[0], zone->policy_output);
228
229                 setbit(zone->flags[1], fw3_to_src_target(zone->policy_input));
230                 setbit(zone->flags[1], fw3_to_src_target(zone->policy_forward));
231                 setbit(zone->flags[1], zone->policy_output);
232
233                 list_add_tail(&zone->list, &state->zones);
234         }
235 }
236
237
238 static void
239 print_zone_chain(struct fw3_ipt_handle *handle, struct fw3_state *state,
240                  bool reload, struct fw3_zone *zone)
241 {
242         int i;
243         struct fw3_ipt_rule *r;
244         const struct fw3_chain_spec *c;
245
246         const char *flt_chains[] = {
247                 "input",   "input",
248                 "output",  "output",
249                 "forward", "forwarding",
250         };
251
252         const char *nat_chains[] = {
253                 "prerouting",  "prerouting",
254                 "postrouting", "postrouting",
255         };
256
257         if (!fw3_is_family(zone, handle->family))
258                 return;
259
260         info("   * Zone '%s'", zone->name);
261
262         set(zone->flags, handle->family, handle->table);
263
264         if (zone->custom_chains)
265                 set(zone->flags, handle->family, FW3_FLAG_CUSTOM_CHAINS);
266
267         if (!zone->conntrack && !state->defaults.drop_invalid)
268                 set(zone->flags, handle->family, FW3_FLAG_NOTRACK);
269
270         for (c = zone_chains; c->format; c++)
271         {
272                 /* don't touch user chains on selective stop */
273                 if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
274                         continue;
275
276                 if (!fw3_is_family(c, handle->family))
277                         continue;
278
279                 if (c->table != handle->table)
280                         continue;
281
282                 if (c->flag &&
283                     !hasbit(zone->flags[handle->family == FW3_FAMILY_V6], c->flag))
284                         continue;
285
286                 fw3_ipt_create_chain(handle, c->format, zone->name);
287         }
288
289         if (zone->custom_chains)
290         {
291                 if (handle->table == FW3_TABLE_FILTER)
292                 {
293                         for (i = 0; i < sizeof(flt_chains)/sizeof(flt_chains[0]); i += 2)
294                         {
295                                 r = fw3_ipt_rule_new(handle);
296                                 fw3_ipt_rule_comment(r, "user chain for %s", flt_chains[i+1]);
297                                 fw3_ipt_rule_target(r, "%s_%s_rule", flt_chains[i+1], zone->name);
298                                 fw3_ipt_rule_append(r, "zone_%s_%s", zone->name, flt_chains[i]);
299                         }
300                 }
301                 else if (handle->table == FW3_TABLE_NAT)
302                 {
303                         for (i = 0; i < sizeof(nat_chains)/sizeof(nat_chains[0]); i += 2)
304                         {
305                                 r = fw3_ipt_rule_new(handle);
306                                 fw3_ipt_rule_comment(r, "user chain for %s", nat_chains[i+1]);
307                                 fw3_ipt_rule_target(r, "%s_%s_rule", nat_chains[i+1], zone->name);
308                                 fw3_ipt_rule_append(r, "zone_%s_%s", zone->name, nat_chains[i]);
309                         }
310                 }
311         }
312
313         set(zone->flags, handle->family, handle->table);
314 }
315
316 static void
317 print_interface_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
318                                          bool reload, struct fw3_zone *zone,
319                      struct fw3_device *dev, struct fw3_address *sub)
320 {
321         struct fw3_protocol tcp = { .protocol = 6 };
322         struct fw3_ipt_rule *r;
323         enum fw3_flag t;
324
325         char buf[32];
326
327         int i;
328
329         const char *chains[] = {
330                 "input",
331                 "output",
332                 "forward",
333         };
334
335 #define jump_target(t) \
336         ((t == FW3_FLAG_REJECT) ? "reject" : fw3_flag_names[t])
337
338         if (handle->table == FW3_TABLE_FILTER)
339         {
340                 for (t = FW3_FLAG_ACCEPT; t <= FW3_FLAG_DROP; t++)
341                 {
342                         if (has(zone->flags, handle->family, fw3_to_src_target(t)))
343                         {
344                                 r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
345                                 fw3_ipt_rule_target(r, jump_target(t));
346                                 fw3_ipt_rule_extra(r, zone->extra_src);
347                                 fw3_ipt_rule_replace(r, "zone_%s_src_%s", zone->name,
348                                                      fw3_flag_names[t]);
349                         }
350
351                         if (has(zone->flags, handle->family, t))
352                         {
353                                 r = fw3_ipt_rule_create(handle, NULL, NULL, dev, NULL, sub);
354                                 fw3_ipt_rule_target(r, jump_target(t));
355                                 fw3_ipt_rule_extra(r, zone->extra_dest);
356                                 fw3_ipt_rule_replace(r, "zone_%s_dest_%s", zone->name,
357                                                      fw3_flag_names[t]);
358                         }
359                 }
360
361                 for (i = 0; i < sizeof(chains)/sizeof(chains[0]); i++)
362                 {
363                         if (*chains[i] == 'o')
364                                 r = fw3_ipt_rule_create(handle, NULL, NULL, dev, NULL, sub);
365                         else
366                                 r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
367
368                         fw3_ipt_rule_target(r, "zone_%s_%s", zone->name, chains[i]);
369
370                         if (*chains[i] == 'o')
371                                 fw3_ipt_rule_extra(r, zone->extra_dest);
372                         else
373                                 fw3_ipt_rule_extra(r, zone->extra_src);
374
375                         fw3_ipt_rule_replace(r, "delegate_%s", chains[i]);
376                 }
377         }
378         else if (handle->table == FW3_TABLE_NAT)
379         {
380                 if (has(zone->flags, handle->family, FW3_FLAG_DNAT))
381                 {
382                         r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
383                         fw3_ipt_rule_target(r, "zone_%s_prerouting", zone->name);
384                         fw3_ipt_rule_extra(r, zone->extra_src);
385                         fw3_ipt_rule_replace(r, "delegate_prerouting");
386                 }
387
388                 if (has(zone->flags, handle->family, FW3_FLAG_SNAT))
389                 {
390                         r = fw3_ipt_rule_create(handle, NULL, NULL, dev, NULL, sub);
391                         fw3_ipt_rule_target(r, "zone_%s_postrouting", zone->name);
392                         fw3_ipt_rule_extra(r, zone->extra_dest);
393                         fw3_ipt_rule_replace(r, "delegate_postrouting");
394                 }
395         }
396         else if (handle->table == FW3_TABLE_MANGLE)
397         {
398                 if (zone->mtu_fix)
399                 {
400                         if (zone->log)
401                         {
402                                 snprintf(buf, sizeof(buf) - 1, "MSSFIX(%s): ", zone->name);
403
404                                 r = fw3_ipt_rule_create(handle, &tcp, NULL, dev, NULL, sub);
405                                 fw3_ipt_rule_addarg(r, false, "--tcp-flags", "SYN,RST");
406                                 fw3_ipt_rule_addarg(r, false, "SYN", NULL);
407                                 fw3_ipt_rule_limit(r, &zone->log_limit);
408                                 fw3_ipt_rule_comment(r, "%s (mtu_fix logging)", zone->name);
409                                 fw3_ipt_rule_target(r, "LOG");
410                                 fw3_ipt_rule_addarg(r, false, "--log-prefix", buf);
411                                 fw3_ipt_rule_replace(r, "mssfix");
412                         }
413
414                         r = fw3_ipt_rule_create(handle, &tcp, NULL, dev, NULL, sub);
415                         fw3_ipt_rule_addarg(r, false, "--tcp-flags", "SYN,RST");
416                         fw3_ipt_rule_addarg(r, false, "SYN", NULL);
417                         fw3_ipt_rule_comment(r, "%s (mtu_fix)", zone->name);
418                         fw3_ipt_rule_target(r, "TCPMSS");
419                         fw3_ipt_rule_addarg(r, false, "--clamp-mss-to-pmtu", NULL);
420                         fw3_ipt_rule_replace(r, "mssfix");
421                 }
422         }
423         else if (handle->table == FW3_TABLE_RAW)
424         {
425                 if (has(zone->flags, handle->family, FW3_FLAG_NOTRACK))
426                 {
427                         r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
428                         fw3_ipt_rule_target(r, "zone_%s_notrack", zone->name);
429                         fw3_ipt_rule_extra(r, zone->extra_src);
430                         fw3_ipt_rule_replace(r, "delegate_notrack");
431                 }
432         }
433 }
434
435 static void
436 print_interface_rules(struct fw3_ipt_handle *handle, struct fw3_state *state,
437                       bool reload, struct fw3_zone *zone)
438 {
439         struct fw3_device *dev;
440         struct fw3_address *sub;
441
442         fw3_foreach(dev, &zone->devices)
443         fw3_foreach(sub, &zone->subnets)
444         {
445                 if (!fw3_is_family(sub, handle->family))
446                         continue;
447
448                 if (!dev && !sub)
449                         continue;
450
451                 print_interface_rule(handle, state, reload, zone, dev, sub);
452         }
453 }
454
455 static void
456 print_zone_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
457                 bool reload, struct fw3_zone *zone)
458 {
459         bool disable_notrack = state->defaults.drop_invalid;
460         struct fw3_address *msrc;
461         struct fw3_address *mdest;
462         struct fw3_ipt_rule *r;
463
464         enum fw3_flag t;
465         char buf[32];
466
467         if (!fw3_is_family(zone, handle->family))
468                 return;
469
470         switch (handle->table)
471         {
472         case FW3_TABLE_FILTER:
473                 if (has(zone->flags, handle->family, FW3_FLAG_DNAT))
474                 {
475                         r = fw3_ipt_rule_new(handle);
476                         fw3_ipt_rule_extra(r, "-m conntrack --ctstate DNAT");
477                         fw3_ipt_rule_comment(r, "Accept port redirections");
478                         fw3_ipt_rule_target(r, fw3_flag_names[FW3_FLAG_ACCEPT]);
479                         fw3_ipt_rule_append(r, "zone_%s_input", zone->name);
480
481                         r = fw3_ipt_rule_new(handle);
482                         fw3_ipt_rule_extra(r, "-m conntrack --ctstate DNAT");
483                         fw3_ipt_rule_comment(r, "Accept port forwards");
484                         fw3_ipt_rule_target(r, fw3_flag_names[FW3_FLAG_ACCEPT]);
485                         fw3_ipt_rule_append(r, "zone_%s_forward", zone->name);
486                 }
487
488                 r = fw3_ipt_rule_new(handle);
489                 fw3_ipt_rule_target(r, "zone_%s_src_%s", zone->name,
490                                      fw3_flag_names[zone->policy_input]);
491                 fw3_ipt_rule_append(r, "zone_%s_input", zone->name);
492
493                 r = fw3_ipt_rule_new(handle);
494                 fw3_ipt_rule_target(r, "zone_%s_src_%s", zone->name,
495                                      fw3_flag_names[zone->policy_forward]);
496                 fw3_ipt_rule_append(r, "zone_%s_forward", zone->name);
497
498                 r = fw3_ipt_rule_new(handle);
499                 fw3_ipt_rule_target(r, "zone_%s_dest_%s", zone->name,
500                                      fw3_flag_names[zone->policy_output]);
501                 fw3_ipt_rule_append(r, "zone_%s_output", zone->name);
502
503                 if (zone->log)
504                 {
505                         for (t = FW3_FLAG_REJECT; t <= FW3_FLAG_DROP; t++)
506                         {
507                                 if (has(zone->flags, handle->family, fw3_to_src_target(t)))
508                                 {
509                                         r = fw3_ipt_rule_new(handle);
510
511                                         snprintf(buf, sizeof(buf) - 1, "%s(src %s)",
512                                                  fw3_flag_names[t], zone->name);
513
514                                         fw3_ipt_rule_limit(r, &zone->log_limit);
515                                         fw3_ipt_rule_target(r, "LOG");
516                                         fw3_ipt_rule_addarg(r, false, "--log-prefix", buf);
517                                         fw3_ipt_rule_append(r, "zone_%s_src_%s",
518                                                             zone->name, fw3_flag_names[t]);
519                                 }
520
521                                 if (has(zone->flags, handle->family, t))
522                                 {
523                                         r = fw3_ipt_rule_new(handle);
524
525                                         snprintf(buf, sizeof(buf) - 1, "%s(dest %s)",
526                                                  fw3_flag_names[t], zone->name);
527
528                                         fw3_ipt_rule_limit(r, &zone->log_limit);
529                                         fw3_ipt_rule_target(r, "LOG");
530                                         fw3_ipt_rule_addarg(r, false, "--log-prefix", buf);
531                                         fw3_ipt_rule_append(r, "zone_%s_dest_%s",
532                                                             zone->name, fw3_flag_names[t]);
533                                 }
534                         }
535                 }
536                 break;
537
538         case FW3_TABLE_NAT:
539                 if (zone->masq && handle->family == FW3_FAMILY_V4)
540                 {
541                         fw3_foreach(msrc, &zone->masq_src)
542                         fw3_foreach(mdest, &zone->masq_dest)
543                         {
544                                 if (!fw3_is_family(msrc, handle->family) ||
545                                     !fw3_is_family(mdest, handle->family))
546                                         continue;
547
548                                 r = fw3_ipt_rule_new(handle);
549                                 fw3_ipt_rule_src_dest(r, msrc, mdest);
550                                 fw3_ipt_rule_target(r, "MASQUERADE");
551                                 fw3_ipt_rule_append(r, "zone_%s_postrouting", zone->name);
552                         }
553                 }
554                 break;
555
556         case FW3_TABLE_RAW:
557                 if (!zone->conntrack && !disable_notrack)
558                 {
559                         r = fw3_ipt_rule_new(handle);
560                         fw3_ipt_rule_target(r, "CT");
561                         fw3_ipt_rule_addarg(r, false, "--notrack", NULL);
562                         fw3_ipt_rule_append(r, "zone_%s_notrack", zone->name);
563                 }
564                 break;
565
566         case FW3_TABLE_MANGLE:
567                 break;
568         }
569
570         print_interface_rules(handle, state, reload, zone);
571 }
572
573 void
574 fw3_print_zone_chains(struct fw3_ipt_handle *handle, struct fw3_state *state,
575                       bool reload)
576 {
577         struct fw3_zone *zone;
578
579         list_for_each_entry(zone, &state->zones, list)
580                 print_zone_chain(handle, state, reload, zone);
581 }
582
583 void
584 fw3_print_zone_rules(struct fw3_ipt_handle *handle, struct fw3_state *state,
585                      bool reload)
586 {
587         struct fw3_zone *zone;
588
589         list_for_each_entry(zone, &state->zones, list)
590                 print_zone_rule(handle, state, reload, zone);
591 }
592
593 void
594 fw3_flush_zones(struct fw3_ipt_handle *handle, struct fw3_state *state,
595                 bool reload)
596 {
597         struct fw3_zone *z, *tmp;
598         const struct fw3_chain_spec *c;
599         char chain[32];
600
601         list_for_each_entry_safe(z, tmp, &state->zones, list)
602         {
603                 if (!has(z->flags, handle->family, handle->table))
604                         continue;
605
606                 for (c = zone_chains; c->format; c++)
607                 {
608                         /* don't touch user chains on selective stop */
609                         if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
610                                 continue;
611
612                         if (!fw3_is_family(c, handle->family))
613                                 continue;
614
615                         if (c->table != handle->table)
616                                 continue;
617
618                         if (c->flag && !has(z->flags, handle->family, c->flag))
619                                 continue;
620
621                         snprintf(chain, sizeof(chain), c->format, z->name);
622                         fw3_ipt_flush_chain(handle, chain);
623
624                         /* keep certain basic chains that do not depend on any settings to
625                            avoid purging unrelated user rules pointing to them */
626                         if (reload && !c->flag)
627                                 continue;
628
629                         fw3_ipt_delete_chain(handle, chain);
630                 }
631
632                 del(z->flags, handle->family, handle->table);
633         }
634 }
635
636 void
637 fw3_hotplug_zones(struct fw3_state *state, bool add)
638 {
639         struct fw3_zone *z;
640         struct fw3_device *d;
641
642         list_for_each_entry(z, &state->zones, list)
643         {
644                 if (add != hasbit(z->flags[0], FW3_FLAG_HOTPLUG))
645                 {
646                         list_for_each_entry(d, &z->devices, list)
647                                 fw3_hotplug(add, z, d);
648
649                         if (add)
650                                 setbit(z->flags[0], FW3_FLAG_HOTPLUG);
651                         else
652                                 delbit(z->flags[0], FW3_FLAG_HOTPLUG);
653                 }
654         }
655 }
656
657 struct fw3_zone *
658 fw3_lookup_zone(struct fw3_state *state, const char *name)
659 {
660         struct fw3_zone *z;
661
662         if (list_empty(&state->zones))
663                 return NULL;
664
665         list_for_each_entry(z, &state->zones, list)
666         {
667                 if (strcmp(z->name, name))
668                         continue;
669
670                 return z;
671         }
672
673         return NULL;
674 }
675
676 struct list_head *
677 fw3_resolve_zone_addresses(struct fw3_zone *zone)
678 {
679         struct fw3_device *net;
680         struct fw3_address *addr, *tmp;
681         struct list_head *all;
682
683         all = calloc(1, sizeof(*all));
684         if (!all)
685                 return NULL;
686
687         INIT_LIST_HEAD(all);
688
689         list_for_each_entry(net, &zone->networks, list)
690                 fw3_ubus_address(all, net->name);
691
692         list_for_each_entry(addr, &zone->subnets, list)
693         {
694                 tmp = malloc(sizeof(*tmp));
695
696                 if (!tmp)
697                         continue;
698
699                 memcpy(tmp, addr, sizeof(*tmp));
700                 list_add_tail(&tmp->list, all);
701         }
702
703         return all;
704 }