/*
* firewall3 - 3rd OpenWrt UCI firewall implementation
*
- * Copyright (C) 2014 Jo-Philipp Wich <jow@openwrt.org>
+ * Copyright (C) 2014 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("family", family, snat, family),
FW3_OPT("src", device, snat, src),
+ FW3_OPT("device", string, snat, device),
FW3_OPT("ipset", setmatch, snat, ipset),
FW3_OPT("limit", limit, snat, limit),
FW3_OPT("limit_burst", int, snat, limit.burst),
+ FW3_OPT("connlimit_ports", bool, snat, connlimit_ports),
+
FW3_OPT("utc_time", bool, snat, time.utc),
FW3_OPT("start_date", date, snat, time.datestart),
FW3_OPT("stop_date", date, snat, time.datestop),
if (r->_src && r->_src->family && r->_src->family != r->family)
{
- warn_elem(e, "refers to source zone with different family");
+ warn_section("nat", r, e, "refers to source zone with different family");
return false;
}
if (r->ipset.ptr && r->ipset.ptr->family &&
r->ipset.ptr->family != r->family)
{
- warn_elem(e, "refers to ipset with different family");
+ warn_section("nat", r, e, "refers to ipset with different family");
return false;
}
if (r->ip_src.family && r->ip_src.family != r->family)
{
- warn_elem(e, "uses source ip with different family");
+ warn_section("nat", r, e, "uses source ip with different family");
return false;
}
if (r->ip_dest.family && r->ip_dest.family != r->family)
{
- warn_elem(e, "uses destination ip with different family");
+ warn_section("nat", r, e, "uses destination ip with different family");
return false;
}
if (r->ip_snat.family && r->ip_snat.family != r->family)
{
- warn_elem(e, "uses snat ip with different family");
+ warn_section("nat", r, e, "uses snat ip with different family");
return false;
}
return true;
}
-void
-fw3_load_snats(struct fw3_state *state, struct uci_package *p)
+
+static struct fw3_snat*
+alloc_snat(struct fw3_state *state)
{
- struct uci_section *s;
- struct uci_element *e;
- struct fw3_snat *snat;
+ struct fw3_snat *snat = calloc(1, sizeof(*snat));
- INIT_LIST_HEAD(&state->snats);
+ if (snat) {
+ INIT_LIST_HEAD(&snat->proto);
+ list_add_tail(&snat->list, &state->snats);
+ snat->enabled = true;
+ }
- uci_foreach_element(&p->sections, e)
+ return snat;
+}
+
+static bool
+check_snat(struct fw3_state *state, struct fw3_snat *snat, struct uci_element *e)
+{
+ if (!snat->enabled)
+ return false;
+
+ if (snat->src.invert)
{
- s = uci_to_section(e);
+ warn_section("nat", snat, e, "must not have an inverted source");
+ return false;
+ }
+ else if (snat->src.set && !snat->src.any &&
+ !(snat->_src = fw3_lookup_zone(state, snat->src.name)))
+ {
+ warn_section("nat", snat, e, "refers to not existing zone '%s'", snat->src.name);
+ return false;
+ }
+ else if (snat->ipset.set && state->disable_ipsets)
+ {
+ warn_section("nat", snat, e, "skipped due to disabled ipset support");
+ return false;
+ }
+ else if (snat->ipset.set &&
+ !(snat->ipset.ptr = fw3_lookup_ipset(state, snat->ipset.name)))
+ {
+ warn_section("nat", snat, e, "refers to unknown ipset '%s'", snat->ipset.name);
+ return false;
+ }
- if (strcmp(s->type, "nat"))
- continue;
+ if (!check_families(e, snat))
+ return false;
- snat = malloc(sizeof(*snat));
+ if (snat->target == FW3_FLAG_UNSPEC)
+ {
+ warn_section("nat", snat, e, "has no target specified, defaulting to MASQUERADE");
+ snat->target = FW3_FLAG_MASQUERADE;
+ }
+ else if (snat->target != FW3_FLAG_ACCEPT && snat->target != FW3_FLAG_SNAT &&
+ snat->target != FW3_FLAG_MASQUERADE)
+ {
+ warn_section("nat", snat, e, "has invalid target specified, defaulting to MASQUERADE");
+ snat->target = FW3_FLAG_MASQUERADE;
+ }
- if (!snat)
- continue;
+ if (snat->target == FW3_FLAG_SNAT &&
+ !snat->ip_snat.set && !snat->port_snat.set)
+ {
+ warn_section("nat", snat, e, "needs either 'snat_ip' or 'snat_port' for SNAT");
+ return false;
+ }
+ else if (snat->target != FW3_FLAG_SNAT && snat->ip_snat.set)
+ {
+ warn_section("nat", snat, e, "must not use 'snat_ip' for non-SNAT");
+ return false;
+ }
+ else if (snat->target != FW3_FLAG_SNAT && snat->port_snat.set)
+ {
+ warn_section("nat", snat, e, "must not use 'snat_port' for non-SNAT");
+ return false;
+ }
- memset(snat, 0, sizeof(*snat));
+ if (list_empty(&snat->proto))
+ {
+ warn_section("nat", snat, e, "does not specify a protocol, assuming all");
+ fw3_parse_protocol(&snat->proto, "all", true);
+ }
- INIT_LIST_HEAD(&snat->proto);
+ if (snat->_src)
+ set(snat->_src->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
- snat->enabled = true;
+ return true;
+}
- if (!fw3_parse_options(snat, fw3_snat_opts, s))
- {
- warn_elem(e, "skipped due to invalid options");
- fw3_free_snat(snat);
- continue;
- }
- if (!snat->enabled)
- {
- fw3_free_snat(snat);
- continue;
- }
+void
+fw3_load_snats(struct fw3_state *state, struct uci_package *p, struct blob_attr *a)
+{
+ struct uci_section *s;
+ struct uci_element *e;
+ struct fw3_snat *snat;
+ struct blob_attr *entry;
+ unsigned rem;
- if (snat->src.invert)
- {
- warn_elem(e, "must not have an inverted source");
- fw3_free_snat(snat);
+ INIT_LIST_HEAD(&state->snats);
+
+ blob_for_each_attr(entry, a, rem) {
+ const char *type = NULL;
+ const char *name = "ubus rule";
+
+ if (!fw3_attr_parse_name_type(entry, &name, &type))
continue;
- }
- else if (snat->src.set && !snat->src.any &&
- !(snat->_src = fw3_lookup_zone(state, snat->src.name)))
- {
- warn_elem(e, "refers to not existing zone '%s'", snat->src.name);
- fw3_free_snat(snat);
+
+ if (strcmp(type, "nat"))
continue;
- }
- else if (snat->ipset.set && state->disable_ipsets)
- {
- warn_elem(e, "skipped due to disabled ipset support");
- fw3_free_snat(snat);
+
+ snat = alloc_snat(state);
+ if (!snat)
continue;
- }
- else if (snat->ipset.set &&
- !(snat->ipset.ptr = fw3_lookup_ipset(state, snat->ipset.name)))
+
+ if (!fw3_parse_blob_options(snat, fw3_snat_opts, entry, name))
{
- warn_elem(e, "refers to unknown ipset '%s'", snat->ipset.name);
+ warn_section("nat", snat, NULL, "skipped due to invalid options");
fw3_free_snat(snat);
continue;
}
- if (!check_families(e, snat))
- {
+ if (!check_snat(state, snat, NULL))
fw3_free_snat(snat);
- continue;
- }
+ }
- if (snat->target == FW3_FLAG_UNSPEC)
- {
- warn_elem(e, "has no target specified, defaulting to MASQUERADE");
- snat->target = FW3_FLAG_MASQUERADE;
- }
- else if (snat->target < FW3_FLAG_SNAT || snat->target > FW3_FLAG_MASQUERADE)
- {
- warn_elem(e, "has invalid target specified, defaulting to MASQUERADE");
- snat->target = FW3_FLAG_MASQUERADE;
- }
+ uci_foreach_element(&p->sections, e)
+ {
+ s = uci_to_section(e);
- if (snat->target == FW3_FLAG_SNAT &&
- !snat->ip_snat.set && !snat->port_snat.set)
- {
- warn_elem(e, "needs either 'snat_ip' or 'snat_port' for SNAT");
- fw3_free_snat(snat);
+ if (strcmp(s->type, "nat"))
continue;
- }
- else if (snat->target == FW3_FLAG_MASQUERADE && snat->ip_snat.set)
- {
- warn_elem(e, "must not use 'snat_ip' for MASQUERADE");
- fw3_free_snat(snat);
+
+ snat = alloc_snat(state);
+ if (!snat)
continue;
- }
- else if (snat->target == FW3_FLAG_MASQUERADE && snat->port_snat.set)
+
+ if (!fw3_parse_options(snat, fw3_snat_opts, s))
{
- warn_elem(e, "must not use 'snat_port' for MASQUERADE");
+ warn_elem(e, "skipped due to invalid options");
fw3_free_snat(snat);
continue;
}
- if (list_empty(&snat->proto))
- {
- warn_elem(e, "does not specify a protocol, assuming all");
- fw3_parse_protocol(&snat->proto, "all", true);
- }
-
- if (snat->_src)
- {
- set(snat->_src->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
- snat->_src->conntrack = true;
- }
-
- list_add_tail(&snat->list, &state->snats);
+ if (!check_snat(state, snat, e))
+ fw3_free_snat(snat);
}
}
if (snat->_src)
fw3_ipt_rule_append(r, "zone_%s_postrouting", snat->src.name);
else
- fw3_ipt_rule_append(r, "delegate_postrouting");
+ fw3_ipt_rule_append(r, "POSTROUTING");
}
static void
}
if (snat->port_snat.set && proto && !proto->any &&
- (proto->protocol == 6 || proto->protocol == 17))
+ (proto->protocol == 6 || proto->protocol == 17 || proto->protocol == 1))
{
if (snat->port_snat.port_min == snat->port_snat.port_max)
sprintf(buf + strlen(buf), ":%u", snat->port_snat.port_min);
else
sprintf(buf + strlen(buf), ":%u-%u",
snat->port_snat.port_min, snat->port_snat.port_max);
+
+ if (snat->connlimit_ports) {
+ char portcntbuf[6];
+ snprintf(portcntbuf, sizeof(portcntbuf), "%u",
+ 1 + snat->port_snat.port_max - snat->port_snat.port_min);
+
+ fw3_ipt_rule_addarg(r, false, "-m", "connlimit");
+ fw3_ipt_rule_addarg(r, false, "--connlimit-daddr", NULL);
+ fw3_ipt_rule_addarg(r, false, "--connlimit-upto", portcntbuf);
+ }
}
fw3_ipt_rule_target(r, "SNAT");
fw3_ipt_rule_addarg(r, false, "--to-source", buf);
}
+ else if (snat->target == FW3_FLAG_ACCEPT)
+ {
+ fw3_ipt_rule_target(r, "ACCEPT");
+ }
else
{
fw3_ipt_rule_target(r, "MASQUERADE");
r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
fw3_ipt_rule_sport_dport(r, spt, dpt);
+ fw3_ipt_rule_device(r, snat->device, true);
fw3_ipt_rule_ipset(r, &snat->ipset);
fw3_ipt_rule_limit(r, &snat->limit);
fw3_ipt_rule_time(r, &snat->time);