X-Git-Url: http://git.archive.openwrt.org/?p=project%2Ffirewall3.git;a=blobdiff_plain;f=defaults.c;h=fc24d0b5294bb041431457f697aa5f45c5f8d849;hp=3e8ea40f164404fb6e2ccaabd2a80239c1e2959c;hb=c7fc65809ae5300f256a1228f7011a1dc1bc85ac;hpb=6e6afb0c7d4d69e4a48122664645c8de67f34c96 diff --git a/defaults.c b/defaults.c index 3e8ea40..fc24d0b 100644 --- a/defaults.c +++ b/defaults.c @@ -60,7 +60,7 @@ static const struct chain toplevel_rules[] = { C(ANY, RAW, UNSPEC, "PREROUTING -j notrack"), }; -static struct fw3_option default_opts[] = { +const struct fw3_option fw3_default_opts[] = { FW3_OPT("input", target, defaults, policy_input), FW3_OPT("forward", target, defaults, policy_forward), FW3_OPT("output", target, defaults, policy_output), @@ -74,7 +74,6 @@ static struct fw3_option default_opts[] = { 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), @@ -82,12 +81,14 @@ static struct fw3_option default_opts[] = { FW3_OPT("custom_chains", bool, defaults, custom_chains), FW3_OPT("disable_ipv6", bool, defaults, disable_ipv6), + + { } }; static bool print_chains(enum fw3_table table, enum fw3_family family, - const char *fmt, uint16_t flags, + const char *fmt, uint32_t flags, const struct chain *chains, int n) { bool rv = false; @@ -157,8 +158,7 @@ fw3_load_defaults(struct fw3_state *state, struct uci_package *p) continue; } - fw3_parse_options(&state->defaults, - default_opts, ARRAY_SIZE(default_opts), s); + fw3_parse_options(&state->defaults, fw3_default_opts, s); check_policy(e, &defs->policy_input, "input"); check_policy(e, &defs->policy_output, "output"); @@ -180,24 +180,23 @@ fw3_print_default_chains(enum fw3_table table, enum fw3_family family, struct fw3_state *state) { 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_TARGET_REJECT) ? "DROP" : fw3_flag_names[t]) 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->flags, + /* user chains already loaded, don't create again */ + if (hasbit(state->defaults.running_flags, FW3_DEFAULT_CUSTOM_CHAINS)) + delbit(custom_mask, FW3_DEFAULT_CUSTOM_CHAINS); + + print_chains(table, family, ":%s - [0:0]\n", defs->flags & custom_mask, default_chains, ARRAY_SIZE(default_chains)); } @@ -208,9 +207,9 @@ fw3_print_default_head_rules(enum fw3_table table, enum fw3_family family, 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, @@ -224,12 +223,15 @@ fw3_print_default_head_rules(enum fw3_table table, enum fw3_family family, 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]); @@ -259,8 +261,13 @@ fw3_print_default_head_rules(enum fw3_table table, enum fw3_family family, 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; @@ -289,49 +296,85 @@ fw3_print_default_tail_rules(enum fw3_table table, enum fw3_family family, } 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) +{ + info("Setting sysctl values"); + + 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_target 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) + bool pass2, struct fw3_state *state, enum fw3_target policy) { - struct fw3_statefile_entry *e; + struct fw3_defaults *defs = &state->defaults; + uint32_t custom_mask = ~0; + + if (!hasbit(defs->running_flags, family)) + return; - list_for_each_entry(e, statefile, list) + /* don't touch user chains on selective stop */ + delbit(custom_mask, FW3_DEFAULT_CUSTOM_CHAINS); + + if (!pass2) { - if (e->type != FW3_TYPE_DEFAULTS) - continue; + reset_policy(table, policy); - if (!pass2) - { - reset_policy(table); + print_chains(table, family, "-D %s\n", + defs->running_flags & custom_mask, + toplevel_rules, ARRAY_SIZE(toplevel_rules)); - print_chains(table, family, "-D %s\n", e->flags[0], - toplevel_rules, ARRAY_SIZE(toplevel_rules)); + print_chains(table, family, "-F %s\n", + defs->running_flags & custom_mask, + default_chains, ARRAY_SIZE(default_chains)); + } + else + { + print_chains(table, family, "-X %s\n", + defs->running_flags & custom_mask, + default_chains, ARRAY_SIZE(default_chains)); - 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)); - } + delbit(defs->flags, family); } } void fw3_flush_all(enum fw3_table table) { - reset_policy(table); + reset_policy(table, FW3_TARGET_ACCEPT); fw3_pr("-F\n"); fw3_pr("-X\n");