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