|
|
926f74 |
From 48f38c4224f31d19176df83539501292fcc6092b Mon Sep 17 00:00:00 2001
|
|
|
926f74 |
From: Phil Sutter <phil@nwl.cc>
|
|
|
926f74 |
Date: Tue, 4 May 2021 16:26:42 +0200
|
|
|
926f74 |
Subject: [PATCH] extensions: sctp: Translate --chunk-types option
|
|
|
926f74 |
|
|
|
926f74 |
The translation is not fully complete as it is not possible to map 'any'
|
|
|
926f74 |
match type into nft syntax with a single rule. Also, 'only' match type
|
|
|
926f74 |
translation is a bit poor as it explicitly lists all chunk types that
|
|
|
926f74 |
are supposed to be missing.
|
|
|
926f74 |
|
|
|
926f74 |
Signed-off-by: Phil Sutter <phil@nwl.cc>
|
|
|
926f74 |
(cherry picked from commit 5818be177110a09120dd8fe4bd2533acbf8da301)
|
|
|
926f74 |
---
|
|
|
926f74 |
extensions/libxt_sctp.c | 91 ++++++++++++++++++++++++++++--------
|
|
|
926f74 |
extensions/libxt_sctp.txlate | 6 +++
|
|
|
926f74 |
2 files changed, 78 insertions(+), 19 deletions(-)
|
|
|
926f74 |
|
|
|
926f74 |
diff --git a/extensions/libxt_sctp.c b/extensions/libxt_sctp.c
|
|
|
926f74 |
index ee4e99ebf11bf..5d8ab85cacf42 100644
|
|
|
926f74 |
--- a/extensions/libxt_sctp.c
|
|
|
926f74 |
+++ b/extensions/libxt_sctp.c
|
|
|
926f74 |
@@ -92,28 +92,29 @@ struct sctp_chunk_names {
|
|
|
926f74 |
const char *name;
|
|
|
926f74 |
unsigned int chunk_type;
|
|
|
926f74 |
const char *valid_flags;
|
|
|
926f74 |
+ const char *nftname;
|
|
|
926f74 |
};
|
|
|
926f74 |
|
|
|
926f74 |
/*'ALL' and 'NONE' will be treated specially. */
|
|
|
926f74 |
static const struct sctp_chunk_names sctp_chunk_names[]
|
|
|
926f74 |
-= { { .name = "DATA", .chunk_type = 0, .valid_flags = "----IUBE"},
|
|
|
926f74 |
- { .name = "INIT", .chunk_type = 1, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "INIT_ACK", .chunk_type = 2, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "SACK", .chunk_type = 3, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "HEARTBEAT", .chunk_type = 4, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "HEARTBEAT_ACK", .chunk_type = 5, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "ABORT", .chunk_type = 6, .valid_flags = "-------T"},
|
|
|
926f74 |
- { .name = "SHUTDOWN", .chunk_type = 7, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "SHUTDOWN_ACK", .chunk_type = 8, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "ERROR", .chunk_type = 9, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "COOKIE_ECHO", .chunk_type = 10, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "COOKIE_ACK", .chunk_type = 11, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "ECN_ECNE", .chunk_type = 12, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "ECN_CWR", .chunk_type = 13, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "SHUTDOWN_COMPLETE", .chunk_type = 14, .valid_flags = "-------T"},
|
|
|
926f74 |
- { .name = "ASCONF", .chunk_type = 193, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "ASCONF_ACK", .chunk_type = 128, .valid_flags = "--------"},
|
|
|
926f74 |
- { .name = "FORWARD_TSN", .chunk_type = 192, .valid_flags = "--------"},
|
|
|
926f74 |
+= { { .name = "DATA", .chunk_type = 0, .valid_flags = "----IUBE", .nftname = "data" },
|
|
|
926f74 |
+ { .name = "INIT", .chunk_type = 1, .valid_flags = "--------", .nftname = "init" },
|
|
|
926f74 |
+ { .name = "INIT_ACK", .chunk_type = 2, .valid_flags = "--------", .nftname = "init-ack" },
|
|
|
926f74 |
+ { .name = "SACK", .chunk_type = 3, .valid_flags = "--------", .nftname = "sack" },
|
|
|
926f74 |
+ { .name = "HEARTBEAT", .chunk_type = 4, .valid_flags = "--------", .nftname = "heartbeat" },
|
|
|
926f74 |
+ { .name = "HEARTBEAT_ACK", .chunk_type = 5, .valid_flags = "--------", .nftname = "heartbeat-ack" },
|
|
|
926f74 |
+ { .name = "ABORT", .chunk_type = 6, .valid_flags = "-------T", .nftname = "abort" },
|
|
|
926f74 |
+ { .name = "SHUTDOWN", .chunk_type = 7, .valid_flags = "--------", .nftname = "shutdown" },
|
|
|
926f74 |
+ { .name = "SHUTDOWN_ACK", .chunk_type = 8, .valid_flags = "--------", .nftname = "shutdown-ack" },
|
|
|
926f74 |
+ { .name = "ERROR", .chunk_type = 9, .valid_flags = "--------", .nftname = "error" },
|
|
|
926f74 |
+ { .name = "COOKIE_ECHO", .chunk_type = 10, .valid_flags = "--------", .nftname = "cookie-echo" },
|
|
|
926f74 |
+ { .name = "COOKIE_ACK", .chunk_type = 11, .valid_flags = "--------", .nftname = "cookie-ack" },
|
|
|
926f74 |
+ { .name = "ECN_ECNE", .chunk_type = 12, .valid_flags = "--------", .nftname = "ecne" },
|
|
|
926f74 |
+ { .name = "ECN_CWR", .chunk_type = 13, .valid_flags = "--------", .nftname = "cwr" },
|
|
|
926f74 |
+ { .name = "SHUTDOWN_COMPLETE", .chunk_type = 14, .valid_flags = "-------T", .nftname = "shutdown-complete" },
|
|
|
926f74 |
+ { .name = "ASCONF", .chunk_type = 193, .valid_flags = "--------", .nftname = "asconf" },
|
|
|
926f74 |
+ { .name = "ASCONF_ACK", .chunk_type = 128, .valid_flags = "--------", .nftname = "asconf-ack" },
|
|
|
926f74 |
+ { .name = "FORWARD_TSN", .chunk_type = 192, .valid_flags = "--------", .nftname = "forward-tsn" },
|
|
|
926f74 |
};
|
|
|
926f74 |
|
|
|
926f74 |
static void
|
|
|
926f74 |
@@ -485,12 +486,52 @@ static void sctp_save(const void *ip, const struct xt_entry_match *match)
|
|
|
926f74 |
}
|
|
|
926f74 |
}
|
|
|
926f74 |
|
|
|
926f74 |
+static const char *sctp_xlate_chunk(struct xt_xlate *xl, const char *space,
|
|
|
926f74 |
+ const struct xt_sctp_info *einfo,
|
|
|
926f74 |
+ const struct sctp_chunk_names *scn)
|
|
|
926f74 |
+{
|
|
|
926f74 |
+ bool inv = einfo->invflags & XT_SCTP_CHUNK_TYPES;
|
|
|
926f74 |
+ const struct xt_sctp_flag_info *flag_info = NULL;
|
|
|
926f74 |
+ int i;
|
|
|
926f74 |
+
|
|
|
926f74 |
+ if (!scn->nftname)
|
|
|
926f74 |
+ return space;
|
|
|
926f74 |
+
|
|
|
926f74 |
+ if (!SCTP_CHUNKMAP_IS_SET(einfo->chunkmap, scn->chunk_type)) {
|
|
|
926f74 |
+ if (einfo->chunk_match_type != SCTP_CHUNK_MATCH_ONLY)
|
|
|
926f74 |
+ return space;
|
|
|
926f74 |
+
|
|
|
926f74 |
+ xt_xlate_add(xl, "%ssctp chunk %s %s", space,
|
|
|
926f74 |
+ scn->nftname, inv ? "exists" : "missing");
|
|
|
926f74 |
+ return " ";
|
|
|
926f74 |
+ }
|
|
|
926f74 |
+
|
|
|
926f74 |
+ for (i = 0; i < einfo->flag_count; i++) {
|
|
|
926f74 |
+ if (einfo->flag_info[i].chunktype == scn->chunk_type) {
|
|
|
926f74 |
+ flag_info = &einfo->flag_info[i];
|
|
|
926f74 |
+ break;
|
|
|
926f74 |
+ }
|
|
|
926f74 |
+ }
|
|
|
926f74 |
+
|
|
|
926f74 |
+ if (!flag_info) {
|
|
|
926f74 |
+ xt_xlate_add(xl, "%ssctp chunk %s %s", space,
|
|
|
926f74 |
+ scn->nftname, inv ? "missing" : "exists");
|
|
|
926f74 |
+ return " ";
|
|
|
926f74 |
+ }
|
|
|
926f74 |
+
|
|
|
926f74 |
+ xt_xlate_add(xl, "%ssctp chunk %s flags & 0x%x %s 0x%x", space,
|
|
|
926f74 |
+ scn->nftname, flag_info->flag_mask,
|
|
|
926f74 |
+ inv ? "!=" : "==", flag_info->flag);
|
|
|
926f74 |
+
|
|
|
926f74 |
+ return " ";
|
|
|
926f74 |
+}
|
|
|
926f74 |
+
|
|
|
926f74 |
static int sctp_xlate(struct xt_xlate *xl,
|
|
|
926f74 |
const struct xt_xlate_mt_params *params)
|
|
|
926f74 |
{
|
|
|
926f74 |
const struct xt_sctp_info *einfo =
|
|
|
926f74 |
(const struct xt_sctp_info *)params->match->data;
|
|
|
926f74 |
- char *space = "";
|
|
|
926f74 |
+ const char *space = "";
|
|
|
926f74 |
|
|
|
926f74 |
if (!einfo->flags)
|
|
|
926f74 |
return 0;
|
|
|
926f74 |
@@ -516,6 +557,18 @@ static int sctp_xlate(struct xt_xlate *xl,
|
|
|
926f74 |
xt_xlate_add(xl, "%ssctp dport%s %u", space,
|
|
|
926f74 |
einfo->invflags & XT_SCTP_DEST_PORTS ? " !=" : "",
|
|
|
926f74 |
einfo->dpts[0]);
|
|
|
926f74 |
+ space = " ";
|
|
|
926f74 |
+ }
|
|
|
926f74 |
+
|
|
|
926f74 |
+ if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
|
|
|
926f74 |
+ int i;
|
|
|
926f74 |
+
|
|
|
926f74 |
+ if (einfo->chunk_match_type == SCTP_CHUNK_MATCH_ANY)
|
|
|
926f74 |
+ return 0;
|
|
|
926f74 |
+
|
|
|
926f74 |
+ for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); i++)
|
|
|
926f74 |
+ space = sctp_xlate_chunk(xl, space, einfo,
|
|
|
926f74 |
+ &sctp_chunk_names[i]);
|
|
|
926f74 |
}
|
|
|
926f74 |
|
|
|
926f74 |
return 1;
|
|
|
926f74 |
diff --git a/extensions/libxt_sctp.txlate b/extensions/libxt_sctp.txlate
|
|
|
926f74 |
index 0d6c59e183675..bb817525db8d8 100644
|
|
|
926f74 |
--- a/extensions/libxt_sctp.txlate
|
|
|
926f74 |
+++ b/extensions/libxt_sctp.txlate
|
|
|
926f74 |
@@ -36,3 +36,9 @@ nft add rule ip filter INPUT sctp sport 50 sctp dport != 80-100 counter accept
|
|
|
926f74 |
|
|
|
926f74 |
iptables-translate -A INPUT -p sctp --dport 80 ! --sport 50:55 -j ACCEPT
|
|
|
926f74 |
nft add rule ip filter INPUT sctp sport != 50-55 sctp dport 80 counter accept
|
|
|
926f74 |
+
|
|
|
926f74 |
+iptables-translate -A INPUT -p sctp --chunk-types all INIT,DATA:iUbE,SACK,ABORT:T -j ACCEPT
|
|
|
926f74 |
+nft add rule ip filter INPUT sctp chunk data flags & 0xf == 0x5 sctp chunk init exists sctp chunk sack exists sctp chunk abort flags & 0x1 == 0x1 counter accept
|
|
|
926f74 |
+
|
|
|
926f74 |
+iptables-translate -A INPUT -p sctp --chunk-types only SHUTDOWN_COMPLETE -j ACCEPT
|
|
|
926f74 |
+nft add rule ip filter INPUT sctp chunk data missing sctp chunk init missing sctp chunk init-ack missing sctp chunk sack missing sctp chunk heartbeat missing sctp chunk heartbeat-ack missing sctp chunk abort missing sctp chunk shutdown missing sctp chunk shutdown-ack missing sctp chunk error missing sctp chunk cookie-echo missing sctp chunk cookie-ack missing sctp chunk ecne missing sctp chunk cwr missing sctp chunk shutdown-complete exists sctp chunk asconf missing sctp chunk asconf-ack missing sctp chunk forward-tsn missing counter accept
|
|
|
926f74 |
--
|
|
|
926f74 |
2.31.1
|
|
|
926f74 |
|