]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
libxtables: xt_xlate_add() to take care of spacing
authorPhil Sutter <phil@nwl.cc>
Fri, 25 Nov 2022 02:13:14 +0000 (03:13 +0100)
committerPhil Sutter <phil@nwl.cc>
Tue, 29 Nov 2022 12:25:26 +0000 (13:25 +0100)
Try to eliminate most of the whitespace issues by separating strings
from separate xt_xlate_add() calls by whitespace if needed.

Cover the common case of consecutive range, list or MAC/IP address
printing by inserting whitespace only if the string to be appended
starts with an alphanumeric character or a brace. The latter helps to
make spacing in anonymous sets consistent.

Provide *_nospc() variants which disable the auto-spacing for the
mandatory exception to the rule.

Make things round by dropping any trailing whitespace before returning
the buffer via xt_xlate_get().

Signed-off-by: Phil Sutter <phil@nwl.cc>
extensions/libxt_dccp.txlate
extensions/libxt_hashlimit.c
extensions/libxt_hashlimit.txlate
extensions/libxt_multiport.txlate
extensions/libxt_tcp.c
extensions/libxt_time.txlate
include/xtables.h
libxtables/xtables.c

index ea853f6acf627d3d0e0cccd732cc97a73de43c89..be950bcb6dbda3834139f6e2a3adcb845d833d7e 100644 (file)
@@ -14,7 +14,7 @@ iptables-translate -A INPUT -p dccp -m dccp --dccp-types INVALID
 nft add rule ip filter INPUT dccp type 10-15 counter
 
 iptables-translate -A INPUT -p dccp -m dccp --dport 100 --dccp-types REQUEST,RESPONSE,DATA,ACK,DATAACK,CLOSEREQ,CLOSE,SYNC,SYNCACK,INVALID
-nft add rule ip filter INPUT dccp dport 100 dccp type {request, response, data, ack, dataack, closereq, close, sync, syncack, 10-15} counter
+nft add rule ip filter INPUT dccp dport 100 dccp type { request, response, data, ack, dataack, closereq, close, sync, syncack, 10-15 } counter
 
 iptables-translate -A INPUT -p dccp -m dccp --sport 200 --dport 100
 nft add rule ip filter INPUT dccp sport 200 dccp dport 100 counter
index 93ee1c32e54c3180b81940c756f8b2a3ed2518da..24e784ab1ab10ae8891114070c2c9e6dfe145393 100644 (file)
@@ -1270,7 +1270,7 @@ static void hashlimit_print_subnet_xlate(struct xt_xlate *xl,
                        }
                }
 
-               xt_xlate_add(xl, fmt, acm);
+               xt_xlate_add_nospc(xl, fmt, acm);
                if (nblocks > 0)
                        xt_xlate_add(xl, "%c", sep);
        }
index 6c8d07f113d26e9ad3067941e7abe8503ffbce8c..251a30d371db42b9a6164082f3beeb6c31c7dd46 100644 (file)
@@ -1,5 +1,5 @@
 iptables-translate -A OUTPUT -m tcp -p tcp --dport 443 -m hashlimit --hashlimit-above 20kb/s --hashlimit-burst 1mb --hashlimit-mode dstip --hashlimit-name https --hashlimit-dstmask 24 -m state --state NEW -j DROP
-nft add rule ip filter OUTPUT tcp dport 443 meter https { ip daddr and 255.255.255.0 timeout 60s limit rate over 20 kbytes/second burst 1 mbytes} ct state new  counter drop
+nft add rule ip filter OUTPUT tcp dport 443 meter https { ip daddr and 255.255.255.0 timeout 60s limit rate over 20 kbytes/second burst 1 mbytes } ct state new  counter drop
 
 iptables-translate -A OUTPUT -m tcp -p tcp --dport 443 -m hashlimit --hashlimit-upto 300 --hashlimit-burst 15 --hashlimit-mode srcip,dstip --hashlimit-name https --hashlimit-htable-expire 300000 -m state --state NEW -j DROP
-nft add rule ip filter OUTPUT tcp dport 443 meter https { ip daddr . ip saddr timeout 300s limit rate 300/second burst 15 packets} ct state new  counter drop
+nft add rule ip filter OUTPUT tcp dport 443 meter https { ip daddr . ip saddr timeout 300s limit rate 300/second burst 15 packets } ct state new  counter drop
index bced1b84c447e38b4e6236d2b04c1214f64a2cb0..bf0152650d79ebb308d58b5e7fee312d8820d99c 100644 (file)
@@ -1,5 +1,5 @@
 iptables-translate -t filter -A INPUT -p tcp -m multiport --dports 80,81 -j ACCEPT
-nft add rule ip filter INPUT ip protocol tcp tcp dport { 80,81} counter accept
+nft add rule ip filter INPUT ip protocol tcp tcp dport { 80,81 } counter accept
 
 iptables-translate -t filter -A INPUT -p tcp -m multiport --dports 80:88 -j ACCEPT
 nft add rule ip filter INPUT ip protocol tcp tcp dport 80-88 counter accept
index 043382d47b8ba56e4f23275012975e8bd238a4cf..2ef842990a4e8cc389f711f50fffaa2894c88270 100644 (file)
@@ -380,10 +380,9 @@ static void print_tcp_xlate(struct xt_xlate *xl, uint8_t flags)
 
                for (i = 0; (flags & tcp_flag_names_xlate[i].flag) == 0; i++);
 
-               if (have_flag)
-                       xt_xlate_add(xl, ",");
-
-               xt_xlate_add(xl, "%s", tcp_flag_names_xlate[i].name);
+               xt_xlate_add(xl, "%s%s",
+                            have_flag ? "," : "",
+                            tcp_flag_names_xlate[i].name);
                have_flag = 1;
 
                flags &= ~tcp_flag_names_xlate[i].flag;
index ff4a7b88a87428f79e9b8bf5cddd6ce5e13c969b..2083ab94f4c24f4dad9f5ecf1da2aa43684130c6 100644 (file)
@@ -1,5 +1,5 @@
 iptables-translate -A INPUT -p icmp --icmp-type echo-request -m time --weekdays Sa,Su -j REJECT
-nft add rule ip filter INPUT icmp type echo-request  meta day {6,0} counter reject
+nft add rule ip filter INPUT icmp type echo-request  meta day { 6,0 } counter reject
 
 iptables-translate -A INPUT -p icmp --icmp-type echo-request -m time --timestart 12:00 -j REJECT
 nft add rule ip filter INPUT icmp type echo-request  meta hour "12:00:00"-"23:59:59" counter reject
@@ -20,7 +20,7 @@ iptables-translate -A INPUT -p icmp --icmp-type echo-request -m time --datestart
 nft add rule ip filter INPUT icmp type echo-request meta time "2020-01-29 00:00:00"-"2038-01-19 03:14:07" meta hour "12:00:00"-"23:59:59" counter reject
 
 iptables-translate -A INPUT -p icmp --icmp-type echo-request -m time --datestart 2020-01-29T00:00:00 --timestart 12:00 --timestop 19:00 --weekdays Mon,Tue,Wed,Thu,Fri -j REJECT
-nft add rule ip filter INPUT icmp type echo-request meta time "2020-01-29 00:00:00"-"2038-01-19 03:14:07" meta hour "12:00:00"-"19:00:00" meta day {1,2,3,4,5} counter reject
+nft add rule ip filter INPUT icmp type echo-request meta time "2020-01-29 00:00:00"-"2038-01-19 03:14:07" meta hour "12:00:00"-"19:00:00" meta day { 1,2,3,4,5 } counter reject
 
 iptables-translate -A INPUT -p icmp --icmp-type echo-request -m time --datestart 2020-01-29T00:00:00 --timestart 12:00 --timestop 19:00 ! --weekdays Mon,Tue,Wed,Thu,Fri -j REJECT
-nft add rule ip filter INPUT icmp type echo-request meta time "2020-01-29 00:00:00"-"2038-01-19 03:14:07" meta hour "12:00:00"-"19:00:00" meta day {6,0} counter reject
+nft add rule ip filter INPUT icmp type echo-request meta time "2020-01-29 00:00:00"-"2038-01-19 03:14:07" meta hour "12:00:00"-"19:00:00" meta day { 6,0 } counter reject
index 9eba4f619d35183f049d1b0e19a65e459b22e3ce..dad1949e553700dfc82ec89ca78db9acb71af76b 100644 (file)
@@ -621,8 +621,11 @@ extern const char *xtables_lmap_id2name(const struct xtables_lmap *, int);
 struct xt_xlate *xt_xlate_alloc(int size);
 void xt_xlate_free(struct xt_xlate *xl);
 void xt_xlate_add(struct xt_xlate *xl, const char *fmt, ...) __attribute__((format(printf,2,3)));
+void xt_xlate_add_nospc(struct xt_xlate *xl, const char *fmt, ...) __attribute__((format(printf,2,3)));
 #define xt_xlate_rule_add xt_xlate_add
+#define xt_xlate_rule_add_nospc xt_xlate_add_nospc
 void xt_xlate_set_add(struct xt_xlate *xl, const char *fmt, ...) __attribute__((format(printf,2,3)));
+void xt_xlate_set_add_nospc(struct xt_xlate *xl, const char *fmt, ...) __attribute__((format(printf,2,3)));
 void xt_xlate_add_comment(struct xt_xlate *xl, const char *comment);
 const char *xt_xlate_get_comment(struct xt_xlate *xl);
 void xl_xlate_set_family(struct xt_xlate *xl, uint8_t family);
index 479dbae0781568379fbbd679c16cf300c11b0406..e3e444acbbaa26c019ee93eff9c47e6c48dd367d 100644 (file)
@@ -2490,16 +2490,39 @@ void xt_xlate_free(struct xt_xlate *xl)
        free(xl);
 }
 
+static bool isbrace(char c)
+{
+       switch (c) {
+       case '(':
+       case ')':
+       case '{':
+       case '}':
+       case '[':
+       case ']':
+               return true;
+       }
+       return false;
+}
+
 static void __xt_xlate_add(struct xt_xlate *xl, enum xt_xlate_type type,
-                          const char *fmt, va_list ap)
+                          bool space, const char *fmt, va_list ap)
 {
        struct xt_xlate_buf *buf = &xl->buf[type];
+       char tmpbuf[1024] = "";
        int len;
 
-       len = vsnprintf(buf->data + buf->off, buf->rem, fmt, ap);
-       if (len < 0 || len >= buf->rem)
+       len = vsnprintf(tmpbuf, 1024, fmt, ap);
+       if (len < 0 || len >= buf->rem - 1)
                xtables_error(RESOURCE_PROBLEM, "OOM");
 
+       if (space && buf->off &&
+           !isspace(buf->data[buf->off - 1]) &&
+           (isalnum(tmpbuf[0]) || isbrace(tmpbuf[0]))) {
+               buf->data[buf->off] = ' ';
+               buf->off++;
+               buf->rem--;
+       }
+       sprintf(buf->data + buf->off, "%s", tmpbuf);
        buf->rem -= len;
        buf->off += len;
 }
@@ -2509,7 +2532,16 @@ void xt_xlate_rule_add(struct xt_xlate *xl, const char *fmt, ...)
        va_list ap;
 
        va_start(ap, fmt);
-       __xt_xlate_add(xl, XT_XLATE_RULE, fmt, ap);
+       __xt_xlate_add(xl, XT_XLATE_RULE, true, fmt, ap);
+       va_end(ap);
+}
+
+void xt_xlate_rule_add_nospc(struct xt_xlate *xl, const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       __xt_xlate_add(xl, XT_XLATE_RULE, false, fmt, ap);
        va_end(ap);
 }
 
@@ -2518,7 +2550,16 @@ void xt_xlate_set_add(struct xt_xlate *xl, const char *fmt, ...)
        va_list ap;
 
        va_start(ap, fmt);
-       __xt_xlate_add(xl, XT_XLATE_SET, fmt, ap);
+       __xt_xlate_add(xl, XT_XLATE_SET, true, fmt, ap);
+       va_end(ap);
+}
+
+void xt_xlate_set_add_nospc(struct xt_xlate *xl, const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       __xt_xlate_add(xl, XT_XLATE_SET, false, fmt, ap);
        va_end(ap);
 }
 
@@ -2545,7 +2586,12 @@ uint8_t xt_xlate_get_family(struct xt_xlate *xl)
 
 const char *xt_xlate_get(struct xt_xlate *xl)
 {
-       return xl->buf[XT_XLATE_RULE].data;
+       struct xt_xlate_buf *buf = &xl->buf[XT_XLATE_RULE];
+
+       while (buf->off && isspace(buf->data[buf->off - 1]))
+               buf->data[--buf->off] = '\0';
+
+       return buf->data;
 }
 
 const char *xt_xlate_set_get(struct xt_xlate *xl)