#include "defaults.h"
-#define C(f, tbl, def, name) \
- { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_DEFAULT_##def, name }
-
-struct chain {
- enum fw3_family family;
- enum fw3_table table;
- enum fw3_default flag;
- const char *name;
-};
+#define C(f, tbl, def, fmt) \
+ { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_FLAG_##def, fmt }
-static const struct chain default_chains[] = {
+static const struct fw3_rule_spec default_chains[] = {
C(ANY, FILTER, UNSPEC, "delegate_input"),
C(ANY, FILTER, UNSPEC, "delegate_output"),
C(ANY, FILTER, UNSPEC, "delegate_forward"),
+ C(ANY, FILTER, UNSPEC, "reject"),
C(ANY, FILTER, CUSTOM_CHAINS, "input_rule"),
C(ANY, FILTER, CUSTOM_CHAINS, "output_rule"),
C(ANY, FILTER, CUSTOM_CHAINS, "forwarding_rule"),
- C(ANY, FILTER, UNSPEC, "reject"),
C(ANY, FILTER, SYN_FLOOD, "syn_flood"),
C(V4, NAT, UNSPEC, "delegate_prerouting"),
C(V4, NAT, CUSTOM_CHAINS, "postrouting_rule"),
C(ANY, MANGLE, UNSPEC, "mssfix"),
+ C(ANY, MANGLE, UNSPEC, "fwmark"),
+
C(ANY, RAW, UNSPEC, "notrack"),
+
+ { }
};
-static const struct chain toplevel_rules[] = {
+static const struct fw3_rule_spec toplevel_rules[] = {
C(ANY, FILTER, UNSPEC, "INPUT -j delegate_input"),
C(ANY, FILTER, UNSPEC, "OUTPUT -j delegate_output"),
C(ANY, FILTER, UNSPEC, "FORWARD -j delegate_forward"),
C(V4, NAT, UNSPEC, "POSTROUTING -j delegate_postrouting"),
C(ANY, MANGLE, UNSPEC, "FORWARD -j mssfix"),
+ C(ANY, MANGLE, UNSPEC, "PREROUTING -j fwmark"),
+
C(ANY, RAW, UNSPEC, "PREROUTING -j notrack"),
+
+ { }
};
-static struct fw3_option default_opts[] = {
+const struct fw3_option fw3_flag_opts[] = {
FW3_OPT("input", target, defaults, policy_input),
FW3_OPT("forward", target, defaults, policy_forward),
FW3_OPT("output", target, defaults, policy_output),
FW3_OPT("tcp_syncookies", bool, defaults, tcp_syncookies),
FW3_OPT("tcp_ecn", bool, defaults, tcp_ecn),
- FW3_OPT("tcp_westwood", bool, defaults, tcp_westwood),
FW3_OPT("tcp_window_scaling", bool, defaults, tcp_window_scaling),
FW3_OPT("accept_redirects", bool, defaults, accept_redirects),
FW3_OPT("custom_chains", bool, defaults, custom_chains),
FW3_OPT("disable_ipv6", bool, defaults, disable_ipv6),
-};
+ FW3_OPT("__flags_v4", int, defaults, flags[0]),
+ FW3_OPT("__flags_v6", int, defaults, flags[1]),
-static bool
-print_chains(enum fw3_table table, enum fw3_family family,
- const char *fmt, uint8_t flags,
- const struct chain *chains, int n)
-{
- bool rv = false;
- const struct chain *c;
-
- for (c = chains; n > 0; c++, n--)
- {
- if (!fw3_is_family(c, family))
- continue;
-
- if (c->table != table)
- continue;
-
- if ((c->flag != FW3_DEFAULT_UNSPEC) && !(flags & (1 << c->flag)))
- continue;
-
- fw3_pr(fmt, c->name);
-
- rv = true;
- }
+ { }
+};
- return rv;
-}
static void
-check_policy(struct uci_element *e, enum fw3_target *pol, const char *name)
+check_policy(struct uci_element *e, enum fw3_flag *pol, const char *name)
{
- if (*pol == FW3_TARGET_UNSPEC)
+ if (*pol == FW3_FLAG_UNSPEC)
{
warn_elem(e, "has no %s policy specified, defaulting to DROP", name);
- *pol = FW3_TARGET_DROP;
+ *pol = FW3_FLAG_DROP;
}
- else if (*pol > FW3_TARGET_DROP)
+ else if (*pol > FW3_FLAG_DROP)
{
warn_elem(e, "has invalid %s policy, defaulting to DROP", name);
- *pol = FW3_TARGET_DROP;
+ *pol = FW3_FLAG_DROP;
}
}
continue;
}
- fw3_parse_options(&state->defaults,
- default_opts, ARRAY_SIZE(default_opts), s);
+ fw3_parse_options(&state->defaults, fw3_flag_opts, s);
check_policy(e, &defs->policy_input, "input");
check_policy(e, &defs->policy_output, "output");
check_policy(e, &defs->policy_forward, "forward");
-
- if (defs->custom_chains)
- defs->has_flag |= (1 << FW3_DEFAULT_CUSTOM_CHAINS);
-
- if (defs->syn_flood)
- defs->has_flag |= (1 << FW3_DEFAULT_SYN_FLOOD);
}
}
void
-fw3_print_default_chains(enum fw3_table table, enum fw3_family family,
- struct fw3_state *state)
+fw3_print_default_chains(struct fw3_state *state, enum fw3_family family,
+ enum fw3_table table, bool reload)
{
+ bool rv;
struct fw3_defaults *defs = &state->defaults;
- const char *policy[] = {
- "(bug)",
- "ACCEPT",
- "DROP",
- "DROP",
- "(bug)",
- "(bug)",
- "(bug)",
- };
+ uint32_t custom_mask = ~0;
+
+#define policy(t) \
+ ((t == FW3_FLAG_REJECT) ? "DROP" : fw3_flag_names[t])
+
+ if (family == FW3_FAMILY_V6 && defs->disable_ipv6)
+ return;
if (table == FW3_TABLE_FILTER)
{
- fw3_pr(":INPUT %s [0:0]\n", policy[defs->policy_input]);
- fw3_pr(":FORWARD %s [0:0]\n", policy[defs->policy_forward]);
- fw3_pr(":OUTPUT %s [0:0]\n", policy[defs->policy_output]);
+ fw3_pr(":INPUT %s [0:0]\n", policy(defs->policy_input));
+ fw3_pr(":FORWARD %s [0:0]\n", policy(defs->policy_forward));
+ fw3_pr(":OUTPUT %s [0:0]\n", policy(defs->policy_output));
}
- print_chains(table, family, ":%s - [0:0]\n", defs->has_flag,
- default_chains, ARRAY_SIZE(default_chains));
+ /* Don't touch user chains on reload */
+ if (reload)
+ delbit(custom_mask, FW3_FLAG_CUSTOM_CHAINS);
+
+ if (defs->custom_chains)
+ set(defs->flags, family, FW3_FLAG_CUSTOM_CHAINS);
+
+ if (defs->syn_flood)
+ set(defs->flags, family, FW3_FLAG_SYN_FLOOD);
+
+ rv = fw3_pr_rulespec(table, family, defs->flags, custom_mask,
+ default_chains, ":%s - [0:0]\n");
+
+ if (rv)
+ set(defs->flags, family, table);
}
void
-fw3_print_default_head_rules(enum fw3_table table, enum fw3_family family,
- struct fw3_state *state)
+fw3_print_default_head_rules(struct fw3_state *state, enum fw3_family family,
+ enum fw3_table table, bool reload)
{
int i;
struct fw3_defaults *defs = &state->defaults;
const char *chains[] = {
- "input",
- "output",
- "forward",
+ "input", "input",
+ "output", "output",
+ "forward", "forwarding",
};
- print_chains(table, family, "-A %s\n", 0,
- toplevel_rules, ARRAY_SIZE(toplevel_rules));
+ fw3_pr_rulespec(table, family, NULL, 0, toplevel_rules, "-A %s\n");
switch (table)
{
if (defs->custom_chains)
{
- fw3_pr("-A delegate_input -j input_rule\n");
- fw3_pr("-A delegate_output -j output_rule\n");
- fw3_pr("-A delegate_forward -j forwarding_rule\n");
+ for (i = 0; i < ARRAY_SIZE(chains); i += 2)
+ {
+ fw3_pr("-A delegate_%s -m comment "
+ "--comment \"user chain for %s\" -j %s_rule\n",
+ chains[i], chains[i+1], chains[i+1]);
+ }
}
- for (i = 0; i < ARRAY_SIZE(chains); i++)
+ for (i = 0; i < ARRAY_SIZE(chains); i += 2)
{
fw3_pr("-A delegate_%s -m conntrack --ctstate RELATED,ESTABLISHED "
"-j ACCEPT\n", chains[i]);
case FW3_TABLE_NAT:
if (defs->custom_chains)
{
- fw3_pr("-A delegate_prerouting -j prerouting_rule\n");
- fw3_pr("-A delegate_postrouting -j postrouting_rule\n");
+ fw3_pr("-A delegate_prerouting "
+ "-m comment --comment \"user chain for prerouting\" "
+ "-j prerouting_rule\n");
+
+ fw3_pr("-A delegate_postrouting "
+ "-m comment --comment \"user chain for postrouting\" "
+ "-j postrouting_rule\n");
}
break;
}
void
-fw3_print_default_tail_rules(enum fw3_table table, enum fw3_family family,
- struct fw3_state *state)
+fw3_print_default_tail_rules(struct fw3_state *state, enum fw3_family family,
+ enum fw3_table table, bool reload)
{
struct fw3_defaults *defs = &state->defaults;
if (table != FW3_TABLE_FILTER)
return;
- if (defs->policy_input == FW3_TARGET_REJECT)
+ if (defs->policy_input == FW3_FLAG_REJECT)
fw3_pr("-A delegate_input -j reject\n");
- if (defs->policy_output == FW3_TARGET_REJECT)
+ if (defs->policy_output == FW3_FLAG_REJECT)
fw3_pr("-A delegate_output -j reject\n");
- if (defs->policy_forward == FW3_TARGET_REJECT)
+ if (defs->policy_forward == FW3_FLAG_REJECT)
fw3_pr("-A delegate_forward -j reject\n");
}
static void
-reset_policy(enum fw3_table table)
+set_default(const char *name, bool set)
+{
+ FILE *f;
+ char path[sizeof("/proc/sys/net/ipv4/tcp_window_scaling\0")];
+
+ snprintf(path, sizeof(path), "/proc/sys/net/ipv4/tcp_%s", name);
+
+ info(" * Set tcp_%s to %s", name, set ? "on" : "off", name);
+
+ if (!(f = fopen(path, "w")))
+ {
+ info(" ! Unable to write value: %s", strerror(errno));
+ return;
+ }
+
+ fprintf(f, "%u\n", set);
+ fclose(f);
+}
+
+void
+fw3_set_defaults(struct fw3_state *state)
+{
+ set_default("ecn", state->defaults.tcp_ecn);
+ set_default("syncookies", state->defaults.tcp_syncookies);
+ set_default("window_scaling", state->defaults.tcp_window_scaling);
+}
+
+static void
+reset_policy(enum fw3_table table, enum fw3_flag policy)
{
if (table != FW3_TABLE_FILTER)
return;
- fw3_pr(":INPUT ACCEPT [0:0]\n");
- fw3_pr(":OUTPUT ACCEPT [0:0]\n");
- fw3_pr(":FORWARD ACCEPT [0:0]\n");
+ fw3_pr(":INPUT %s [0:0]\n", fw3_flag_names[policy]);
+ fw3_pr(":OUTPUT %s [0:0]\n", fw3_flag_names[policy]);
+ fw3_pr(":FORWARD %s [0:0]\n", fw3_flag_names[policy]);
}
void
-fw3_flush_rules(enum fw3_table table, enum fw3_family family,
- bool pass2, struct list_head *statefile)
+fw3_flush_rules(struct fw3_state *state, enum fw3_family family,
+ enum fw3_table table, bool reload, bool pass2)
{
- struct fw3_statefile_entry *e;
+ struct fw3_defaults *defs = &state->defaults;
+ uint32_t custom_mask = ~0;
+
+ if (!has(defs->flags, family, table))
+ return;
+
+ /* don't touch user chains on selective stop */
+ if (reload)
+ delbit(custom_mask, FW3_FLAG_CUSTOM_CHAINS);
- list_for_each_entry(e, statefile, list)
+ if (!pass2)
{
- if (e->type != FW3_TYPE_DEFAULTS)
- continue;
+ reset_policy(table, reload ? FW3_FLAG_DROP : FW3_FLAG_ACCEPT);
- if (!pass2)
- {
- reset_policy(table);
+ fw3_pr_rulespec(table, family, defs->flags, custom_mask,
+ toplevel_rules, "-D %s\n");
- print_chains(table, family, "-D %s\n", e->flags[0],
- toplevel_rules, ARRAY_SIZE(toplevel_rules));
+ fw3_pr_rulespec(table, family, defs->flags, custom_mask,
+ default_chains, "-F %s\n");
+ }
+ else
+ {
+ fw3_pr_rulespec(table, family, defs->flags, custom_mask,
+ default_chains, "-X %s\n");
- print_chains(table, family, "-F %s\n", e->flags[0],
- default_chains, ARRAY_SIZE(default_chains));
- }
- else
- {
- print_chains(table, family, "-X %s\n", e->flags[0],
- default_chains, ARRAY_SIZE(default_chains));
- }
+ del(defs->flags, family, table);
}
}
void
fw3_flush_all(enum fw3_table table)
{
- reset_policy(table);
+ reset_policy(table, FW3_FLAG_ACCEPT);
fw3_pr("-F\n");
fw3_pr("-X\n");