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