Blame SOURCES/0043-extensions-dccp-Fix-for-DCCP-type-INVALID.patch

b144b7
From 98794894774a39927bc975921fc9e40f00db937b Mon Sep 17 00:00:00 2001
b144b7
From: Phil Sutter <phil@nwl.cc>
b144b7
Date: Wed, 2 Dec 2020 13:37:06 +0100
b144b7
Subject: [PATCH] extensions: dccp: Fix for DCCP type 'INVALID'
b144b7
b144b7
Support for matching on invalid DCCP type field values was pretty
b144b7
broken: While RFC4340 declares any type value from 10 to 15 invalid, the
b144b7
extension's type name 'INVALID' mapped to type value 10 only. Fix this
b144b7
by introduction of INVALID_OTHER_TYPE_MASK which has the remaining
b144b7
invalid type's bits set and apply it if bit 10 is set after parsing the
b144b7
type list. When printing, stop searching type names after printing
b144b7
'INVALID' - unless numeric output was requested. The latter prints all
b144b7
actual type values. Since parsing types in numeric form is not
b144b7
supported, changing the output should not break existing scripts.
b144b7
b144b7
When translating into nftables syntax, the code returned prematurely if
b144b7
'INVALID' was among the list of types - thereby emitting invalid syntax.
b144b7
Instead print a real match for invalid types by use of a range
b144b7
expression.
b144b7
b144b7
While being at it, fix syntax of translator output: If only
b144b7
'--dccp-types' was translated, the output contained an extra 'dccp'. On
b144b7
the other hand, if '--sport' and '--dport' was present, a required
b144b7
'dccp' between the translations of both was missing.
b144b7
b144b7
Fixes: e40b11d7ef827 ("add support for new 'dccp' protocol match")
b144b7
Fixes: c94a998724143 ("extensions: libxt_dccp: Add translation to nft")
b144b7
Signed-off-by: Phil Sutter <phil@nwl.cc>
b144b7
(cherry picked from commit 4bcbc8e11a2764f4537dc405962f83cd072cccfe)
b144b7
Signed-off-by: Phil Sutter <psutter@redhat.com>
b144b7
---
b144b7
 extensions/libxt_dccp.c      | 58 ++++++++++++++++++++++--------------
b144b7
 extensions/libxt_dccp.txlate | 12 ++++++--
b144b7
 2 files changed, 45 insertions(+), 25 deletions(-)
b144b7
b144b7
diff --git a/extensions/libxt_dccp.c b/extensions/libxt_dccp.c
b144b7
index 5e67c264db2a9..aea3e20be4818 100644
b144b7
--- a/extensions/libxt_dccp.c
b144b7
+++ b/extensions/libxt_dccp.c
b144b7
@@ -76,6 +76,9 @@ static const char *const dccp_pkt_types[] = {
b144b7
 	[DCCP_PKT_INVALID]	= "INVALID",
b144b7
 };
b144b7
 
b144b7
+/* Bits for type values 11-15 */
b144b7
+#define INVALID_OTHER_TYPE_MASK		0xf800
b144b7
+
b144b7
 static uint16_t
b144b7
 parse_dccp_types(const char *typestring)
b144b7
 {
b144b7
@@ -95,6 +98,9 @@ parse_dccp_types(const char *typestring)
b144b7
 			xtables_error(PARAMETER_PROBLEM,
b144b7
 				   "Unknown DCCP type `%s'", ptr);
b144b7
 	}
b144b7
+	if (typemask & (1 << DCCP_PKT_INVALID))
b144b7
+		typemask |= INVALID_OTHER_TYPE_MASK;
b144b7
+
b144b7
 
b144b7
 	free(buffer);
b144b7
 	return typemask;
b144b7
@@ -193,9 +199,13 @@ print_types(uint16_t types, int inverted, int numeric)
b144b7
 
b144b7
 		if (numeric)
b144b7
 			printf("%u", i);
b144b7
-		else
b144b7
+		else {
b144b7
 			printf("%s", dccp_pkt_types[i]);
b144b7
 
b144b7
+			if (i == DCCP_PKT_INVALID)
b144b7
+				break;
b144b7
+		}
b144b7
+
b144b7
 		types &= ~(1 << i);
b144b7
 	}
b144b7
 }
b144b7
@@ -288,6 +298,7 @@ static const char *const dccp_pkt_types_xlate[] = {
b144b7
 	[DCCP_PKT_RESET]        = "reset",
b144b7
 	[DCCP_PKT_SYNC]         = "sync",
b144b7
 	[DCCP_PKT_SYNCACK]      = "syncack",
b144b7
+	[DCCP_PKT_INVALID]	= "10-15",
b144b7
 };
b144b7
 
b144b7
 static int dccp_type_xlate(const struct xt_dccp_info *einfo,
b144b7
@@ -296,10 +307,10 @@ static int dccp_type_xlate(const struct xt_dccp_info *einfo,
b144b7
 	bool have_type = false, set_need = false;
b144b7
 	uint16_t types = einfo->typemask;
b144b7
 
b144b7
-	if (types & (1 << DCCP_PKT_INVALID))
b144b7
-		return 0;
b144b7
-
b144b7
-	xt_xlate_add(xl, " dccp type%s ", einfo->invflags ? " !=" : "");
b144b7
+	if (types & INVALID_OTHER_TYPE_MASK) {
b144b7
+		types &= ~INVALID_OTHER_TYPE_MASK;
b144b7
+		types |= 1 << DCCP_PKT_INVALID;
b144b7
+	}
b144b7
 
b144b7
 	if ((types != 0) && !(types == (types & -types))) {
b144b7
 		xt_xlate_add(xl, "{");
b144b7
@@ -335,34 +346,37 @@ static int dccp_xlate(struct xt_xlate *xl,
b144b7
 	char *space = "";
b144b7
 	int ret = 1;
b144b7
 
b144b7
-	xt_xlate_add(xl, "dccp ");
b144b7
-
b144b7
 	if (einfo->flags & XT_DCCP_SRC_PORTS) {
b144b7
+		xt_xlate_add(xl, "dccp sport%s %u",
b144b7
+			     einfo->invflags & XT_DCCP_SRC_PORTS ? " !=" : "",
b144b7
+			     einfo->spts[0]);
b144b7
+
b144b7
 		if (einfo->spts[0] != einfo->spts[1])
b144b7
-			xt_xlate_add(xl, "sport%s %u-%u",
b144b7
-				     einfo->invflags & XT_DCCP_SRC_PORTS ? " !=" : "",
b144b7
-				     einfo->spts[0], einfo->spts[1]);
b144b7
-		else
b144b7
-			xt_xlate_add(xl, "sport%s %u",
b144b7
-				     einfo->invflags & XT_DCCP_SRC_PORTS ? " !=" : "",
b144b7
-				     einfo->spts[0]);
b144b7
+			xt_xlate_add(xl, "-%u", einfo->spts[1]);
b144b7
+
b144b7
 		space = " ";
b144b7
 	}
b144b7
 
b144b7
 	if (einfo->flags & XT_DCCP_DEST_PORTS) {
b144b7
+		xt_xlate_add(xl, "%sdccp dport%s %u", space,
b144b7
+			     einfo->invflags & XT_DCCP_DEST_PORTS ? " !=" : "",
b144b7
+			     einfo->dpts[0]);
b144b7
+
b144b7
 		if (einfo->dpts[0] != einfo->dpts[1])
b144b7
-			xt_xlate_add(xl, "%sdport%s %u-%u", space,
b144b7
-				     einfo->invflags & XT_DCCP_DEST_PORTS ? " !=" : "",
b144b7
-				     einfo->dpts[0], einfo->dpts[1]);
b144b7
-		else
b144b7
-			xt_xlate_add(xl, "%sdport%s %u", space,
b144b7
-				     einfo->invflags & XT_DCCP_DEST_PORTS ? " !=" : "",
b144b7
-				     einfo->dpts[0]);
b144b7
+			xt_xlate_add(xl, "-%u", einfo->dpts[1]);
b144b7
+
b144b7
+		space = " ";
b144b7
 	}
b144b7
 
b144b7
-	if (einfo->flags & XT_DCCP_TYPE)
b144b7
+	if (einfo->flags & XT_DCCP_TYPE && einfo->typemask) {
b144b7
+		xt_xlate_add(xl, "%sdccp type%s ", space,
b144b7
+			     einfo->invflags & XT_DCCP_TYPE ? " !=" : "");
b144b7
 		ret = dccp_type_xlate(einfo, xl);
b144b7
 
b144b7
+		space = " ";
b144b7
+	}
b144b7
+
b144b7
+	/* FIXME: no dccp option support in nftables yet */
b144b7
 	if (einfo->flags & XT_DCCP_OPTION)
b144b7
 		ret = 0;
b144b7
 
b144b7
diff --git a/extensions/libxt_dccp.txlate b/extensions/libxt_dccp.txlate
b144b7
index b47dc65f5bc4f..ea853f6acf627 100644
b144b7
--- a/extensions/libxt_dccp.txlate
b144b7
+++ b/extensions/libxt_dccp.txlate
b144b7
@@ -7,8 +7,14 @@ nft add rule ip filter INPUT dccp dport 100-200 counter
b144b7
 iptables-translate -A INPUT -p dccp -m dccp ! --dport 100
b144b7
 nft add rule ip filter INPUT dccp dport != 100 counter
b144b7
 
b144b7
-iptables-translate -A INPUT -p dccp -m dccp --dport 100 --dccp-types REQUEST,RESPONSE,DATA,ACK,DATAACK,CLOSEREQ,CLOSE,SYNC,SYNCACK
b144b7
-nft add rule ip filter INPUT dccp dport 100 dccp type {request, response, data, ack, dataack, closereq, close, sync, syncack} counter
b144b7
+iptables-translate -A INPUT -p dccp -m dccp --dccp-types CLOSE
b144b7
+nft add rule ip filter INPUT dccp type close counter
b144b7
+
b144b7
+iptables-translate -A INPUT -p dccp -m dccp --dccp-types INVALID
b144b7
+nft add rule ip filter INPUT dccp type 10-15 counter
b144b7
+
b144b7
+iptables-translate -A INPUT -p dccp -m dccp --dport 100 --dccp-types REQUEST,RESPONSE,DATA,ACK,DATAACK,CLOSEREQ,CLOSE,SYNC,SYNCACK,INVALID
b144b7
+nft add rule ip filter INPUT dccp dport 100 dccp type {request, response, data, ack, dataack, closereq, close, sync, syncack, 10-15} counter
b144b7
 
b144b7
 iptables-translate -A INPUT -p dccp -m dccp --sport 200 --dport 100
b144b7
-nft add rule ip filter INPUT dccp sport 200 dport 100 counter
b144b7
+nft add rule ip filter INPUT dccp sport 200 dccp dport 100 counter
b144b7
-- 
b144b7
2.28.0
b144b7