X-Git-Url: http://git.archive.openwrt.org/?p=project%2Ffirewall3.git;a=blobdiff_plain;f=snats.c;h=f43daf244c05362243453a7a436f4d34ea6e3035;hp=11bcc068390661e0cc68f0324abbc5de4e928267;hb=47b23946cb2d51c486cd01596744955f850e2060;hpb=6907da00bbf0a5c6778f3ca8c9646bdc04119705 diff --git a/snats.c b/snats.c index 11bcc06..f43daf2 100644 --- a/snats.c +++ b/snats.c @@ -1,7 +1,7 @@ /* * firewall3 - 3rd OpenWrt UCI firewall implementation * - * Copyright (C) 2014 Jo-Philipp Wich + * Copyright (C) 2014 Jo-Philipp Wich * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -26,6 +26,7 @@ const struct fw3_option fw3_snat_opts[] = { FW3_OPT("family", family, snat, family), FW3_OPT("src", device, snat, src), + FW3_OPT("device", string, snat, device), FW3_OPT("ipset", setmatch, snat, ipset), @@ -45,6 +46,8 @@ const struct fw3_option fw3_snat_opts[] = { 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), @@ -101,32 +104,65 @@ check_families(struct uci_element *e, struct fw3_snat *r) return true; } + +static struct fw3_snat* +alloc_snat(struct fw3_state *state) +{ + struct fw3_snat *snat = calloc(1, sizeof(*snat)); + + if (snat) { + INIT_LIST_HEAD(&snat->proto); + list_add_tail(&snat->list, &state->snats); + snat->enabled = true; + } + + return snat; +} + + void -fw3_load_snats(struct fw3_state *state, struct uci_package *p) +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 fw3_snat *snat, *n; + struct blob_attr *rule, *opt; + unsigned rem, orem; INIT_LIST_HEAD(&state->snats); - uci_foreach_element(&p->sections, e) - { - s = uci_to_section(e); + blob_for_each_attr(rule, a, rem) { + const char *type = NULL; + const char *name = "ubus rule"; + blobmsg_for_each_attr(opt, rule, 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 (strcmp(s->type, "nat")) + if (!type || strcmp(type, "nat")) continue; - snat = malloc(sizeof(*snat)); + if (!(snat = alloc_snat(state))) + continue; - if (!snat) + if (!fw3_parse_blob_options(snat, fw3_snat_opts, rule, name)) + { + fprintf(stderr, "%s skipped due to invalid options\n", name); + fw3_free_snat(snat); continue; + } + } - memset(snat, 0, sizeof(*snat)); + uci_foreach_element(&p->sections, e) + { + s = uci_to_section(e); - INIT_LIST_HEAD(&snat->proto); + if (strcmp(s->type, "nat")) + continue; - snat->enabled = true; + if (!(snat = alloc_snat(state))) + continue; if (!fw3_parse_options(snat, fw3_snat_opts, s)) { @@ -134,7 +170,10 @@ fw3_load_snats(struct fw3_state *state, struct uci_package *p) fw3_free_snat(snat); continue; } + } + list_for_each_entry_safe(snat, n, &state->snats, list) + { if (!snat->enabled) { fw3_free_snat(snat); @@ -217,8 +256,6 @@ fw3_load_snats(struct fw3_state *state, struct uci_package *p) set(snat->_src->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT); snat->_src->conntrack = true; } - - list_add_tail(&snat->list, &state->snats); } } @@ -228,7 +265,7 @@ append_chain(struct fw3_ipt_rule *r, struct fw3_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 @@ -247,13 +284,23 @@ set_target(struct fw3_ipt_rule *r, struct fw3_snat *snat, } 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"); @@ -296,6 +343,7 @@ print_snat(struct fw3_ipt_handle *h, struct fw3_state *state, 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);