/*
* firewall3 - 3rd OpenWrt UCI firewall implementation
*
- * Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
+ * Copyright (C) 2013-2018 Jo-Philipp Wich <jo@mein.io>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
FW3_OPT("direction", direction, rule, direction_out),
FW3_OPT("ipset", setmatch, rule, ipset),
+ FW3_OPT("helper", cthelper, rule, helper),
+ FW3_OPT("set_helper", cthelper, rule, set_helper),
FW3_LIST("proto", protocol, rule, proto),
FW3_OPT("set_mark", mark, rule, set_mark),
FW3_OPT("set_xmark", mark, rule, set_xmark),
+ FW3_OPT("dscp", dscp, rule, dscp),
+ FW3_OPT("set_dscp", dscp, rule, set_dscp),
+
FW3_OPT("target", target, rule, target),
{ }
return rule;
}
-#define warn_rule(r, e, fmt, ...) \
- do { \
- if (e) \
- warn_elem(e, fmt, ##__VA_ARGS__); \
- else \
- warn("Warning: ubus rule (%s) " fmt, \
- (r && r->name) ? r->name : "?", ##__VA_ARGS__); \
- } while(0)
-
static bool
check_rule(struct fw3_state *state, struct fw3_rule *r, struct uci_element *e)
{
if (r->src.invert || r->dest.invert)
{
- warn_rule(r, e, "must not have inverted 'src' or 'dest' options");
+ warn_section("rule", r, e, "must not have inverted 'src' or 'dest' options");
return false;
}
else if (r->src.set && !r->src.any &&
!(r->_src = fw3_lookup_zone(state, r->src.name)))
{
- warn_rule(r, e, "refers to not existing zone '%s'", r->src.name);
+ warn_section("rule", r, e, "refers to not existing zone '%s'", r->src.name);
return false;
}
else if (r->dest.set && !r->dest.any &&
!(r->_dest = fw3_lookup_zone(state, r->dest.name)))
{
- warn_rule(r, e, "refers to not existing zone '%s'", r->dest.name);
+ warn_section("rule", r, e, "refers to not existing zone '%s'", r->dest.name);
return false;
}
else if (r->ipset.set && state->disable_ipsets)
{
- warn_rule(r, e, "skipped due to disabled ipset support");
+ warn_section("rule", r, e, "skipped due to disabled ipset support");
return false;
}
else if (r->ipset.set &&
!(r->ipset.ptr = fw3_lookup_ipset(state, r->ipset.name)))
{
- warn_rule(r, e, "refers to unknown ipset '%s'", r->ipset.name);
+ warn_section("rule", r, e, "refers to unknown ipset '%s'", r->ipset.name);
+ return false;
+ }
+ else if (r->helper.set &&
+ !(r->helper.ptr = fw3_lookup_cthelper(state, r->helper.name)))
+ {
+ warn_section("rule", r, e, "refers to unknown CT helper '%s'", r->helper.name);
+ return false;
+ }
+ else if (r->set_helper.set &&
+ !(r->set_helper.ptr = fw3_lookup_cthelper(state, r->set_helper.name)))
+ {
+ warn_section("rule", r, e, "refers to unknown CT helper '%s'", r->set_helper.name);
return false;
}
- if (!r->_src && r->target == FW3_FLAG_NOTRACK)
+ if (!r->_src && (r->target == FW3_FLAG_NOTRACK || r->target == FW3_FLAG_HELPER))
{
- warn_rule(r, e, "is set to target NOTRACK but has no source assigned");
+ warn_section("rule", r, e, "is set to target %s but has no source assigned",
+ fw3_flag_names[r->target]);
return false;
}
if (!r->set_mark.set && !r->set_xmark.set &&
r->target == FW3_FLAG_MARK)
{
- warn_rule(r, e, "is set to target MARK but specifies neither "
- "'set_mark' nor 'set_xmark' option");
+ warn_section("rule", r, e, "is set to target MARK but specifies neither "
+ "'set_mark' nor 'set_xmark' option");
+ return false;
+ }
+
+ if (!r->set_dscp.set && r->target == FW3_FLAG_DSCP)
+ {
+ warn_section("rule", r, e, "is set to target DSCP but specifies no "
+ "'set_dscp' option");
+ return false;
+ }
+
+ if (r->_dest && (r->target == FW3_FLAG_MARK || r->target == FW3_FLAG_DSCP))
+ {
+ warn_section("rule", r, e, "must not specify 'dest' for %s target",
+ fw3_flag_names[r->target]);
return false;
}
- if (r->_dest && r->target == FW3_FLAG_MARK)
+ if (r->set_mark.invert || r->set_xmark.invert || r->set_dscp.invert)
{
- warn_rule(r, e, "must not specify 'dest' for MARK target");
+ warn_section("rule", r, e, "must not have inverted 'set_mark', "
+ "'set_xmark' or 'set_dscp'");
return false;
}
- if (r->set_mark.invert || r->set_xmark.invert)
+ if (!r->set_helper.set && r->target == FW3_FLAG_HELPER)
{
- warn_rule(r, e, "must not have inverted 'set_mark' or 'set_xmark'");
+ warn_section("rule", r, e, "is set to target HELPER but specifies "
+ "no 'set_helper' option");
+ return false;
+ }
+
+ if (r->set_helper.invert && r->target == FW3_FLAG_HELPER)
+ {
+ warn_section("rule", r, e, "must not have inverted 'set_helper' option");
return false;
}
if (!r->_src && !r->_dest && !r->src.any && !r->dest.any)
{
- warn_rule(r, e, "has neither a source nor a destination zone assigned "
- "- assuming an output r");
+ warn_section("rule", r, e, "has neither a source nor a destination zone assigned "
+ "- assuming an output rule");
}
if (list_empty(&r->proto))
{
- warn_rule(r, e, "does not specify a protocol, assuming TCP+UDP");
+ warn_section("rule", r, e, "does not specify a protocol, assuming TCP+UDP");
fw3_parse_protocol(&r->proto, "tcpudp", true);
}
if (r->target == FW3_FLAG_UNSPEC)
{
- warn_rule(r, e, "has no target specified, defaulting to REJECT");
+ warn_section("rule", r, e, "has no target specified, defaulting to REJECT");
r->target = FW3_FLAG_REJECT;
}
- else if (r->target > FW3_FLAG_MARK)
+ else if (r->target > FW3_FLAG_DSCP)
{
- warn_rule(r, e, "has invalid target specified, defaulting to REJECT");
+ warn_section("rule", r, e, "has invalid target specified, defaulting to REJECT");
r->target = FW3_FLAG_REJECT;
}
struct uci_section *s;
struct uci_element *e;
struct fw3_rule *rule;
- struct blob_attr *entry, *opt;
- unsigned rem, orem;
+ struct blob_attr *entry;
+ unsigned rem;
INIT_LIST_HEAD(&state->rules);
blob_for_each_attr(entry, a, rem) {
- const char *type = NULL;
+ const char *type;
const char *name = "ubus rule";
- blobmsg_for_each_attr(opt, entry, orem)
- if (!strcmp(blobmsg_name(opt), "type"))
- type = blobmsg_get_string(opt);
- else if (!strcmp(blobmsg_name(opt), "name"))
- name = blobmsg_get_string(opt);
- if (!type || strcmp(type, "rule"))
+ if (!fw3_attr_parse_name_type(entry, &name, &type))
+ continue;
+
+ if (strcmp(type, "rule"))
continue;
if (!(rule = alloc_rule(state)))
if (!fw3_parse_blob_options(rule, fw3_rule_opts, entry, name))
{
- warn_rule(rule, NULL, "skipped due to invalid options\n");
+ warn_section("rule", rule, NULL, "skipped due to invalid options");
fw3_free_rule(rule);
continue;
}
{
snprintf(chain, sizeof(chain), "zone_%s_notrack", rule->src.name);
}
- else if (rule->target == FW3_FLAG_MARK && (rule->_src || rule->src.any))
+ else if (rule->target == FW3_FLAG_HELPER)
+ {
+ snprintf(chain, sizeof(chain), "zone_%s_helper", rule->src.name);
+ }
+ else if ((rule->target == FW3_FLAG_MARK || rule->target == FW3_FLAG_DSCP) &&
+ (rule->_src || rule->src.any))
{
snprintf(chain, sizeof(chain), "PREROUTING");
}
{
const char *name;
struct fw3_mark *mark;
- char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
+ char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF")];
switch(rule->target)
{
case FW3_FLAG_MARK:
name = rule->set_mark.set ? "--set-mark" : "--set-xmark";
mark = rule->set_mark.set ? &rule->set_mark : &rule->set_xmark;
- sprintf(buf, "0x%x/0x%x", mark->mark, mark->mask);
+ snprintf(buf, sizeof(buf), "0x%x/0x%x", mark->mark, mark->mask);
fw3_ipt_rule_target(r, "MARK");
fw3_ipt_rule_addarg(r, false, name, buf);
return;
+ case FW3_FLAG_DSCP:
+ snprintf(buf, sizeof(buf), "0x%x", rule->set_dscp.dscp);
+
+ fw3_ipt_rule_target(r, "DSCP");
+ fw3_ipt_rule_addarg(r, false, "--set-dscp", buf);
+ return;
+
case FW3_FLAG_NOTRACK:
fw3_ipt_rule_target(r, "CT");
fw3_ipt_rule_addarg(r, false, "--notrack", NULL);
return;
+ case FW3_FLAG_HELPER:
+ fw3_ipt_rule_target(r, "CT");
+ fw3_ipt_rule_addarg(r, false, "--helper", rule->set_helper.ptr->name);
+ return;
+
case FW3_FLAG_ACCEPT:
case FW3_FLAG_DROP:
name = fw3_flag_names[rule->target];
return;
}
+ if (!fw3_is_family(sip, handle->family) ||
+ !fw3_is_family(dip, handle->family))
+ {
+ if ((sip && !sip->resolved) || (dip && !dip->resolved))
+ info(" ! Skipping due to different family of ip address");
+
+ return;
+ }
+
+ if (!fw3_is_family(sip, handle->family) ||
+ !fw3_is_family(dip, handle->family))
+ {
+ if ((sip && !sip->resolved) || (dip && !dip->resolved))
+ info(" ! Skipping due to different family of ip address");
+
+ return;
+ }
+
if (proto->protocol == 58 && handle->family == FW3_FAMILY_V4)
{
- info(" ! Skipping due to different family of protocol");
+ info(" ! Skipping protocol %s due to different family",
+ fw3_protoname(proto));
+ return;
+ }
+
+ if (rule->helper.ptr &&
+ !fw3_cthelper_check_proto(rule->helper.ptr, proto))
+ {
+ info(" ! Skipping protocol %s since helper '%s' does not support it",
+ fw3_protoname(proto), rule->helper.ptr->name);
+ return;
+ }
+
+ if (rule->set_helper.ptr &&
+ !fw3_cthelper_check_proto(rule->set_helper.ptr, proto))
+ {
+ info(" ! Skipping protocol %s since helper '%s' does not support it",
+ fw3_protoname(proto), rule->helper.ptr->name);
return;
}
fw3_ipt_rule_icmptype(r, icmptype);
fw3_ipt_rule_mac(r, mac);
fw3_ipt_rule_ipset(r, &rule->ipset);
+ fw3_ipt_rule_helper(r, &rule->helper);
fw3_ipt_rule_limit(r, &rule->limit);
fw3_ipt_rule_time(r, &rule->time);
fw3_ipt_rule_mark(r, &rule->mark);
+ fw3_ipt_rule_dscp(r, &rule->dscp);
set_target(r, rule);
fw3_ipt_rule_extra(r, rule->extra);
set_comment(r, rule->name, num);
return;
if ((rule->target == FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_RAW) ||
+ (rule->target == FW3_FLAG_HELPER && handle->table != FW3_TABLE_RAW) ||
(rule->target == FW3_FLAG_MARK && handle->table != FW3_TABLE_MANGLE) ||
+ (rule->target == FW3_FLAG_DSCP && handle->table != FW3_TABLE_MANGLE) ||
(rule->target < FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_FILTER))
return;
set(rule->ipset.ptr->flags, handle->family, handle->family);
}
+ if (rule->helper.ptr && !fw3_is_family(rule->helper.ptr, handle->family))
+ {
+ info(" ! Skipping due to unsupported family of CT helper");
+ return;
+ }
+
+ if (rule->set_helper.ptr && !fw3_is_family(rule->set_helper.ptr, handle->family))
+ {
+ info(" ! Skipping due to unsupported family of CT helper");
+ return;
+ }
+
list_for_each_entry(proto, &rule->proto, list)
{
/* icmp / ipv6-icmp */