firewall3: check the return value of fw3_parse_options()
[project/firewall3.git] / options.c
index 47cd727..d88d3ba 100644 (file)
--- a/options.c
+++ b/options.c
@@ -1,7 +1,7 @@
 /*
  * firewall3 - 3rd OpenWrt UCI firewall implementation
  *
- *   Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
+ *   Copyright (C) 2013-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
@@ -78,13 +78,14 @@ const char *fw3_flag_names[__FW3_FLAG_MAX] = {
        "MARK",
        "DNAT",
        "SNAT",
+       "MASQUERADE",
 
        "ACCEPT",
        "REJECT",
        "DROP",
 };
 
-static const char *limit_units[] = {
+const char *fw3_limit_units[__FW3_LIMIT_UNIT_MAX] = {
        "second",
        "minute",
        "hour",
@@ -142,9 +143,10 @@ fw3_parse_bool(void *ptr, const char *val, bool is_list)
 bool
 fw3_parse_int(void *ptr, const char *val, bool is_list)
 {
-       int n = strtol(val, NULL, 0);
+       char *e;
+       int n = strtol(val, &e, 0);
 
-       if (errno == ERANGE || errno == EINVAL)
+       if (e == val || *e)
                return false;
 
        *((int *)ptr) = n;
@@ -163,7 +165,7 @@ bool
 fw3_parse_target(void *ptr, const char *val, bool is_list)
 {
        return parse_enum(ptr, val, &fw3_flag_names[FW3_FLAG_ACCEPT],
-                         FW3_FLAG_ACCEPT, FW3_FLAG_SNAT);
+                         FW3_FLAG_ACCEPT, FW3_FLAG_MASQUERADE);
 }
 
 bool
@@ -191,7 +193,7 @@ fw3_parse_limit(void *ptr, const char *val, bool is_list)
        if (!strlen(e))
                return false;
 
-       if (!parse_enum(&u, e, limit_units, 0, FW3_LIMIT_UNIT_DAY))
+       if (!parse_enum(&u, e, fw3_limit_units, 0, FW3_LIMIT_UNIT_DAY))
                return false;
 
        limit->rate = n;
@@ -203,6 +205,7 @@ fw3_parse_limit(void *ptr, const char *val, bool is_list)
 bool
 fw3_parse_device(void *ptr, const char *val, bool is_list)
 {
+       char *p;
        struct fw3_device dev = { };
 
        if (*val == '*')
@@ -219,6 +222,12 @@ fw3_parse_device(void *ptr, const char *val, bool is_list)
                while (isspace(*++val));
        }
 
+       if ((p = strchr(val, '@')) != NULL)
+       {
+               *p++ = 0;
+               snprintf(dev.network, sizeof(dev.network), "%s", p);
+       }
+
        if (*val)
                snprintf(dev.name, sizeof(dev.name), "%s", val);
        else
@@ -235,8 +244,8 @@ fw3_parse_address(void *ptr, const char *val, bool is_list)
        struct fw3_address addr = { };
        struct in_addr v4;
        struct in6_addr v6;
-       char *p, *s, *e;
-       int i, m = -1;
+       char *p = NULL, *m = NULL, *s, *e;
+       int bits = -1;
 
        if (*val == '!')
        {
@@ -249,98 +258,114 @@ fw3_parse_address(void *ptr, const char *val, bool is_list)
        if (!s)
                return false;
 
-       if ((p = strchr(s, '/')) != NULL)
-       {
+       if ((m = strchr(s, '/')) != NULL)
+               *m++ = 0;
+       else if ((p = strchr(s, '-')) != NULL)
                *p++ = 0;
-               m = strtoul(p, &e, 10);
 
-               if ((e == p) || (*e != 0))
+       if (inet_pton(AF_INET6, s, &v6))
+       {
+               addr.family = FW3_FAMILY_V6;
+               addr.address.v6 = v6;
+
+               if (m)
                {
-                       if (strchr(s, ':') || !inet_pton(AF_INET, p, &v4))
+                       if (!inet_pton(AF_INET6, m, &v6))
                        {
-                               free(s);
-                               return false;
-                       }
+                               bits = strtol(m, &e, 10);
 
-                       for (i = 0, m = 32; !(v4.s_addr & 1) && (i < 32); i++)
-                       {
-                               m--;
-                               v4.s_addr >>= 1;
+                               if ((*e != 0) || !fw3_bitlen2netmask(addr.family, bits, &v6))
+                                       goto fail;
                        }
-               }
-       }
-       else if ((p = strchr(s, '-')) != NULL)
-       {
-               *p++ = 0;
 
-               if (inet_pton(AF_INET6, p, &v6))
-               {
-                       addr.family = FW3_FAMILY_V6;
-                       addr.address2.v6 = v6;
-                       addr.range = true;
+                       addr.mask.v6 = v6;
                }
-               else if (inet_pton(AF_INET, p, &v4))
+               else if (p)
                {
-                       addr.family = FW3_FAMILY_V4;
-                       addr.address2.v4 = v4;
+                       if (!inet_pton(AF_INET6, p, &addr.mask.v6))
+                               goto fail;
+
                        addr.range = true;
                }
                else
                {
-                       free(s);
-                       return false;
+                       memset(addr.mask.v6.s6_addr, 0xFF, 16);
                }
        }
-
-       if (inet_pton(AF_INET6, s, &v6))
-       {
-               addr.family = FW3_FAMILY_V6;
-               addr.address.v6 = v6;
-               addr.mask = (m >= 0) ? m : 128;
-       }
        else if (inet_pton(AF_INET, s, &v4))
        {
                addr.family = FW3_FAMILY_V4;
                addr.address.v4 = v4;
-               addr.mask = (m >= 0) ? m : 32;
+
+               if (m)
+               {
+                       if (!inet_pton(AF_INET, m, &v4))
+                       {
+                               bits = strtol(m, &e, 10);
+
+                               if ((*e != 0) || !fw3_bitlen2netmask(addr.family, bits, &v4))
+                                       goto fail;
+                       }
+
+                       addr.mask.v4 = v4;
+               }
+               else if (p)
+               {
+                       if (!inet_pton(AF_INET, p, &addr.mask.v4))
+                               goto fail;
+
+                       addr.range = true;
+               }
+               else
+               {
+                       addr.mask.v4.s_addr = 0xFFFFFFFF;
+               }
        }
        else
        {
-               free(s);
-               return false;
+               goto fail;
        }
 
        free(s);
        addr.set = true;
        put_value(ptr, &addr, sizeof(addr), is_list);
        return true;
+
+fail:
+       free(s);
+       return false;
 }
 
 bool
 fw3_parse_network(void *ptr, const char *val, bool is_list)
 {
        struct fw3_device dev = { };
-       struct fw3_address *addr;
-       struct list_head *addr_list;
+       struct fw3_address *addr, *tmp;
+       LIST_HEAD(addr_list);
 
        if (!fw3_parse_address(ptr, val, is_list))
        {
                if (!fw3_parse_device(&dev, val, false))
                        return false;
 
-               addr_list = fw3_ubus_address(dev.name);
-
-               if (addr_list)
+               fw3_ubus_address(&addr_list, dev.name);
+               list_for_each_entry(addr, &addr_list, list)
                {
-                       list_for_each_entry(addr, addr_list, list)
-                       {
-                               addr->invert = dev.invert;
+                       addr->invert = dev.invert;
+                       addr->resolved = true;
+               }
 
-                               if (!put_value(ptr, addr, sizeof(*addr), is_list))
-                                       break;
-                       }
+               if (is_list)
+               {
+                       list_splice_tail(&addr_list, ptr);
+               }
+               else if (!list_empty(&addr_list))
+               {
+                       memcpy(ptr, list_first_entry(&addr_list, typeof(*addr), list),
+                              sizeof(*addr));
 
-                       fw3_ubus_address_free(addr_list);
+                       list_for_each_entry_safe(addr, tmp, &addr_list, list)
+                               free(addr);
                }
        }
 
@@ -417,7 +442,7 @@ fw3_parse_port(void *ptr, const char *val, bool is_list)
 bool
 fw3_parse_family(void *ptr, const char *val, bool is_list)
 {
-       if (!strcmp(val, "any"))
+       if (!strcmp(val, "any") || !strcmp(val, "*"))
                *((enum fw3_family *)ptr) = FW3_FAMILY_ANY;
        else if (!strcmp(val, "inet") || strrchr(val, '4'))
                *((enum fw3_family *)ptr) = FW3_FAMILY_V4;
@@ -510,6 +535,7 @@ fw3_parse_protocol(void *ptr, const char *val, bool is_list)
 {
        struct fw3_protocol proto = { };
        struct protoent *ent;
+       char *e;
 
        if (*val == '!')
        {
@@ -517,7 +543,7 @@ fw3_parse_protocol(void *ptr, const char *val, bool is_list)
                while (isspace(*++val));
        }
 
-       if (!strcmp(val, "all"))
+       if (!strcmp(val, "all") || !strcmp(val, "any") || !strcmp(val, "*"))
        {
                proto.any = true;
                put_value(ptr, &proto, sizeof(proto), is_list);
@@ -548,9 +574,9 @@ fw3_parse_protocol(void *ptr, const char *val, bool is_list)
                return true;
        }
 
-       proto.protocol = strtoul(val, NULL, 10);
+       proto.protocol = strtoul(val, &e, 10);
 
-       if (errno == ERANGE || errno == EINVAL)
+       if ((e == val) || (*e != 0))
                return false;
 
        put_value(ptr, &proto, sizeof(proto), is_list);
@@ -569,20 +595,22 @@ fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list)
 {
        struct fw3_ipset_datatype type = { };
 
+       type.dir = "src";
+
        if (!strncmp(val, "dest_", 5))
        {
                val += 5;
-               type.dest = true;
+               type.dir = "dst";
        }
        else if (!strncmp(val, "dst_", 4))
        {
                val += 4;
-               type.dest = true;
+               type.dir = "dst";
        }
        else if (!strncmp(val, "src_", 4))
        {
                val += 4;
-               type.dest = false;
+               type.dir = "src";
        }
 
        if (parse_enum(&type.type, val, &fw3_ipset_type_names[FW3_IPSET_TYPE_IP],
@@ -690,7 +718,7 @@ fw3_parse_weekdays(void *ptr, const char *val, bool is_list)
 
        if (*val == '!')
        {
-               setbit(*(uint8_t *)ptr, 0);
+               fw3_setbit(*(uint8_t *)ptr, 0);
                while (isspace(*++val));
        }
 
@@ -710,7 +738,7 @@ fw3_parse_weekdays(void *ptr, const char *val, bool is_list)
                        }
                }
 
-               setbit(*(uint8_t *)ptr, w);
+               fw3_setbit(*(uint8_t *)ptr, w);
        }
 
        free(s);
@@ -725,7 +753,7 @@ fw3_parse_monthdays(void *ptr, const char *val, bool is_list)
 
        if (*val == '!')
        {
-               setbit(*(uint32_t *)ptr, 0);
+               fw3_setbit(*(uint32_t *)ptr, 0);
                while (isspace(*++val));
        }
 
@@ -742,7 +770,7 @@ fw3_parse_monthdays(void *ptr, const char *val, bool is_list)
                        return false;
                }
 
-               setbit(*(uint32_t *)ptr, d);
+               fw3_setbit(*(uint32_t *)ptr, d);
        }
 
        free(s);
@@ -801,8 +829,64 @@ fw3_parse_mark(void *ptr, const char *val, bool is_list)
        return true;
 }
 
+bool
+fw3_parse_setmatch(void *ptr, const char *val, bool is_list)
+{
+       struct fw3_setmatch *m = ptr;
+       char *p, *s;
+       int i;
+
+       if (*val == '!')
+       {
+               m->invert = true;
+               while (isspace(*++val));
+       }
+
+       if (!(s = strdup(val)))
+               return false;
+
+       if (!(p = strtok(s, " \t")))
+       {
+               free(s);
+               return false;
+       }
+
+       strncpy(m->name, p, sizeof(m->name));
+
+       for (i = 0, p = strtok(NULL, " \t,");
+            i < 3 && p != NULL;
+            i++, p = strtok(NULL, " \t,"))
+       {
+               if (!strncmp(p, "dest", 4) || !strncmp(p, "dst", 3))
+                       m->dir[i] = "dst";
+               else if (!strncmp(p, "src", 3))
+                       m->dir[i] = "src";
+       }
+
+       free(s);
+
+       m->set = true;
+       return true;
+}
+
+bool
+fw3_parse_direction(void *ptr, const char *val, bool is_list)
+{
+       bool *is_out = ptr;
+       bool valid = true;
+
+       if (!strcmp(val, "in") || !strcmp(val, "ingress"))
+               *is_out = false;
+       else if (!strcmp(val, "out") || !strcmp(val, "egress"))
+               *is_out = true;
+       else
+               valid = false;
+
+       return valid;
+}
+
 
-void
+bool
 fw3_parse_options(void *s, const struct fw3_option *opts,
                   struct uci_section *section)
 {
@@ -812,6 +896,7 @@ fw3_parse_options(void *s, const struct fw3_option *opts,
        struct uci_option *o;
        const struct fw3_option *opt;
        struct list_head *dest;
+       bool valid = true;
 
        uci_foreach_element(&section->options, e)
        {
@@ -831,6 +916,7 @@ fw3_parse_options(void *s, const struct fw3_option *opts,
                                if (!opt->elem_size)
                                {
                                        warn_elem(e, "must not be a list");
+                                       valid = false;
                                }
                                else
                                {
@@ -844,6 +930,7 @@ fw3_parse_options(void *s, const struct fw3_option *opts,
                                                if (!opt->parse(dest, l->name, true))
                                                {
                                                        warn_elem(e, "has invalid value '%s'", l->name);
+                                                       valid = false;
                                                        continue;
                                                }
                                        }
@@ -859,7 +946,10 @@ fw3_parse_options(void *s, const struct fw3_option *opts,
                                if (!opt->elem_size)
                                {
                                        if (!opt->parse((char *)s + opt->offset, o->v.string, false))
+                                       {
                                                warn_elem(e, "has invalid value '%s'", o->v.string);
+                                               valid = false;
+                                       }
                                }
                                else
                                {
@@ -870,6 +960,7 @@ fw3_parse_options(void *s, const struct fw3_option *opts,
                                                if (!opt->parse(dest, p, true))
                                                {
                                                        warn_elem(e, "has invalid value '%s'", p);
+                                                       valid = false;
                                                        continue;
                                                }
                                        }
@@ -883,331 +974,152 @@ fw3_parse_options(void *s, const struct fw3_option *opts,
                if (!known)
                        warn_elem(e, "is unknown");
        }
-}
-
-
-void
-fw3_format_in_out(struct fw3_device *in, struct fw3_device *out)
-{
-       if (in && !in->any)
-               fw3_pr(" %s-i %s", in->invert ? "! " : "", in->name);
 
-       if (out && !out->any)
-               fw3_pr(" %s-o %s", out->invert ? "! " : "", out->name);
+       return valid;
 }
 
-const char *
-fw3_address_to_string(struct fw3_address *address, bool allow_invert)
-{
-       char *p, ip[INET6_ADDRSTRLEN];
-       static char buf[INET6_ADDRSTRLEN * 2 + 2];
-
-       p = buf;
-
-       if (address->invert && allow_invert)
-               p += sprintf(p, "!");
-
-       inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                 &address->address.v4, ip, sizeof(ip));
-
-       p += sprintf(p, "%s", ip);
-
-       if (address->range)
-       {
-               inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                         &address->address2.v4, ip, sizeof(ip));
-
-               p += sprintf(p, "-%s", ip);
-       }
-       else
-       {
-               p += sprintf(p, "/%u", address->mask);
-       }
-
-       return buf;
-}
 
-void
-fw3_format_src_dest(struct fw3_address *src, struct fw3_address *dest)
+bool
+fw3_parse_blob_options(void *s, const struct fw3_option *opts,
+                       struct blob_attr *a, const char *name)
 {
-       if ((src && src->range) || (dest && dest->range))
-               fw3_pr(" -m iprange");
-
-       if (src && src->set)
-       {
-               fw3_pr(" %s%s %s", src->invert ? "! " : "",
-                                  src->range ? "--src-range" : "-s",
-                                  fw3_address_to_string(src, false));
-       }
+       char *p, *v, buf[16];
+       bool known;
+       unsigned rem, erem;
+       struct blob_attr *o, *e;
+       const struct fw3_option *opt;
+       struct list_head *dest;
+       bool valid = true;
 
-       if (dest && dest->set)
+       blobmsg_for_each_attr(o, a, rem)
        {
-               fw3_pr(" %s%s %s", dest->invert ? "! " : "",
-                                  dest->range ? "--dst-range" : "-d",
-                                  fw3_address_to_string(dest, false));
-       }
-}
+               known = false;
 
-void
-fw3_format_sport_dport(struct fw3_port *sp, struct fw3_port *dp)
-{
-       if (sp && sp->set)
-       {
-               if (sp->port_min == sp->port_max)
-                       fw3_pr(" %s--sport %u", sp->invert ? "! " : "", sp->port_min);
-               else
-                       fw3_pr(" %s--sport %u:%u",
-                              sp->invert ? "! " : "", sp->port_min, sp->port_max);
-       }
+               for (opt = opts; opt->name; opt++)
+               {
+                       if (!opt->parse)
+                               continue;
 
-       if (dp && dp->set)
-       {
-               if (dp->port_min == dp->port_max)
-                       fw3_pr(" %s--dport %u", dp->invert ? "! " : "", dp->port_min);
-               else
-                       fw3_pr(" %s--dport %u:%u",
-                              dp->invert ? "! " : "", dp->port_min, dp->port_max);
-       }
-}
+                       if (strcmp(opt->name, blobmsg_name(o)))
+                               continue;
 
-void
-fw3_format_mac(struct fw3_mac *mac)
-{
-       if (!mac)
-               return;
+                       if (blobmsg_type(o) == BLOBMSG_TYPE_ARRAY)
+                       {
+                               if (!opt->elem_size)
+                               {
+                                       fprintf(stderr, "%s: '%s' must not be a list\n",
+                                               name, opt->name);
 
-       fw3_pr(" -m mac %s--mac-source %s",
-              mac->invert ? "! " : "", ether_ntoa(&mac->mac));
-}
+                                       valid = false;
+                               }
+                               else
+                               {
+                                       dest = (struct list_head *)((char *)s + opt->offset);
 
-void
-fw3_format_protocol(struct fw3_protocol *proto, enum fw3_family family)
-{
-       uint16_t pr;
+                                       blobmsg_for_each_attr(e, o, erem)
+                                       {
+                                               if (blobmsg_type(e) == BLOBMSG_TYPE_INT32) {
+                                                       snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(e));
+                                                       v = buf;
+                                               } else {
+                                                       v = blobmsg_get_string(e);
+                                               }
 
-       if (!proto)
-               return;
+                                               if (!opt->parse(dest, v, true))
+                                               {
+                                                       fprintf(stderr, "%s: '%s' has invalid value '%s'\n",
+                                                               name, opt->name, v);
+                                                       valid = false;
+                                                       continue;
+                                               }
+                                       }
+                               }
+                       }
+                       else
+                       {
+                               if (blobmsg_type(o) == BLOBMSG_TYPE_INT32) {
+                                       snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(o));
+                                       v = buf;
+                               } else {
+                                       v = blobmsg_get_string(o);
+                               }
 
-       pr = proto->protocol;
+                               if (!v)
+                                       continue;
 
-       if (pr == 1 && family == FW3_FAMILY_V6)
-               pr = 58;
+                               if (!opt->elem_size)
+                               {
+                                       if (!opt->parse((char *)s + opt->offset, v, false))
+                                       {
+                                               fprintf(stderr, "%s: '%s' has invalid value '%s'\n",
+                                                       name, opt->name, v);
+                                               valid = false;
+                                       }
+                               }
+                               else
+                               {
+                                       dest = (struct list_head *)((char *)s + opt->offset);
 
-       if (proto->any)
-               fw3_pr(" -p all");
-       else
-               fw3_pr(" %s-p %u", proto->invert ? "! " : "", pr);
-}
+                                       for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
+                                       {
+                                               if (!opt->parse(dest, p, true))
+                                               {
+                                                       fprintf(stderr, "%s: '%s' has invalid value '%s'\n",
+                                                               name, opt->name, p);
+                                                       valid = false;
+                                                       continue;
+                                               }
+                                       }
+                               }
+                       }
 
-void
-fw3_format_icmptype(struct fw3_icmptype *icmp, enum fw3_family family)
-{
-       if (!icmp)
-               return;
+                       known = true;
+                       break;
+               }
 
-       if (family != FW3_FAMILY_V6)
-       {
-               if (icmp->code_min == 0 && icmp->code_max == 0xFF)
-                       fw3_pr(" %s--icmp-type %u", icmp->invert ? "! " : "", icmp->type);
-               else
-                       fw3_pr(" %s--icmp-type %u/%u",
-                                  icmp->invert ? "! " : "", icmp->type, icmp->code_min);
+               if (!known && strcmp(blobmsg_name(o), "type"))
+                       fprintf(stderr, "%s: '%s' is unknown\n", name, blobmsg_name(o));
        }
-       else
-       {
-               if (icmp->code6_min == 0 && icmp->code6_max == 0xFF)
-                       fw3_pr(" %s--icmpv6-type %u", icmp->invert ? "! " : "", icmp->type6);
-               else
-                       fw3_pr(" %s--icmpv6-type %u/%u",
-                                  icmp->invert ? "! " : "", icmp->type6, icmp->code6_min);
-       }
-}
-
-void
-fw3_format_limit(struct fw3_limit *limit)
-{
-       if (!limit)
-               return;
-
-       if (limit->rate > 0)
-       {
-               fw3_pr(" -m limit %s--limit %u/%s",
-                      limit->invert ? "! " : "",
-                      limit->rate, limit_units[limit->unit]);
 
-               if (limit->burst > 0)
-                       fw3_pr(" --limit-burst %u", limit->burst);
-       }
+       return valid;
 }
 
-void
-fw3_format_ipset(struct fw3_ipset *ipset, bool invert)
-{
-       bool first = true;
-       const char *name = NULL;
-       struct fw3_ipset_datatype *type;
-
-       if (!ipset)
-               return;
-
-       if (ipset->external && *ipset->external)
-               name = ipset->external;
-       else
-               name = ipset->name;
-
-       fw3_pr(" -m set %s--match-set %s", invert ? "! " : "", name);
-
-       list_for_each_entry(type, &ipset->datatypes, list)
-       {
-               fw3_pr("%c%s", first ? ' ' : ',', type->dest ? "dst" : "src");
-               first = false;
-       }
-}
 
-void
-fw3_format_time(struct fw3_time *time)
+const char *
+fw3_address_to_string(struct fw3_address *address, bool allow_invert, bool as_cidr)
 {
-       int i;
-       struct tm empty = { 0 };
-       char buf[sizeof("9999-99-99T23:59:59\0")];
-       bool d1 = memcmp(&time->datestart, &empty, sizeof(empty));
-       bool d2 = memcmp(&time->datestop, &empty, sizeof(empty));
-       bool first;
-
-       if (!d1 && !d2 && !time->timestart && !time->timestop &&
-           !(time->monthdays & 0xFFFFFFFE) && !(time->weekdays & 0xFE))
-       {
-               return;
-       }
-
-       fw3_pr(" -m time");
-
-       if (time->utc)
-               fw3_pr(" --utc");
+       char *p, ip[INET6_ADDRSTRLEN];
+       static char buf[INET6_ADDRSTRLEN * 2 + 2];
 
-       if (d1)
-       {
-               strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestart);
-               fw3_pr(" --datestart %s", buf);
-       }
+       p = buf;
 
-       if (d2)
-       {
-               strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestop);
-               fw3_pr(" --datestop %s", buf);
-       }
+       if (address->invert && allow_invert)
+               p += sprintf(p, "!");
 
-       if (time->timestart)
-       {
-               fw3_pr(" --timestart %02d:%02d:%02d",
-                      time->timestart / 3600,
-                      time->timestart % 3600 / 60,
-                      time->timestart % 60);
-       }
+       inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
+                 &address->address.v4, ip, sizeof(ip));
 
-       if (time->timestop)
-       {
-               fw3_pr(" --timestop %02d:%02d:%02d",
-                      time->timestop / 3600,
-                      time->timestop % 3600 / 60,
-                      time->timestop % 60);
-       }
+       p += sprintf(p, "%s", ip);
 
-       if (time->monthdays & 0xFFFFFFFE)
+       if (address->range)
        {
-               fw3_pr(" %s--monthdays", hasbit(time->monthdays, 0) ? "! " : "");
+               inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
+                         &address->mask.v4, ip, sizeof(ip));
 
-               for (i = 1, first = true; i < 32; i++)
-               {
-                       if (hasbit(time->monthdays, i))
-                       {
-                               fw3_pr("%c%u", first ? ' ' : ',', i);
-                               first = false;
-                       }
-               }
+               p += sprintf(p, "-%s", ip);
        }
-
-       if (time->weekdays & 0xFE)
+       else if (!as_cidr)
        {
-               fw3_pr(" %s--weekdays", hasbit(time->weekdays, 0) ? "! " : "");
+               inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
+                         &address->mask.v4, ip, sizeof(ip));
 
-               for (i = 1, first = true; i < 8; i++)
-               {
-                       if (hasbit(time->weekdays, i))
-                       {
-                               fw3_pr("%c%u", first ? ' ' : ',', i);
-                               first = false;
-                       }
-               }
+               p += sprintf(p, "/%s", ip);
        }
-}
-
-void
-fw3_format_mark(struct fw3_mark *mark)
-{
-       if (!mark->set)
-               return;
-
-       fw3_pr(" -m mark %s--mark 0x%x", mark->invert ? "! " : "", mark->mark);
-
-       if (mark->mask < 0xFFFFFFFF)
-               fw3_pr("/0x%x", mark->mask);
-}
-
-void
-__fw3_format_comment(const char *comment, ...)
-{
-       va_list ap;
-       int len = 0;
-       const char *c;
-
-       if (!comment || !*comment)
-               return;
-
-       fw3_pr(" -m comment --comment \"");
-
-       c = comment;
-
-       va_start(ap, comment);
-
-       do
+       else
        {
-               while (*c)
-               {
-                       switch (*c)
-                       {
-                       case '"':
-                       case '$':
-                       case '`':
-                       case '\\':
-                               fw3_pr("\\");
-                               /* fall through */
-
-                       default:
-                               fw3_pr("%c", *c);
-                               break;
-                       }
-
-                       c++;
-
-                       if (len++ >= 255)
-                               goto end;
-               }
-
-               c = va_arg(ap, const char *);
+               p += sprintf(p, "/%u", fw3_netmask2bitlen(address->family,
+                                                         &address->mask.v6));
        }
-       while (c);
-
-end:
-       va_end(ap);
-       fw3_pr("\"");
-}
-
-void
-fw3_format_extra(const char *extra)
-{
-       if (!extra || !*extra)
-               return;
 
-       fw3_pr(" %s", extra);
+       return buf;
 }