#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_DEFAULT_##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, MANGLE, UNSPEC, "mssfix"),
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(ANY, MANGLE, UNSPEC, "FORWARD -j mssfix"),
C(ANY, RAW, UNSPEC, "PREROUTING -j notrack"),
+
+ { }
};
const struct fw3_option fw3_default_opts[] = {
};
-static bool
-print_chains(enum fw3_table table, enum fw3_family family,
- const char *fmt, uint32_t *flags, uint32_t mask,
- const struct chain *chains, int n)
-{
- bool rv = false;
- const struct chain *c;
- uint32_t f = flags ? flags[family == FW3_FAMILY_V6] : 0;
-
- if (mask)
- f &= mask;
-
- 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) && !hasbit(f, 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)
{
if (defs->syn_flood)
set(defs->flags, family, FW3_DEFAULT_SYN_FLOOD);
- rv = print_chains(table, family, ":%s - [0:0]\n", defs->flags, custom_mask,
- default_chains, ARRAY_SIZE(default_chains));
+ rv = fw3_pr_rulespec(table, family, defs->flags, custom_mask,
+ default_chains, ":%s - [0:0]\n");
if (rv)
set(defs->flags, family, table);
"forward", "forwarding",
};
- print_chains(table, family, "-A %s\n", NULL, 0,
- toplevel_rules, ARRAY_SIZE(toplevel_rules));
+ fw3_pr_rulespec(table, family, NULL, 0, toplevel_rules, "-A %s\n");
switch (table)
{
{
reset_policy(table, reload ? FW3_TARGET_DROP : FW3_TARGET_ACCEPT);
- print_chains(table, family, "-D %s\n",
- defs->flags, custom_mask,
- toplevel_rules, ARRAY_SIZE(toplevel_rules));
+ fw3_pr_rulespec(table, family, defs->flags, custom_mask,
+ toplevel_rules, "-D %s\n");
- print_chains(table, family, "-F %s\n",
- defs->flags, custom_mask,
- default_chains, ARRAY_SIZE(default_chains));
+ fw3_pr_rulespec(table, family, defs->flags, custom_mask,
+ default_chains, "-F %s\n");
}
else
{
- print_chains(table, family, "-X %s\n",
- defs->flags, custom_mask,
- default_chains, ARRAY_SIZE(default_chains));
+ fw3_pr_rulespec(table, family, defs->flags, custom_mask,
+ default_chains, "-X %s\n");
del(defs->flags, family, table);
}
free(obj);
}
+
+
+bool
+fw3_pr_rulespec(int table, int family, uint32_t *flags, uint32_t mask,
+ const struct fw3_rule_spec *r, const char *fmt, ...)
+{
+ char buf[256];
+ bool rv = false;
+
+ va_list ap;
+ uint32_t f = flags ? flags[family == FW3_FAMILY_V6] : 0;
+
+ if (mask)
+ f &= mask;
+
+ for (; r->format; r++)
+ {
+ if (!fw3_is_family(r, family))
+ continue;
+
+ if (r->table != table)
+ continue;
+
+ if ((r->flag != 0) && !hasbit(f, r->flag))
+ continue;
+
+ va_start(ap, fmt);
+ vsnprintf(buf, sizeof(buf), r->format, ap);
+ va_end(ap);
+
+ fw3_pr(fmt, buf);
+
+ rv = true;
+ }
+
+ return rv;
+}
void fw3_free_object(void *obj, const void *opts);
+
+struct fw3_rule_spec {
+ int family;
+ int table;
+ int flag;
+ const char *format;
+};
+
+bool fw3_pr_rulespec(int table, int family, uint32_t *flags, uint32_t mask,
+ const struct fw3_rule_spec *r, const char *fmt, ...);
+
#endif
#include "ubus.h"
-#define C(f, tbl, tgt, name) \
- { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_TARGET_##tgt, name }
-
-struct chain {
- enum fw3_family family;
- enum fw3_table table;
- enum fw3_target target;
- const char *name;
-};
+#define C(f, tbl, tgt, fmt) \
+ { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_TARGET_##tgt, fmt }
-static const struct chain zone_chains[] = {
+static const struct fw3_rule_spec zone_chains[] = {
C(ANY, FILTER, UNSPEC, "zone_%1$s_input"),
C(ANY, FILTER, UNSPEC, "zone_%1$s_output"),
C(ANY, FILTER, UNSPEC, "zone_%1$s_forward"),
C(V4, NAT, CUSTOM_CHAINS, "prerouting_%1$s_rule"),
C(V4, NAT, CUSTOM_CHAINS, "postrouting_%1$s_rule"),
+
+ { }
};
"zone_%1$s_" #dir1 " -m comment --comment \"user chain for %1$s " \
#dir2 "\" -j " #dir2 "_%1$s_rule"
-static const struct chain zone_rules[] = {
+static const struct fw3_rule_spec zone_rules[] = {
C(ANY, FILTER, CUSTOM_CHAINS, R(input, input)),
C(ANY, FILTER, CUSTOM_CHAINS, R(output, output)),
C(ANY, FILTER, CUSTOM_CHAINS, R(forward, forwarding)),
C(V4, NAT, CUSTOM_CHAINS, R(prerouting, prerouting)),
C(V4, NAT, CUSTOM_CHAINS, R(postrouting, postrouting)),
+
+ { }
};
const struct fw3_option fw3_zone_opts[] = {
};
-static bool
-print_chains(enum fw3_table table, enum fw3_family family,
- const char *fmt, const char *name, uint32_t *targets, uint32_t mask,
- const struct chain *chains, int n)
-{
- bool rv = false;
- char cn[256] = { 0 };
- const struct chain *c;
- uint32_t t = targets ? targets[family == FW3_FAMILY_V6] : 0;
-
- if (mask)
- t &= mask;
-
- for (c = chains; n > 0; c++, n--)
- {
- if (!fw3_is_family(c, family))
- continue;
-
- if (c->table != table)
- continue;
-
- if ((c->target != FW3_TARGET_UNSPEC) && !hasbit(t, c->target))
- continue;
-
- snprintf(cn, sizeof(cn), c->name, name);
- fw3_pr(fmt, cn);
-
- rv = true;
- }
-
- return rv;
-}
-
static void
check_policy(struct uci_element *e, enum fw3_target *pol, enum fw3_target def,
const char *name)
if (!zone->conntrack && !state->defaults.drop_invalid)
set(zone->flags, family, FW3_TARGET_NOTRACK);
- c = print_chains(table, family, ":%s - [0:0]\n", zone->name,
- zone->flags, custom_mask,
- zone_chains, ARRAY_SIZE(zone_chains));
+ c = fw3_pr_rulespec(table, family, zone->flags, custom_mask, zone_chains,
+ ":%s - [0:0]\n", zone->name);
- r = print_chains(table, family, "-A %s\n", zone->name,
- zone->flags, 0,
- zone_rules, ARRAY_SIZE(zone_rules));
+ r = fw3_pr_rulespec(table, family, zone->flags, 0, zone_rules,
+ "-A %s\n", zone->name);
if (c || r)
{
if (!has(z->flags, family, table))
continue;
- print_chains(table, family, pass2 ? "-X %s\n" : "-F %s\n",
- z->name, z->flags, custom_mask,
- zone_chains, ARRAY_SIZE(zone_chains));
+ fw3_pr_rulespec(table, family, z->flags, custom_mask, zone_chains,
+ pass2 ? "-X %s\n" : "-F %s\n", z->name);
if (pass2)
{