a4458fe5f7ea96b0248bb1ed70f7c459f56317a4
[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,
354                                                            "-m conntrack --ctstate NEW,UNTRACKED");
355
356                                 fw3_ipt_rule_replace(r, "zone_%s_src_%s", zone->name,
357                                                      fw3_flag_names[t]);
358                         }
359
360                         if (has(zone->flags, handle->family, t))
361                         {
362                                 r = fw3_ipt_rule_create(handle, NULL, NULL, dev, NULL, sub);
363                                 fw3_ipt_rule_target(r, jump_target(t));
364                                 fw3_ipt_rule_extra(r, zone->extra_dest);
365
366                                 if (t == FW3_FLAG_ACCEPT && !state->defaults.drop_invalid)
367                                         fw3_ipt_rule_extra(r,
368                                                            "-m conntrack --ctstate NEW,UNTRACKED");
369
370                                 fw3_ipt_rule_replace(r, "zone_%s_dest_%s", zone->name,
371                                                      fw3_flag_names[t]);
372                         }
373                 }
374
375                 for (i = 0; i < sizeof(chains)/sizeof(chains[0]); i += 2)
376                 {
377                         if (*chains[i] == 'o')
378                                 r = fw3_ipt_rule_create(handle, NULL, NULL, dev, NULL, sub);
379                         else
380                                 r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
381
382                         fw3_ipt_rule_target(r, "zone_%s_%s", zone->name, chains[i]);
383
384                         if (*chains[i] == 'o')
385                                 fw3_ipt_rule_extra(r, zone->extra_dest);
386                         else
387                                 fw3_ipt_rule_extra(r, zone->extra_src);
388
389                         fw3_ipt_rule_replace(r, chains[i + 1]);
390                 }
391         }
392         else if (handle->table == FW3_TABLE_NAT)
393         {
394                 if (has(zone->flags, handle->family, FW3_FLAG_DNAT))
395                 {
396                         r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
397                         fw3_ipt_rule_target(r, "zone_%s_prerouting", zone->name);
398                         fw3_ipt_rule_extra(r, zone->extra_src);
399                         fw3_ipt_rule_replace(r, "PREROUTING");
400                 }
401
402                 if (has(zone->flags, handle->family, FW3_FLAG_SNAT))
403                 {
404                         r = fw3_ipt_rule_create(handle, NULL, NULL, dev, NULL, sub);
405                         fw3_ipt_rule_target(r, "zone_%s_postrouting", zone->name);
406                         fw3_ipt_rule_extra(r, zone->extra_dest);
407                         fw3_ipt_rule_replace(r, "POSTROUTING");
408                 }
409         }
410         else if (handle->table == FW3_TABLE_MANGLE)
411         {
412                 if (zone->mtu_fix)
413                 {
414                         if (zone->log)
415                         {
416                                 snprintf(buf, sizeof(buf) - 1, "MSSFIX(%s): ", zone->name);
417
418                                 r = fw3_ipt_rule_create(handle, &tcp, NULL, dev, NULL, sub);
419                                 fw3_ipt_rule_addarg(r, false, "--tcp-flags", "SYN,RST");
420                                 fw3_ipt_rule_addarg(r, false, "SYN", NULL);
421                                 fw3_ipt_rule_limit(r, &zone->log_limit);
422                                 fw3_ipt_rule_comment(r, "%s (mtu_fix logging)", zone->name);
423                                 fw3_ipt_rule_target(r, "LOG");
424                                 fw3_ipt_rule_addarg(r, false, "--log-prefix", buf);
425                                 fw3_ipt_rule_replace(r, "FORWARD");
426                         }
427
428                         r = fw3_ipt_rule_create(handle, &tcp, NULL, dev, NULL, sub);
429                         fw3_ipt_rule_addarg(r, false, "--tcp-flags", "SYN,RST");
430                         fw3_ipt_rule_addarg(r, false, "SYN", NULL);
431                         fw3_ipt_rule_comment(r, "%s (mtu_fix)", zone->name);
432                         fw3_ipt_rule_target(r, "TCPMSS");
433                         fw3_ipt_rule_addarg(r, false, "--clamp-mss-to-pmtu", NULL);
434                         fw3_ipt_rule_replace(r, "FORWARD");
435                 }
436         }
437         else if (handle->table == FW3_TABLE_RAW)
438         {
439                 if (has(zone->flags, handle->family, FW3_FLAG_NOTRACK))
440                 {
441                         r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
442                         fw3_ipt_rule_target(r, "zone_%s_notrack", zone->name);
443                         fw3_ipt_rule_extra(r, zone->extra_src);
444                         fw3_ipt_rule_replace(r, "PREROUTING");
445                 }
446         }
447 }
448
449 static void
450 print_interface_rules(struct fw3_ipt_handle *handle, struct fw3_state *state,
451                       bool reload, struct fw3_zone *zone)
452 {
453         struct fw3_device *dev;
454         struct fw3_address *sub;
455
456         fw3_foreach(dev, &zone->devices)
457         fw3_foreach(sub, &zone->subnets)
458         {
459                 if (!fw3_is_family(sub, handle->family))
460                         continue;
461
462                 if (!dev && !sub)
463                         continue;
464
465                 print_interface_rule(handle, state, reload, zone, dev, sub);
466         }
467 }
468
469 static void
470 print_zone_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
471                 bool reload, struct fw3_zone *zone)
472 {
473         bool disable_notrack = state->defaults.drop_invalid;
474         struct fw3_address *msrc;
475         struct fw3_address *mdest;
476         struct fw3_ipt_rule *r;
477
478         enum fw3_flag t;
479         char buf[32];
480
481         if (!fw3_is_family(zone, handle->family))
482                 return;
483
484         switch (handle->table)
485         {
486         case FW3_TABLE_FILTER:
487                 if (has(zone->flags, handle->family, FW3_FLAG_DNAT))
488                 {
489                         r = fw3_ipt_rule_new(handle);
490                         fw3_ipt_rule_extra(r, "-m conntrack --ctstate DNAT");
491                         fw3_ipt_rule_comment(r, "Accept port redirections");
492                         fw3_ipt_rule_target(r, fw3_flag_names[FW3_FLAG_ACCEPT]);
493                         fw3_ipt_rule_append(r, "zone_%s_input", zone->name);
494
495                         r = fw3_ipt_rule_new(handle);
496                         fw3_ipt_rule_extra(r, "-m conntrack --ctstate DNAT");
497                         fw3_ipt_rule_comment(r, "Accept port forwards");
498                         fw3_ipt_rule_target(r, fw3_flag_names[FW3_FLAG_ACCEPT]);
499                         fw3_ipt_rule_append(r, "zone_%s_forward", zone->name);
500                 }
501
502                 r = fw3_ipt_rule_new(handle);
503                 fw3_ipt_rule_target(r, "zone_%s_src_%s", zone->name,
504                                      fw3_flag_names[zone->policy_input]);
505                 fw3_ipt_rule_append(r, "zone_%s_input", zone->name);
506
507                 r = fw3_ipt_rule_new(handle);
508                 fw3_ipt_rule_target(r, "zone_%s_dest_%s", zone->name,
509                                      fw3_flag_names[zone->policy_forward]);
510                 fw3_ipt_rule_append(r, "zone_%s_forward", zone->name);
511
512                 r = fw3_ipt_rule_new(handle);
513                 fw3_ipt_rule_target(r, "zone_%s_dest_%s", zone->name,
514                                      fw3_flag_names[zone->policy_output]);
515                 fw3_ipt_rule_append(r, "zone_%s_output", zone->name);
516
517                 if (zone->log)
518                 {
519                         for (t = FW3_FLAG_REJECT; t <= FW3_FLAG_DROP; t++)
520                         {
521                                 if (has(zone->flags, handle->family, fw3_to_src_target(t)))
522                                 {
523                                         r = fw3_ipt_rule_new(handle);
524
525                                         snprintf(buf, sizeof(buf) - 1, "%s(src %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_src_%s",
532                                                             zone->name, fw3_flag_names[t]);
533                                 }
534
535                                 if (has(zone->flags, handle->family, t))
536                                 {
537                                         r = fw3_ipt_rule_new(handle);
538
539                                         snprintf(buf, sizeof(buf) - 1, "%s(dest %s)",
540                                                  fw3_flag_names[t], zone->name);
541
542                                         fw3_ipt_rule_limit(r, &zone->log_limit);
543                                         fw3_ipt_rule_target(r, "LOG");
544                                         fw3_ipt_rule_addarg(r, false, "--log-prefix", buf);
545                                         fw3_ipt_rule_append(r, "zone_%s_dest_%s",
546                                                             zone->name, fw3_flag_names[t]);
547                                 }
548                         }
549                 }
550                 break;
551
552         case FW3_TABLE_NAT:
553                 if (zone->masq && handle->family == FW3_FAMILY_V4)
554                 {
555                         fw3_foreach(msrc, &zone->masq_src)
556                         fw3_foreach(mdest, &zone->masq_dest)
557                         {
558                                 if (!fw3_is_family(msrc, handle->family) ||
559                                     !fw3_is_family(mdest, handle->family))
560                                         continue;
561
562                                 r = fw3_ipt_rule_new(handle);
563                                 fw3_ipt_rule_src_dest(r, msrc, mdest);
564                                 fw3_ipt_rule_target(r, "MASQUERADE");
565                                 fw3_ipt_rule_append(r, "zone_%s_postrouting", zone->name);
566                         }
567                 }
568                 break;
569
570         case FW3_TABLE_RAW:
571                 if (!zone->conntrack && !disable_notrack)
572                 {
573                         r = fw3_ipt_rule_new(handle);
574                         fw3_ipt_rule_target(r, "CT");
575                         fw3_ipt_rule_addarg(r, false, "--notrack", NULL);
576                         fw3_ipt_rule_append(r, "zone_%s_notrack", zone->name);
577                 }
578                 break;
579
580         case FW3_TABLE_MANGLE:
581                 break;
582         }
583
584         print_interface_rules(handle, state, reload, zone);
585 }
586
587 void
588 fw3_print_zone_chains(struct fw3_ipt_handle *handle, struct fw3_state *state,
589                       bool reload)
590 {
591         struct fw3_zone *zone;
592
593         list_for_each_entry(zone, &state->zones, list)
594                 print_zone_chain(handle, state, reload, zone);
595 }
596
597 void
598 fw3_print_zone_rules(struct fw3_ipt_handle *handle, struct fw3_state *state,
599                      bool reload)
600 {
601         struct fw3_zone *zone;
602
603         list_for_each_entry(zone, &state->zones, list)
604                 print_zone_rule(handle, state, reload, zone);
605 }
606
607 void
608 fw3_flush_zones(struct fw3_ipt_handle *handle, struct fw3_state *state,
609                 bool reload)
610 {
611         struct fw3_zone *z, *tmp;
612         const struct fw3_chain_spec *c;
613         char chain[32];
614
615         list_for_each_entry_safe(z, tmp, &state->zones, list)
616         {
617                 if (!has(z->flags, handle->family, handle->table))
618                         continue;
619
620                 for (c = zone_chains; c->format; c++)
621                 {
622                         /* don't touch user chains on selective stop */
623                         if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
624                                 continue;
625
626                         if (!fw3_is_family(c, handle->family))
627                                 continue;
628
629                         if (c->table != handle->table)
630                                 continue;
631
632                         if (c->flag && !has(z->flags, handle->family, c->flag))
633                                 continue;
634
635                         snprintf(chain, sizeof(chain), c->format, z->name);
636                         fw3_ipt_flush_chain(handle, chain);
637
638                         /* keep certain basic chains that do not depend on any settings to
639                            avoid purging unrelated user rules pointing to them */
640                         if (reload && !c->flag)
641                                 continue;
642
643                         fw3_ipt_delete_chain(handle, chain);
644                 }
645
646                 del(z->flags, handle->family, handle->table);
647         }
648 }
649
650 void
651 fw3_hotplug_zones(struct fw3_state *state, bool add)
652 {
653         struct fw3_zone *z;
654         struct fw3_device *d;
655
656         list_for_each_entry(z, &state->zones, list)
657         {
658                 if (add != hasbit(z->flags[0], FW3_FLAG_HOTPLUG))
659                 {
660                         list_for_each_entry(d, &z->devices, list)
661                                 fw3_hotplug(add, z, d);
662
663                         if (add)
664                                 setbit(z->flags[0], FW3_FLAG_HOTPLUG);
665                         else
666                                 delbit(z->flags[0], FW3_FLAG_HOTPLUG);
667                 }
668         }
669 }
670
671 struct fw3_zone *
672 fw3_lookup_zone(struct fw3_state *state, const char *name)
673 {
674         struct fw3_zone *z;
675
676         if (list_empty(&state->zones))
677                 return NULL;
678
679         list_for_each_entry(z, &state->zones, list)
680         {
681                 if (strcmp(z->name, name))
682                         continue;
683
684                 return z;
685         }
686
687         return NULL;
688 }
689
690 struct list_head *
691 fw3_resolve_zone_addresses(struct fw3_zone *zone, struct fw3_address *addr)
692 {
693         struct fw3_device *net;
694         struct fw3_address *cur, *tmp;
695         struct list_head *all;
696
697         all = calloc(1, sizeof(*all));
698         if (!all)
699                 return NULL;
700
701         INIT_LIST_HEAD(all);
702
703         if (addr && addr->set)
704         {
705                 tmp = malloc(sizeof(*tmp));
706
707                 if (tmp)
708                 {
709                         *tmp = *addr;
710                         list_add_tail(&tmp->list, all);
711                 }
712         }
713         else
714         {
715                 list_for_each_entry(net, &zone->networks, list)
716                         fw3_ubus_address(all, net->name);
717
718                 list_for_each_entry(cur, &zone->subnets, list)
719                 {
720                         tmp = malloc(sizeof(*tmp));
721
722                         if (!tmp)
723                                 continue;
724
725                         *tmp = *cur;
726                         list_add_tail(&tmp->list, all);
727                 }
728         }
729
730         return all;
731 }