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