Add common fw3_address_to_string() helper function
[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_rule_spec zone_chains[] = {
27         C(ANY, FILTER, UNSPEC,        "zone_%1$s_input"),
28         C(ANY, FILTER, UNSPEC,        "zone_%1$s_output"),
29         C(ANY, FILTER, UNSPEC,        "zone_%1$s_forward"),
30
31         C(ANY, FILTER, SRC_ACCEPT,    "zone_%1$s_src_ACCEPT"),
32         C(ANY, FILTER, SRC_REJECT,    "zone_%1$s_src_REJECT"),
33         C(ANY, FILTER, SRC_DROP,      "zone_%1$s_src_DROP"),
34
35         C(ANY, FILTER, ACCEPT,        "zone_%1$s_dest_ACCEPT"),
36         C(ANY, FILTER, REJECT,        "zone_%1$s_dest_REJECT"),
37         C(ANY, FILTER, DROP,          "zone_%1$s_dest_DROP"),
38
39         C(V4,  NAT,    SNAT,          "zone_%1$s_postrouting"),
40         C(V4,  NAT,    DNAT,          "zone_%1$s_prerouting"),
41
42         C(ANY, FILTER, CUSTOM_CHAINS, "input_%1$s_rule"),
43         C(ANY, FILTER, CUSTOM_CHAINS, "output_%1$s_rule"),
44         C(ANY, FILTER, CUSTOM_CHAINS, "forwarding_%1$s_rule"),
45
46         C(V4,  NAT,    CUSTOM_CHAINS, "prerouting_%1$s_rule"),
47         C(V4,  NAT,    CUSTOM_CHAINS, "postrouting_%1$s_rule"),
48
49         { }
50 };
51
52
53 #define R(dir1, dir2) \
54         "zone_%1$s_" #dir1 " -m comment --comment \"user chain for %1$s " \
55         #dir2 "\" -j " #dir2 "_%1$s_rule"
56
57 static const struct fw3_rule_spec zone_rules[] = {
58         C(ANY, FILTER, CUSTOM_CHAINS, R(input, input)),
59         C(ANY, FILTER, CUSTOM_CHAINS, R(output, output)),
60         C(ANY, FILTER, CUSTOM_CHAINS, R(forward, forwarding)),
61
62         C(V4,  NAT,    CUSTOM_CHAINS, R(prerouting, prerouting)),
63         C(V4,  NAT,    CUSTOM_CHAINS, R(postrouting, postrouting)),
64
65         { }
66 };
67
68 const struct fw3_option fw3_zone_opts[] = {
69         FW3_OPT("enabled",             bool,     zone,     enabled),
70
71         FW3_OPT("name",                string,   zone,     name),
72         FW3_OPT("family",              family,   zone,     family),
73
74         FW3_LIST("network",            device,   zone,     networks),
75         FW3_LIST("device",             device,   zone,     devices),
76         FW3_LIST("subnet",             network,  zone,     subnets),
77
78         FW3_OPT("input",               target,   zone,     policy_input),
79         FW3_OPT("forward",             target,   zone,     policy_forward),
80         FW3_OPT("output",              target,   zone,     policy_output),
81
82         FW3_OPT("masq",                bool,     zone,     masq),
83         FW3_LIST("masq_src",           network,  zone,     masq_src),
84         FW3_LIST("masq_dest",          network,  zone,     masq_dest),
85
86         FW3_OPT("extra",               string,   zone,     extra_src),
87         FW3_OPT("extra_src",           string,   zone,     extra_src),
88         FW3_OPT("extra_dest",          string,   zone,     extra_dest),
89
90         FW3_OPT("conntrack",           bool,     zone,     conntrack),
91         FW3_OPT("mtu_fix",             bool,     zone,     mtu_fix),
92         FW3_OPT("custom_chains",       bool,     zone,     custom_chains),
93
94         FW3_OPT("log",                 bool,     zone,     log),
95         FW3_OPT("log_limit",           limit,    zone,     log_limit),
96
97         FW3_OPT("__flags_v4",          int,      zone,     flags[0]),
98         FW3_OPT("__flags_v6",          int,      zone,     flags[1]),
99
100         { }
101 };
102
103
104 static void
105 check_policy(struct uci_element *e, enum fw3_flag *pol, enum fw3_flag def,
106              const char *name)
107 {
108         if (*pol == FW3_FLAG_UNSPEC)
109         {
110                 warn_elem(e, "has no %s policy specified, using default", name);
111                 *pol = def;
112         }
113         else if (*pol > FW3_FLAG_DROP)
114         {
115                 warn_elem(e, "has invalid %s policy, using default", name);
116                 *pol = def;
117         }
118 }
119
120 static void
121 resolve_networks(struct uci_element *e, struct fw3_zone *zone)
122 {
123         struct fw3_device *net, *tmp;
124
125         list_for_each_entry(net, &zone->networks, list)
126         {
127                 tmp = fw3_ubus_device(net->name);
128
129                 if (!tmp)
130                 {
131                         warn_elem(e, "cannot resolve device of network '%s'", net->name);
132                         continue;
133                 }
134
135                 tmp->network = net;
136                 list_add_tail(&tmp->list, &zone->devices);
137         }
138 }
139
140 struct fw3_zone *
141 fw3_alloc_zone(void)
142 {
143         struct fw3_zone *zone;
144
145         zone = malloc(sizeof(*zone));
146
147         if (!zone)
148                 return NULL;
149
150         memset(zone, 0, sizeof(*zone));
151
152         INIT_LIST_HEAD(&zone->networks);
153         INIT_LIST_HEAD(&zone->devices);
154         INIT_LIST_HEAD(&zone->subnets);
155         INIT_LIST_HEAD(&zone->masq_src);
156         INIT_LIST_HEAD(&zone->masq_dest);
157
158         zone->enabled = true;
159         zone->custom_chains = true;
160         zone->log_limit.rate = 10;
161
162         return zone;
163 }
164
165 void
166 fw3_load_zones(struct fw3_state *state, struct uci_package *p)
167 {
168         struct uci_section *s;
169         struct uci_element *e;
170         struct fw3_zone *zone;
171         struct fw3_defaults *defs = &state->defaults;
172
173         INIT_LIST_HEAD(&state->zones);
174
175         uci_foreach_element(&p->sections, e)
176         {
177                 s = uci_to_section(e);
178
179                 if (strcmp(s->type, "zone"))
180                         continue;
181
182                 zone = fw3_alloc_zone();
183
184                 if (!zone)
185                         continue;
186
187                 fw3_parse_options(zone, fw3_zone_opts, s);
188
189                 if (!zone->enabled)
190                 {
191                         fw3_free_zone(zone);
192                         continue;
193                 }
194
195                 if (!zone->extra_dest)
196                         zone->extra_dest = zone->extra_src;
197
198                 if (!defs->custom_chains && zone->custom_chains)
199                         zone->custom_chains = false;
200
201                 if (!zone->name || !*zone->name)
202                 {
203                         warn_elem(e, "has no name - ignoring");
204                         fw3_free_zone(zone);
205                         continue;
206                 }
207
208                 if (list_empty(&zone->networks) && list_empty(&zone->devices) &&
209                     list_empty(&zone->subnets) && !zone->extra_src)
210                 {
211                         warn_elem(e, "has no device, network, subnet or extra options");
212                 }
213
214                 check_policy(e, &zone->policy_input, defs->policy_input, "input");
215                 check_policy(e, &zone->policy_output, defs->policy_output, "output");
216                 check_policy(e, &zone->policy_forward, defs->policy_forward, "forward");
217
218                 resolve_networks(e, zone);
219
220                 if (zone->masq)
221                 {
222                         setbit(zone->flags[0], FW3_FLAG_SNAT);
223                         zone->conntrack = true;
224                 }
225
226                 if (zone->custom_chains)
227                 {
228                         setbit(zone->flags[0], FW3_FLAG_SNAT);
229                         setbit(zone->flags[0], FW3_FLAG_DNAT);
230                 }
231
232                 setbit(zone->flags[0], fw3_to_src_target(zone->policy_input));
233                 setbit(zone->flags[0], zone->policy_output);
234                 setbit(zone->flags[0], zone->policy_forward);
235
236                 setbit(zone->flags[1], fw3_to_src_target(zone->policy_input));
237                 setbit(zone->flags[1], zone->policy_output);
238                 setbit(zone->flags[1], zone->policy_forward);
239
240                 list_add_tail(&zone->list, &state->zones);
241         }
242 }
243
244
245 static void
246 print_zone_chain(struct fw3_state *state, enum fw3_family family,
247                  enum fw3_table table, bool reload, struct fw3_zone *zone)
248 {
249         bool c, r;
250         uint32_t custom_mask = ~0;
251
252         if (!fw3_is_family(zone, family))
253                 return;
254
255         set(zone->flags, family, table);
256
257         /* Don't touch user chains on reload */
258         if (reload)
259                 delbit(custom_mask, FW3_FLAG_CUSTOM_CHAINS);
260
261         if (zone->custom_chains)
262                 set(zone->flags, family, FW3_FLAG_CUSTOM_CHAINS);
263
264         if (!zone->conntrack && !state->defaults.drop_invalid)
265                 set(zone->flags, family, FW3_FLAG_NOTRACK);
266
267         c = fw3_pr_rulespec(table, family, zone->flags, custom_mask, zone_chains,
268                             ":%s - [0:0]\n", zone->name);
269
270         r = fw3_pr_rulespec(table, family, zone->flags, 0, zone_rules,
271                             "-A %s\n", zone->name);
272
273         if (c || r)
274         {
275                 info("   * Zone '%s'", zone->name);
276
277                 set(zone->flags, family, table);
278         }
279 }
280
281 static void
282 print_interface_rule(struct fw3_state *state, enum fw3_family family,
283                      enum fw3_table table, bool reload, struct fw3_zone *zone,
284                      struct fw3_device *dev, struct fw3_address *sub)
285 {
286         bool disable_notrack = state->defaults.drop_invalid;
287
288         enum fw3_flag t;
289
290 #define jump_target(t) \
291         ((t == FW3_FLAG_REJECT) ? "reject" : fw3_flag_names[t])
292
293         if (table == FW3_TABLE_FILTER)
294         {
295                 for (t = FW3_FLAG_ACCEPT; t <= FW3_FLAG_DROP; t++)
296                 {
297                         if (has(zone->flags, family, fw3_to_src_target(t)))
298                         {
299                                 fw3_pr("-A zone_%s_src_%s", zone->name, fw3_flag_names[t]);
300                                 fw3_format_in_out(dev, NULL);
301                                 fw3_format_src_dest(sub, NULL);
302                                 fw3_format_extra(zone->extra_src);
303                                 fw3_pr(" -j %s\n", jump_target(t));
304                         }
305
306                         if (has(zone->flags, family, t))
307                         {
308                                 fw3_pr("-A zone_%s_dest_%s", zone->name, fw3_flag_names[t]);
309                                 fw3_format_in_out(NULL, dev);
310                                 fw3_format_src_dest(NULL, sub);
311                                 fw3_format_extra(zone->extra_dest);
312                                 fw3_pr(" -j %s\n", jump_target(t));
313                         }
314                 }
315
316                 fw3_pr("-A delegate_input");
317                 fw3_format_in_out(dev, NULL);
318                 fw3_format_src_dest(sub, NULL);
319                 fw3_format_extra(zone->extra_src);
320                 fw3_pr(" -j zone_%s_input\n", zone->name);
321
322                 fw3_pr("-A delegate_forward");
323                 fw3_format_in_out(dev, NULL);
324                 fw3_format_src_dest(sub, NULL);
325                 fw3_format_extra(zone->extra_src);
326                 fw3_pr(" -j zone_%s_forward\n", zone->name);
327
328                 fw3_pr("-A delegate_output");
329                 fw3_format_in_out(NULL, dev);
330                 fw3_format_src_dest(NULL, sub);
331                 fw3_format_extra(zone->extra_dest);
332                 fw3_pr(" -j zone_%s_output\n", zone->name);
333         }
334         else if (table == FW3_TABLE_NAT)
335         {
336                 if (has(zone->flags, family, FW3_FLAG_DNAT))
337                 {
338                         fw3_pr("-A delegate_prerouting");
339                         fw3_format_in_out(dev, NULL);
340                         fw3_format_src_dest(sub, NULL);
341                         fw3_format_extra(zone->extra_src);
342                         fw3_pr(" -j zone_%s_prerouting\n", zone->name);
343                 }
344
345                 if (has(zone->flags, family, FW3_FLAG_SNAT))
346                 {
347                         fw3_pr("-A delegate_postrouting");
348                         fw3_format_in_out(NULL, dev);
349                         fw3_format_src_dest(NULL, sub);
350                         fw3_format_extra(zone->extra_dest);
351                         fw3_pr(" -j zone_%s_postrouting\n", zone->name);
352                 }
353         }
354         else if (table == FW3_TABLE_MANGLE)
355         {
356                 if (zone->mtu_fix)
357                 {
358                         if (zone->log)
359                         {
360                                 fw3_pr("-A mssfix");
361                                 fw3_format_in_out(NULL, dev);
362                                 fw3_format_src_dest(NULL, sub);
363                                 fw3_pr(" -p tcp --tcp-flags SYN,RST SYN");
364                                 fw3_format_limit(&zone->log_limit);
365                                 fw3_format_comment(zone->name, " (mtu_fix logging)");
366                                 fw3_pr(" -j LOG --log-prefix \"MSSFIX(%s): \"\n", zone->name);
367                         }
368
369                         fw3_pr("-A mssfix");
370                         fw3_format_in_out(NULL, dev);
371                         fw3_format_src_dest(NULL, sub);
372                         fw3_pr(" -p tcp --tcp-flags SYN,RST SYN");
373                         fw3_format_comment(zone->name, " (mtu_fix)");
374                         fw3_pr(" -j TCPMSS --clamp-mss-to-pmtu\n");
375                 }
376         }
377         else if (table == FW3_TABLE_RAW)
378         {
379                 if (!zone->conntrack && !disable_notrack)
380                 {
381                         fw3_pr("-A notrack");
382                         fw3_format_in_out(dev, NULL);
383                         fw3_format_src_dest(sub, NULL);
384                         fw3_format_extra(zone->extra_src);
385                         fw3_format_comment(zone->name, " (notrack)");
386                         fw3_pr(" -j CT --notrack\n", zone->name);
387                 }
388         }
389 }
390
391 static void
392 print_interface_rules(struct fw3_state *state, enum fw3_family family,
393                       enum fw3_table table, bool reload, struct fw3_zone *zone)
394 {
395         struct fw3_device *dev;
396         struct fw3_address *sub;
397
398         fw3_foreach(dev, &zone->devices)
399         fw3_foreach(sub, &zone->subnets)
400         {
401                 if (!fw3_is_family(sub, family))
402                         continue;
403
404                 if (!dev && !sub)
405                         continue;
406
407                 print_interface_rule(state, family, table, reload, zone, dev, sub);
408         }
409 }
410
411 static void
412 print_zone_rule(struct fw3_state *state, enum fw3_family family,
413                 enum fw3_table table, bool reload, struct fw3_zone *zone)
414 {
415         struct fw3_address *msrc;
416         struct fw3_address *mdest;
417
418         enum fw3_flag t;
419
420         if (!fw3_is_family(zone, family))
421                 return;
422
423         switch (table)
424         {
425         case FW3_TABLE_FILTER:
426                 fw3_pr("-A zone_%s_input -j zone_%s_src_%s\n",
427                            zone->name, zone->name, fw3_flag_names[zone->policy_input]);
428
429                 fw3_pr("-A zone_%s_forward -j zone_%s_dest_%s\n",
430                            zone->name, zone->name, fw3_flag_names[zone->policy_forward]);
431
432                 fw3_pr("-A zone_%s_output -j zone_%s_dest_%s\n",
433                            zone->name, zone->name, fw3_flag_names[zone->policy_output]);
434
435                 if (zone->log)
436                 {
437                         for (t = FW3_FLAG_REJECT; t <= FW3_FLAG_DROP; t++)
438                         {
439                                 if (has(zone->flags, family, fw3_to_src_target(t)))
440                                 {
441                                         fw3_pr("-A zone_%s_src_%s", zone->name, fw3_flag_names[t]);
442                                         fw3_format_limit(&zone->log_limit);
443                                         fw3_pr(" -j LOG --log-prefix \"%s(src %s)\"\n",
444                                                    fw3_flag_names[t], zone->name);
445                                 }
446
447                                 if (has(zone->flags, family, t))
448                                 {
449                                         fw3_pr("-A zone_%s_dest_%s", zone->name, fw3_flag_names[t]);
450                                         fw3_format_limit(&zone->log_limit);
451                                         fw3_pr(" -j LOG --log-prefix \"%s(dest %s)\"\n",
452                                                    fw3_flag_names[t], zone->name);
453                                 }
454                         }
455                 }
456                 break;
457
458         case FW3_TABLE_NAT:
459                 if (zone->masq && family == FW3_FAMILY_V4)
460                 {
461                         fw3_foreach(msrc, &zone->masq_src)
462                         fw3_foreach(mdest, &zone->masq_dest)
463                         {
464                                 if (!fw3_is_family(msrc, family) ||
465                                     !fw3_is_family(mdest, family))
466                                         continue;
467
468                                 fw3_pr("-A zone_%s_postrouting", zone->name);
469                                 fw3_format_src_dest(msrc, mdest);
470                                 fw3_pr(" -j MASQUERADE\n");
471                         }
472                 }
473                 break;
474
475         case FW3_TABLE_RAW:
476         case FW3_TABLE_MANGLE:
477                 break;
478         }
479
480         print_interface_rules(state, family, table, reload, zone);
481 }
482
483 void
484 fw3_print_zone_chains(struct fw3_state *state, enum fw3_family family,
485                       enum fw3_table table, bool reload)
486 {
487         struct fw3_zone *zone;
488
489         list_for_each_entry(zone, &state->zones, list)
490                 print_zone_chain(state, family, table, reload, zone);
491 }
492
493 void
494 fw3_print_zone_rules(struct fw3_state *state, enum fw3_family family,
495                      enum fw3_table table, bool reload)
496 {
497         struct fw3_zone *zone;
498
499         list_for_each_entry(zone, &state->zones, list)
500                 print_zone_rule(state, family, table, reload, zone);
501 }
502
503 void
504 fw3_flush_zones(struct fw3_state *state, enum fw3_family family,
505                 enum fw3_table table, bool reload, bool pass2)
506 {
507         struct fw3_zone *z, *tmp;
508         uint32_t custom_mask = ~0;
509
510         /* don't touch user chains on selective stop */
511         if (reload)
512                 delbit(custom_mask, FW3_FLAG_CUSTOM_CHAINS);
513
514         list_for_each_entry_safe(z, tmp, &state->zones, list)
515         {
516                 if (!has(z->flags, family, table))
517                         continue;
518
519                 fw3_pr_rulespec(table, family, z->flags, custom_mask, zone_chains,
520                                 pass2 ? "-X %s\n" : "-F %s\n", z->name);
521
522                 if (pass2)
523                         del(z->flags, family, table);
524         }
525 }
526
527 void
528 fw3_hotplug_zones(struct fw3_state *state, bool add)
529 {
530         struct fw3_zone *z;
531         struct fw3_device *d;
532
533         if (add)
534         {
535                 list_for_each_entry(z, &state->zones, list)
536                 {
537                         if (!hasbit(z->flags[0], FW3_FLAG_HOTPLUG))
538                         {
539                                 list_for_each_entry(d, &z->devices, list)
540                                         fw3_hotplug(add, z, d);
541
542                                 setbit(z->flags[0], FW3_FLAG_HOTPLUG);
543                         }
544                 }
545         }
546         else
547         {
548                 list_for_each_entry(z, &state->zones, list)
549                 {
550                         if (hasbit(z->flags[0], FW3_FLAG_HOTPLUG))
551                         {
552                                 list_for_each_entry(d, &z->devices, list)
553                                         fw3_hotplug(add, z, d);
554
555                                 delbit(z->flags[0], FW3_FLAG_HOTPLUG);
556                         }
557                 }
558         }
559 }
560
561 struct fw3_zone *
562 fw3_lookup_zone(struct fw3_state *state, const char *name)
563 {
564         struct fw3_zone *z;
565
566         if (list_empty(&state->zones))
567                 return NULL;
568
569         list_for_each_entry(z, &state->zones, list)
570         {
571                 if (strcmp(z->name, name))
572                         continue;
573
574                 return z;
575         }
576
577         return NULL;
578 }
579
580 void
581 fw3_free_zone(struct fw3_zone *zone)
582 {
583         struct fw3_device *dev, *tmp;
584
585         list_for_each_entry_safe(dev, tmp, &zone->devices, list)
586         {
587                 list_del(&dev->list);
588                 free(dev);
589         }
590
591         list_for_each_entry_safe(dev, tmp, &zone->networks, list)
592         {
593                 list_del(&dev->list);
594                 free(dev);
595         }
596
597         fw3_free_object(zone, fw3_zone_opts);
598 }