2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
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.
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.
23 #define C(f, tbl, tgt, name) \
24 { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_TARGET_##tgt, name }
27 enum fw3_family family;
29 enum fw3_target target;
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"),
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"),
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"),
48 C(V4, NAT, SNAT, "zone_%s_postrouting"),
49 C(V4, NAT, DNAT, "zone_%s_prerouting"),
51 C(ANY, RAW, NOTRACK, "zone_%s_notrack"),
54 const struct fw3_option fw3_zone_opts[] = {
55 FW3_OPT("name", string, zone, name),
56 FW3_OPT("family", family, zone, family),
58 FW3_LIST("network", device, zone, networks),
59 FW3_LIST("device", device, zone, devices),
60 FW3_LIST("subnet", address, zone, subnets),
62 FW3_OPT("input", target, zone, policy_input),
63 FW3_OPT("forward", target, zone, policy_forward),
64 FW3_OPT("output", target, zone, policy_output),
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),
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),
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),
78 FW3_OPT("log", bool, zone, log),
79 FW3_OPT("log_limit", limit, zone, log_limit),
86 print_chains(enum fw3_table table, enum fw3_family family,
87 const char *fmt, const char *name, uint16_t targets,
88 const struct chain *chains, int n)
92 const struct chain *c;
94 for (c = chains; n > 0; c++, n--)
96 if (!fw3_is_family(c, family))
99 if (c->table != table)
102 if ((c->target != FW3_TARGET_UNSPEC) && !hasbit(targets, c->target))
105 snprintf(cn, sizeof(cn), c->name, name);
115 check_policy(struct uci_element *e, enum fw3_target *pol, enum fw3_target def,
118 if (*pol == FW3_TARGET_UNSPEC)
120 warn_elem(e, "has no %s policy specified, using default", name);
123 else if (*pol > FW3_TARGET_DROP)
125 warn_elem(e, "has invalid %s policy, using default", name);
131 resolve_networks(struct uci_element *e, struct fw3_zone *zone)
133 struct fw3_device *net, *tmp;
135 list_for_each_entry(net, &zone->networks, list)
137 tmp = fw3_ubus_device(net->name);
141 warn_elem(e, "cannot resolve device of network '%s'", net->name);
145 list_add_tail(&tmp->list, &zone->devices);
152 struct fw3_zone *zone;
154 zone = malloc(sizeof(*zone));
159 memset(zone, 0, sizeof(*zone));
161 INIT_LIST_HEAD(&zone->networks);
162 INIT_LIST_HEAD(&zone->devices);
163 INIT_LIST_HEAD(&zone->subnets);
164 INIT_LIST_HEAD(&zone->masq_src);
165 INIT_LIST_HEAD(&zone->masq_dest);
167 zone->log_limit.rate = 10;
173 fw3_load_zones(struct fw3_state *state, struct uci_package *p)
175 struct uci_section *s;
176 struct uci_element *e;
177 struct fw3_zone *zone;
178 struct fw3_defaults *defs = &state->defaults;
180 INIT_LIST_HEAD(&state->zones);
182 uci_foreach_element(&p->sections, e)
184 s = uci_to_section(e);
186 if (strcmp(s->type, "zone"))
189 zone = fw3_alloc_zone();
194 fw3_parse_options(zone, fw3_zone_opts, s);
196 if (!zone->extra_dest)
197 zone->extra_dest = zone->extra_src;
199 if (!zone->name || !*zone->name)
201 warn_elem(e, "has no name - ignoring");
206 if (list_empty(&zone->networks) && list_empty(&zone->devices) &&
207 list_empty(&zone->subnets) && !zone->extra_src)
209 warn_elem(e, "has no device, network, subnet or extra options");
212 check_policy(e, &zone->policy_input, defs->policy_input, "input");
213 check_policy(e, &zone->policy_output, defs->policy_output, "output");
214 check_policy(e, &zone->policy_forward, defs->policy_forward, "forward");
216 resolve_networks(e, zone);
220 setbit(zone->dst_flags, FW3_TARGET_SNAT);
221 zone->conntrack = true;
224 setbit(zone->src_flags, zone->policy_input);
225 setbit(zone->dst_flags, zone->policy_output);
226 setbit(zone->dst_flags, zone->policy_forward);
228 list_add_tail(&zone->list, &state->zones);
234 print_zone_chain(enum fw3_table table, enum fw3_family family,
235 struct fw3_zone *zone, struct fw3_state *state)
239 if (!fw3_is_family(zone, family))
242 setbit(zone->dst_flags, family);
244 if (!zone->conntrack && !state->defaults.drop_invalid)
245 setbit(zone->dst_flags, FW3_TARGET_NOTRACK);
247 s = print_chains(table, family, ":%s - [0:0]\n", zone->name,
248 zone->src_flags, src_chains, ARRAY_SIZE(src_chains));
250 d = print_chains(table, family, ":%s - [0:0]\n", zone->name,
251 zone->dst_flags, dst_chains, ARRAY_SIZE(dst_chains));
255 info(" * Zone '%s'", zone->name);
256 fw3_set_running(zone, &state->running_zones);
261 print_interface_rule(enum fw3_table table, enum fw3_family family,
262 struct fw3_zone *zone, struct fw3_device *dev,
263 struct fw3_address *sub, bool disable_notrack)
267 #define jump_target(t) \
268 ((t == FW3_TARGET_REJECT) ? "reject" : fw3_flag_names[t])
270 if (table == FW3_TABLE_FILTER)
272 for (t = FW3_TARGET_ACCEPT; t <= FW3_TARGET_DROP; t++)
274 if (hasbit(zone->src_flags, t))
276 fw3_pr("-A zone_%s_src_%s", zone->name, fw3_flag_names[t]);
277 fw3_format_in_out(dev, NULL);
278 fw3_format_src_dest(sub, NULL);
279 fw3_format_extra(zone->extra_src);
280 fw3_pr(" -j %s\n", jump_target(t));
283 if (hasbit(zone->dst_flags, t))
285 fw3_pr("-A zone_%s_dest_%s", zone->name, fw3_flag_names[t]);
286 fw3_format_in_out(NULL, dev);
287 fw3_format_src_dest(NULL, sub);
288 fw3_format_extra(zone->extra_dest);
289 fw3_pr(" -j %s\n", jump_target(t));
293 fw3_pr("-A delegate_input");
294 fw3_format_in_out(dev, NULL);
295 fw3_format_src_dest(sub, NULL);
296 fw3_format_extra(zone->extra_src);
297 fw3_pr(" -j zone_%s_input\n", zone->name);
299 fw3_pr("-A delegate_forward");
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 zone_%s_forward\n", zone->name);
305 fw3_pr("-A delegate_output");
306 fw3_format_in_out(NULL, dev);
307 fw3_format_src_dest(NULL, sub);
308 fw3_format_extra(zone->extra_dest);
309 fw3_pr(" -j zone_%s_output\n", zone->name);
311 else if (table == FW3_TABLE_NAT)
313 if (hasbit(zone->dst_flags, FW3_TARGET_DNAT))
315 fw3_pr("-A delegate_prerouting");
316 fw3_format_in_out(dev, NULL);
317 fw3_format_src_dest(sub, NULL);
318 fw3_format_extra(zone->extra_src);
319 fw3_pr(" -j zone_%s_prerouting\n", zone->name);
322 if (hasbit(zone->dst_flags, FW3_TARGET_SNAT))
324 fw3_pr("-A delegate_postrouting");
325 fw3_format_in_out(NULL, dev);
326 fw3_format_src_dest(NULL, sub);
327 fw3_format_extra(zone->extra_dest);
328 fw3_pr(" -j zone_%s_postrouting\n", zone->name);
331 else if (table == FW3_TABLE_MANGLE)
338 fw3_format_in_out(NULL, dev);
339 fw3_format_src_dest(NULL, sub);
340 fw3_pr(" -p tcp --tcp-flags SYN,RST SYN");
341 fw3_format_limit(&zone->log_limit);
342 fw3_format_comment(zone->name, " (mtu_fix logging)");
343 fw3_pr(" -j LOG --log-prefix \"MSSFIX(%s): \"\n", zone->name);
347 fw3_format_in_out(NULL, dev);
348 fw3_format_src_dest(NULL, sub);
349 fw3_pr(" -p tcp --tcp-flags SYN,RST SYN");
350 fw3_format_comment(zone->name, " (mtu_fix)");
351 fw3_pr(" -j TCPMSS --clamp-mss-to-pmtu\n");
354 else if (table == FW3_TABLE_RAW)
356 if (!zone->conntrack && !disable_notrack)
358 fw3_pr("-A notrack");
359 fw3_format_in_out(dev, NULL);
360 fw3_format_src_dest(sub, NULL);
361 fw3_format_extra(zone->extra_src);
362 fw3_format_comment(zone->name, " (notrack)");
363 fw3_pr(" -j CT --notrack\n", zone->name);
369 print_interface_rules(enum fw3_table table, enum fw3_family family,
370 struct fw3_zone *zone, bool disable_notrack)
372 struct fw3_device *dev;
373 struct fw3_address *sub;
375 fw3_foreach(dev, &zone->devices)
376 fw3_foreach(sub, &zone->subnets)
378 if (!fw3_is_family(sub, family))
384 print_interface_rule(table, family, zone, dev, sub, disable_notrack);
389 print_zone_rule(enum fw3_table table, enum fw3_family family,
390 struct fw3_zone *zone, bool disable_notrack)
392 struct fw3_address *msrc;
393 struct fw3_address *mdest;
397 if (!fw3_is_family(zone, family))
402 case FW3_TABLE_FILTER:
403 fw3_pr("-A zone_%s_input -j zone_%s_src_%s\n",
404 zone->name, zone->name, fw3_flag_names[zone->policy_input]);
406 fw3_pr("-A zone_%s_forward -j zone_%s_dest_%s\n",
407 zone->name, zone->name, fw3_flag_names[zone->policy_forward]);
409 fw3_pr("-A zone_%s_output -j zone_%s_dest_%s\n",
410 zone->name, zone->name, fw3_flag_names[zone->policy_output]);
414 for (t = FW3_TARGET_REJECT; t <= FW3_TARGET_DROP; t++)
416 if (hasbit(zone->src_flags, t))
418 fw3_pr("-A zone_%s_src_%s", zone->name, fw3_flag_names[t]);
419 fw3_format_limit(&zone->log_limit);
420 fw3_pr(" -j LOG --log-prefix \"%s(src %s)\"\n",
421 fw3_flag_names[t], zone->name);
424 if (hasbit(zone->dst_flags, t))
426 fw3_pr("-A zone_%s_dest_%s", zone->name, fw3_flag_names[t]);
427 fw3_format_limit(&zone->log_limit);
428 fw3_pr(" -j LOG --log-prefix \"%s(dest %s)\"\n",
429 fw3_flag_names[t], zone->name);
436 if (zone->masq && family == FW3_FAMILY_V4)
438 fw3_foreach(msrc, &zone->masq_src)
439 fw3_foreach(mdest, &zone->masq_dest)
441 fw3_pr("-A zone_%s_postrouting ", zone->name);
442 fw3_format_src_dest(msrc, mdest);
443 fw3_pr("-j MASQUERADE\n");
449 case FW3_TABLE_MANGLE:
453 print_interface_rules(table, family, zone, disable_notrack);
457 fw3_print_zone_chains(enum fw3_table table, enum fw3_family family,
458 struct fw3_state *state)
460 struct fw3_zone *zone;
462 list_for_each_entry(zone, &state->zones, list)
463 print_zone_chain(table, family, zone, state);
467 fw3_print_zone_rules(enum fw3_table table, enum fw3_family family,
468 struct fw3_state *state)
470 struct fw3_zone *zone;
472 list_for_each_entry(zone, &state->zones, list)
473 print_zone_rule(table, family, zone, state->defaults.drop_invalid);
477 fw3_flush_zones(enum fw3_table table, enum fw3_family family,
478 bool pass2, struct fw3_state *state)
480 struct fw3_zone *z, *tmp;
481 int mask = (1 << FW3_FAMILY_V4) | (1 << FW3_FAMILY_V6);
483 list_for_each_entry_safe(z, tmp, &state->running_zones, running_list)
485 if (!hasbit(z->dst_flags, family))
488 print_chains(table, family, pass2 ? "-X %s\n" : "-F %s\n",
489 z->name, z->src_flags, src_chains, ARRAY_SIZE(src_chains));
491 print_chains(table, family, pass2 ? "-X %s\n" : "-F %s\n",
492 z->name, z->dst_flags, dst_chains, ARRAY_SIZE(dst_chains));
496 delbit(z->dst_flags, family);
498 if (!(z->dst_flags & mask))
499 fw3_set_running(z, NULL);
505 fw3_lookup_zone(struct fw3_state *state, const char *name, bool running)
509 if (list_empty(&state->zones))
512 list_for_each_entry(z, &state->zones, list)
514 if (strcmp(z->name, name))
517 if (!running || z->running_list.next)