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, fmt) \
24 { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_TARGET_##tgt, fmt }
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"),
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"),
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"),
39 C(V4
, NAT
, SNAT
, "zone_%1$s_postrouting"),
40 C(V4
, NAT
, DNAT
, "zone_%1$s_prerouting"),
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"),
46 C(V4
, NAT
, CUSTOM_CHAINS
, "prerouting_%1$s_rule"),
47 C(V4
, NAT
, CUSTOM_CHAINS
, "postrouting_%1$s_rule"),
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"
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
)),
62 C(V4
, NAT
, CUSTOM_CHAINS
, R(prerouting
, prerouting
)),
63 C(V4
, NAT
, CUSTOM_CHAINS
, R(postrouting
, postrouting
)),
68 const struct fw3_option fw3_zone_opts
[] = {
69 FW3_OPT("enabled", bool, zone
, enabled
),
71 FW3_OPT("name", string
, zone
, name
),
72 FW3_OPT("family", family
, zone
, family
),
74 FW3_LIST("network", device
, zone
, networks
),
75 FW3_LIST("device", device
, zone
, devices
),
76 FW3_LIST("subnet", address
, zone
, subnets
),
78 FW3_OPT("input", target
, zone
, policy_input
),
79 FW3_OPT("forward", target
, zone
, policy_forward
),
80 FW3_OPT("output", target
, zone
, policy_output
),
82 FW3_OPT("masq", bool, zone
, masq
),
83 FW3_LIST("masq_src", address
, zone
, masq_src
),
84 FW3_LIST("masq_dest", address
, zone
, masq_dest
),
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
),
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
),
94 FW3_OPT("log", bool, zone
, log
),
95 FW3_OPT("log_limit", limit
, zone
, log_limit
),
102 check_policy(struct uci_element
*e
, enum fw3_target
*pol
, enum fw3_target def
,
105 if (*pol
== FW3_TARGET_UNSPEC
)
107 warn_elem(e
, "has no %s policy specified, using default", name
);
110 else if (*pol
> FW3_TARGET_DROP
)
112 warn_elem(e
, "has invalid %s policy, using default", name
);
118 resolve_networks(struct uci_element
*e
, struct fw3_zone
*zone
)
120 struct fw3_device
*net
, *tmp
;
122 list_for_each_entry(net
, &zone
->networks
, list
)
124 tmp
= fw3_ubus_device(net
->name
);
128 warn_elem(e
, "cannot resolve device of network '%s'", net
->name
);
132 list_add_tail(&tmp
->list
, &zone
->devices
);
139 struct fw3_zone
*zone
;
141 zone
= malloc(sizeof(*zone
));
146 memset(zone
, 0, sizeof(*zone
));
148 INIT_LIST_HEAD(&zone
->networks
);
149 INIT_LIST_HEAD(&zone
->devices
);
150 INIT_LIST_HEAD(&zone
->subnets
);
151 INIT_LIST_HEAD(&zone
->masq_src
);
152 INIT_LIST_HEAD(&zone
->masq_dest
);
154 zone
->enabled
= true;
155 zone
->custom_chains
= true;
156 zone
->log_limit
.rate
= 10;
162 fw3_load_zones(struct fw3_state
*state
, struct uci_package
*p
)
164 struct uci_section
*s
;
165 struct uci_element
*e
;
166 struct fw3_zone
*zone
;
167 struct fw3_defaults
*defs
= &state
->defaults
;
169 INIT_LIST_HEAD(&state
->zones
);
171 uci_foreach_element(&p
->sections
, e
)
173 s
= uci_to_section(e
);
175 if (strcmp(s
->type
, "zone"))
178 zone
= fw3_alloc_zone();
183 fw3_parse_options(zone
, fw3_zone_opts
, s
);
191 if (!zone
->extra_dest
)
192 zone
->extra_dest
= zone
->extra_src
;
194 if (!defs
->custom_chains
&& zone
->custom_chains
)
195 zone
->custom_chains
= false;
197 if (!zone
->name
|| !*zone
->name
)
199 warn_elem(e
, "has no name - ignoring");
204 if (list_empty(&zone
->networks
) && list_empty(&zone
->devices
) &&
205 list_empty(&zone
->subnets
) && !zone
->extra_src
)
207 warn_elem(e
, "has no device, network, subnet or extra options");
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");
214 resolve_networks(e
, zone
);
218 setbit(zone
->flags
[0], FW3_TARGET_SNAT
);
219 zone
->conntrack
= true;
222 if (zone
->custom_chains
)
224 setbit(zone
->flags
[0], FW3_TARGET_SNAT
);
225 setbit(zone
->flags
[0], FW3_TARGET_DNAT
);
228 setbit(zone
->flags
[0], fw3_to_src_target(zone
->policy_input
));
229 setbit(zone
->flags
[0], zone
->policy_output
);
230 setbit(zone
->flags
[0], zone
->policy_forward
);
232 setbit(zone
->flags
[1], fw3_to_src_target(zone
->policy_input
));
233 setbit(zone
->flags
[1], zone
->policy_output
);
234 setbit(zone
->flags
[1], zone
->policy_forward
);
236 list_add_tail(&zone
->list
, &state
->zones
);
242 print_zone_chain(enum fw3_table table
, enum fw3_family family
,
243 struct fw3_zone
*zone
, bool reload
, struct fw3_state
*state
)
246 uint32_t custom_mask
= ~0;
248 if (!fw3_is_family(zone
, family
))
251 set(zone
->flags
, family
, table
);
253 /* Don't touch user chains on reload */
255 delbit(custom_mask
, FW3_TARGET_CUSTOM_CHAINS
);
257 if (zone
->custom_chains
)
258 set(zone
->flags
, family
, FW3_TARGET_CUSTOM_CHAINS
);
260 if (!zone
->conntrack
&& !state
->defaults
.drop_invalid
)
261 set(zone
->flags
, family
, FW3_TARGET_NOTRACK
);
263 c
= fw3_pr_rulespec(table
, family
, zone
->flags
, custom_mask
, zone_chains
,
264 ":%s - [0:0]\n", zone
->name
);
266 r
= fw3_pr_rulespec(table
, family
, zone
->flags
, 0, zone_rules
,
267 "-A %s\n", zone
->name
);
271 info(" * Zone '%s'", zone
->name
);
272 fw3_set_running(zone
, &state
->running_zones
);
274 set(zone
->flags
, family
, table
);
279 print_interface_rule(enum fw3_table table
, enum fw3_family family
,
280 struct fw3_zone
*zone
, struct fw3_device
*dev
,
281 struct fw3_address
*sub
, bool reload
, bool disable_notrack
)
285 #define jump_target(t) \
286 ((t == FW3_TARGET_REJECT) ? "reject" : fw3_flag_names[t])
288 if (table
== FW3_TABLE_FILTER
)
290 for (t
= FW3_TARGET_ACCEPT
; t
<= FW3_TARGET_DROP
; t
++)
292 if (has(zone
->flags
, family
, fw3_to_src_target(t
)))
294 fw3_pr("-A zone_%s_src_%s", zone
->name
, fw3_flag_names
[t
]);
295 fw3_format_in_out(dev
, NULL
);
296 fw3_format_src_dest(sub
, NULL
);
297 fw3_format_extra(zone
->extra_src
);
298 fw3_pr(" -j %s\n", jump_target(t
));
301 if (has(zone
->flags
, family
, t
))
303 fw3_pr("-A zone_%s_dest_%s", zone
->name
, fw3_flag_names
[t
]);
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 %s\n", jump_target(t
));
311 fw3_pr("-A delegate_input");
312 fw3_format_in_out(dev
, NULL
);
313 fw3_format_src_dest(sub
, NULL
);
314 fw3_format_extra(zone
->extra_src
);
315 fw3_pr(" -j zone_%s_input\n", zone
->name
);
317 fw3_pr("-A delegate_forward");
318 fw3_format_in_out(dev
, NULL
);
319 fw3_format_src_dest(sub
, NULL
);
320 fw3_format_extra(zone
->extra_src
);
321 fw3_pr(" -j zone_%s_forward\n", zone
->name
);
323 fw3_pr("-A delegate_output");
324 fw3_format_in_out(NULL
, dev
);
325 fw3_format_src_dest(NULL
, sub
);
326 fw3_format_extra(zone
->extra_dest
);
327 fw3_pr(" -j zone_%s_output\n", zone
->name
);
329 else if (table
== FW3_TABLE_NAT
)
331 if (has(zone
->flags
, family
, FW3_TARGET_DNAT
))
333 fw3_pr("-A delegate_prerouting");
334 fw3_format_in_out(dev
, NULL
);
335 fw3_format_src_dest(sub
, NULL
);
336 fw3_format_extra(zone
->extra_src
);
337 fw3_pr(" -j zone_%s_prerouting\n", zone
->name
);
340 if (has(zone
->flags
, family
, FW3_TARGET_SNAT
))
342 fw3_pr("-A delegate_postrouting");
343 fw3_format_in_out(NULL
, dev
);
344 fw3_format_src_dest(NULL
, sub
);
345 fw3_format_extra(zone
->extra_dest
);
346 fw3_pr(" -j zone_%s_postrouting\n", zone
->name
);
349 else if (table
== FW3_TABLE_MANGLE
)
356 fw3_format_in_out(NULL
, dev
);
357 fw3_format_src_dest(NULL
, sub
);
358 fw3_pr(" -p tcp --tcp-flags SYN,RST SYN");
359 fw3_format_limit(&zone
->log_limit
);
360 fw3_format_comment(zone
->name
, " (mtu_fix logging)");
361 fw3_pr(" -j LOG --log-prefix \"MSSFIX(%s): \"\n", zone
->name
);
365 fw3_format_in_out(NULL
, dev
);
366 fw3_format_src_dest(NULL
, sub
);
367 fw3_pr(" -p tcp --tcp-flags SYN,RST SYN");
368 fw3_format_comment(zone
->name
, " (mtu_fix)");
369 fw3_pr(" -j TCPMSS --clamp-mss-to-pmtu\n");
372 else if (table
== FW3_TABLE_RAW
)
374 if (!zone
->conntrack
&& !disable_notrack
)
376 fw3_pr("-A notrack");
377 fw3_format_in_out(dev
, NULL
);
378 fw3_format_src_dest(sub
, NULL
);
379 fw3_format_extra(zone
->extra_src
);
380 fw3_format_comment(zone
->name
, " (notrack)");
381 fw3_pr(" -j CT --notrack\n", zone
->name
);
387 print_interface_rules(enum fw3_table table
, enum fw3_family family
,
388 struct fw3_zone
*zone
, bool reload
, bool disable_notrack
)
390 struct fw3_device
*dev
;
391 struct fw3_address
*sub
;
393 fw3_foreach(dev
, &zone
->devices
)
394 fw3_foreach(sub
, &zone
->subnets
)
396 if (!fw3_is_family(sub
, family
))
402 print_interface_rule(table
, family
, zone
, dev
, sub
, reload
, disable_notrack
);
407 print_zone_rule(enum fw3_table table
, enum fw3_family family
,
408 struct fw3_zone
*zone
, bool reload
, bool disable_notrack
)
410 struct fw3_address
*msrc
;
411 struct fw3_address
*mdest
;
415 if (!fw3_is_family(zone
, family
))
420 case FW3_TABLE_FILTER
:
421 fw3_pr("-A zone_%s_input -j zone_%s_src_%s\n",
422 zone
->name
, zone
->name
, fw3_flag_names
[zone
->policy_input
]);
424 fw3_pr("-A zone_%s_forward -j zone_%s_dest_%s\n",
425 zone
->name
, zone
->name
, fw3_flag_names
[zone
->policy_forward
]);
427 fw3_pr("-A zone_%s_output -j zone_%s_dest_%s\n",
428 zone
->name
, zone
->name
, fw3_flag_names
[zone
->policy_output
]);
432 for (t
= FW3_TARGET_REJECT
; t
<= FW3_TARGET_DROP
; t
++)
434 if (has(zone
->flags
, family
, fw3_to_src_target(t
)))
436 fw3_pr("-A zone_%s_src_%s", zone
->name
, fw3_flag_names
[t
]);
437 fw3_format_limit(&zone
->log_limit
);
438 fw3_pr(" -j LOG --log-prefix \"%s(src %s)\"\n",
439 fw3_flag_names
[t
], zone
->name
);
442 if (has(zone
->flags
, family
, t
))
444 fw3_pr("-A zone_%s_dest_%s", zone
->name
, fw3_flag_names
[t
]);
445 fw3_format_limit(&zone
->log_limit
);
446 fw3_pr(" -j LOG --log-prefix \"%s(dest %s)\"\n",
447 fw3_flag_names
[t
], zone
->name
);
454 if (zone
->masq
&& family
== FW3_FAMILY_V4
)
456 fw3_foreach(msrc
, &zone
->masq_src
)
457 fw3_foreach(mdest
, &zone
->masq_dest
)
459 fw3_pr("-A zone_%s_postrouting ", zone
->name
);
460 fw3_format_src_dest(msrc
, mdest
);
461 fw3_pr("-j MASQUERADE\n");
467 case FW3_TABLE_MANGLE
:
471 print_interface_rules(table
, family
, zone
, reload
, disable_notrack
);
475 fw3_print_zone_chains(enum fw3_table table
, enum fw3_family family
,
476 bool reload
, struct fw3_state
*state
)
478 struct fw3_zone
*zone
;
480 list_for_each_entry(zone
, &state
->zones
, list
)
481 print_zone_chain(table
, family
, zone
, reload
, state
);
485 fw3_print_zone_rules(enum fw3_table table
, enum fw3_family family
,
486 bool reload
, struct fw3_state
*state
)
488 struct fw3_zone
*zone
;
490 list_for_each_entry(zone
, &state
->zones
, list
)
491 print_zone_rule(table
, family
, zone
, reload
, state
->defaults
.drop_invalid
);
495 fw3_flush_zones(enum fw3_table table
, enum fw3_family family
,
496 bool pass2
, bool reload
, struct fw3_state
*state
)
498 struct fw3_zone
*z
, *tmp
;
499 uint32_t custom_mask
= ~0;
501 /* don't touch user chains on selective stop */
503 delbit(custom_mask
, FW3_TARGET_CUSTOM_CHAINS
);
505 list_for_each_entry_safe(z
, tmp
, &state
->running_zones
, running_list
)
507 if (!has(z
->flags
, family
, table
))
510 fw3_pr_rulespec(table
, family
, z
->flags
, custom_mask
, zone_chains
,
511 pass2
? "-X %s\n" : "-F %s\n", z
->name
);
515 del(z
->flags
, family
, table
);
521 fw3_lookup_zone(struct fw3_state
*state
, const char *name
, bool running
)
525 if (list_empty(&state
->zones
))
528 list_for_each_entry(z
, &state
->zones
, list
)
530 if (strcmp(z
->name
, name
))
533 if (!running
|| z
->running_list
.next
)