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