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