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