X-Git-Url: https://git.archive.openwrt.org/?a=blobdiff_plain;f=utils.c;h=fa4a73f9e692723490a66b5696d6cf8103e74244;hb=91953d6a6e90df988f442f53097bd208784a295d;hp=3558450d8831a96aa2a9ffa0763ea2a6a0eaf464;hpb=d5aa4e5d12fe80cf82da0ae4a0da595a8bfd83bb;p=project%2Ffirewall3.git diff --git a/utils.c b/utils.c index 3558450..fa4a73f 100644 --- a/utils.c +++ b/utils.c @@ -16,6 +16,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#define _GNU_SOURCE #include "utils.h" #include "options.h" @@ -132,6 +133,32 @@ info(const char* format, ...) fprintf(stderr, "\n"); } +void * +fw3_alloc(size_t size) +{ + void *mem; + + mem = calloc(1, size); + + if (!mem) + error("Out of memory while allocating %d bytes", size); + + return mem; +} + +char * +fw3_strdup(const char *s) +{ + char *ns; + + ns = strdup(s); + + if (!ns) + error("Out of memory while duplicating string '%s'", s); + + return ns; +} + const char * fw3_find_command(const char *cmd) { @@ -198,7 +225,7 @@ __fw3_command_pipe(bool silent, const char *command, ...) return false; argn = 2; - args = malloc(argn * sizeof(arg)); + args = calloc(argn, sizeof(arg)); if (!args) return false; @@ -357,6 +384,21 @@ write_defaults_uci(struct uci_context *ctx, struct fw3_defaults *d, uci_add_section(ctx, dest, "defaults", &ptr.s); + ptr.o = NULL; + ptr.option = "input"; + ptr.value = fw3_flag_names[d->policy_input]; + uci_set(ctx, &ptr); + + ptr.o = NULL; + ptr.option = "output"; + ptr.value = fw3_flag_names[d->policy_output]; + uci_set(ctx, &ptr); + + ptr.o = NULL; + ptr.option = "forward"; + ptr.value = fw3_flag_names[d->policy_forward]; + uci_set(ctx, &ptr); + sprintf(buf, "0x%x", d->flags[0]); ptr.o = NULL; ptr.option = "__flags_v4"; @@ -455,7 +497,10 @@ write_zone_uci(struct uci_context *ctx, struct fw3_zone *z, if (dev->invert) p += sprintf(p, "!"); - p += sprintf(p, "%s", dev->name); + if (*dev->network) + p += sprintf(p, "%s@%s", dev->name, dev->network); + else + p += sprintf(p, "%s", dev->name); ptr.value = buf; uci_add_list(ctx, &ptr); @@ -469,7 +514,7 @@ write_zone_uci(struct uci_context *ctx, struct fw3_zone *z, if (!sub) continue; - ptr.value = fw3_address_to_string(sub, true); + ptr.value = fw3_address_to_string(sub, true, false); uci_add_list(ctx, &ptr); } @@ -490,20 +535,13 @@ static void write_ipset_uci(struct uci_context *ctx, struct fw3_ipset *s, struct uci_package *dest) { - enum fw3_family fam = FW3_FAMILY_ANY; + struct fw3_ipset_datatype *type; - char buf[sizeof("0xffffffff\0")]; + char buf[sizeof("65535-65535\0")]; struct uci_ptr ptr = { .p = dest }; - if (!s->enabled || (s->external && *s->external)) - return; - - if (fw3_no_family(s->flags[0]) && !fw3_no_family(s->flags[1])) - fam = FW3_FAMILY_V6; - else if (!fw3_no_family(s->flags[0]) && fw3_no_family(s->flags[1])) - fam = FW3_FAMILY_V4; - else if (fw3_no_family(s->flags[0]) && fw3_no_family(s->flags[1])) + if (!s->enabled || s->external) return; uci_add_section(ctx, dest, "ipset", &ptr.s); @@ -513,25 +551,36 @@ write_ipset_uci(struct uci_context *ctx, struct fw3_ipset *s, ptr.value = s->name; uci_set(ctx, &ptr); - if (fam != FW3_FAMILY_ANY) + ptr.o = NULL; + ptr.option = "storage"; + ptr.value = fw3_ipset_method_names[s->method]; + uci_set(ctx, &ptr); + + list_for_each_entry(type, &s->datatypes, list) { + sprintf(buf, "%s_%s", type->dir, fw3_ipset_type_names[type->type]); ptr.o = NULL; - ptr.option = "family"; - ptr.value = fw3_flag_names[fam]; - uci_set(ctx, &ptr); + ptr.option = "match"; + ptr.value = buf; + uci_add_list(ctx, &ptr); } - sprintf(buf, "0x%x", s->flags[0]); - ptr.o = NULL; - ptr.option = "__flags_v4"; - ptr.value = buf; - uci_set(ctx, &ptr); + if (s->iprange.set) + { + ptr.o = NULL; + ptr.option = "iprange"; + ptr.value = fw3_address_to_string(&s->iprange, false, false); + uci_set(ctx, &ptr); + } - sprintf(buf, "0x%x", s->flags[1]); - ptr.o = NULL; - ptr.option = "__flags_v6"; - ptr.value = buf; - uci_set(ctx, &ptr); + if (s->portrange.set) + { + sprintf(buf, "%u-%u", s->portrange.port_min, s->portrange.port_max); + ptr.o = NULL; + ptr.option = "portrange"; + ptr.value = buf; + uci_set(ctx, &ptr); + } } void @@ -578,6 +627,7 @@ fw3_write_statefile(void *state) uci_unload(s->uci, p); } + fsync(fileno(sf)); fclose(sf); } } @@ -605,51 +655,30 @@ fw3_free_object(void *obj, const void *opts) 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, ...) +void +fw3_free_list(struct list_head *head) { - char buf[256]; - bool rv = false; + struct list_head *entry, *tmp; - va_list ap; - uint32_t f = flags ? flags[family == FW3_FAMILY_V6] : 0; - - if (mask) - f &= mask; + if (!head) + return; - for (; r->format; r++) + list_for_each_safe(entry, tmp, head) { - 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; + list_del(entry); + free(entry); } - return rv; + free(head); } - bool fw3_hotplug(bool add, void *zone, void *device) { struct fw3_zone *z = zone; struct fw3_device *d = device; - if (!d->network) + if (!*d->network) return false; switch (fork()) @@ -668,12 +697,12 @@ fw3_hotplug(bool add, void *zone, void *device) close(0); close(1); close(2); - chdir("/"); + if (chdir("/")) {}; clearenv(); setenv("ACTION", add ? "add" : "remove", 1); setenv("ZONE", z->name, 1); - setenv("INTERFACE", d->network->name, 1); + setenv("INTERFACE", d->network, 1); setenv("DEVICE", d->name, 1); execl(FW3_HOTPLUG, FW3_HOTPLUG, "firewall", NULL); @@ -681,3 +710,60 @@ fw3_hotplug(bool add, void *zone, void *device) /* unreached */ return false; } + +int +fw3_netmask2bitlen(int family, void *mask) +{ + int bits; + struct in_addr *v4; + struct in6_addr *v6; + + if (family == FW3_FAMILY_V6) + for (bits = 0, v6 = mask; + bits < 128 && (v6->s6_addr[bits / 8] << (bits % 8)) & 128; + bits++); + else + for (bits = 0, v4 = mask; + bits < 32 && (ntohl(v4->s_addr) << bits) & 0x80000000; + bits++); + + return bits; +} + +bool +fw3_bitlen2netmask(int family, int bits, void *mask) +{ + int i; + struct in_addr *v4; + struct in6_addr *v6; + + if (family == FW3_FAMILY_V6) + { + if (bits < -128 || bits > 128) + return false; + + v6 = mask; + i = abs(bits); + + memset(v6->s6_addr, 0xff, i / 8); + memset(v6->s6_addr + (i / 8) + 1, 0, (128 - i) / 8); + v6->s6_addr[i / 8] = 0xff << (8 - (i & 7)); + + if (bits < 0) + for (i = 0; i < 16; i++) + v6->s6_addr[i] = ~v6->s6_addr[i]; + } + else + { + if (bits < -32 || bits > 32) + return false; + + v4 = mask; + v4->s_addr = htonl(~((1 << (32 - abs(bits))) - 1)); + + if (bits < 0) + v4->s_addr = ~v4->s_addr; + } + + return true; +}