|
|
b144b7 |
From 87a2128fcfd4c5b0847a8c611652ade8c54d8185 Mon Sep 17 00:00:00 2001
|
|
|
6ef880 |
From: Phil Sutter <phil@nwl.cc>
|
|
|
6ef880 |
Date: Fri, 2 Oct 2020 09:44:38 +0200
|
|
|
6ef880 |
Subject: [PATCH] nft: Optimize class-based IP prefix matches
|
|
|
6ef880 |
|
|
|
6ef880 |
Payload expression works on byte-boundaries, leverage this with suitable
|
|
|
6ef880 |
prefix lengths.
|
|
|
6ef880 |
|
|
|
6ef880 |
Signed-off-by: Phil Sutter <phil@nwl.cc>
|
|
|
6ef880 |
(cherry picked from commit 323259001d617ae359430a03ee3d3e7f107684e0)
|
|
|
6ef880 |
Signed-off-by: Phil Sutter <psutter@redhat.com>
|
|
|
6ef880 |
---
|
|
|
6ef880 |
iptables/nft-arp.c | 11 ++++++++---
|
|
|
6ef880 |
iptables/nft-ipv4.c | 6 ++++--
|
|
|
6ef880 |
iptables/nft-ipv6.c | 6 ++++--
|
|
|
6ef880 |
iptables/nft-shared.c | 14 ++++++++++----
|
|
|
6ef880 |
iptables/nft-shared.h | 4 ++++
|
|
|
6ef880 |
5 files changed, 30 insertions(+), 11 deletions(-)
|
|
|
6ef880 |
|
|
|
6ef880 |
diff --git a/iptables/nft-arp.c b/iptables/nft-arp.c
|
|
|
6ef880 |
index d4a86610ec217..ac400e484a4fa 100644
|
|
|
6ef880 |
--- a/iptables/nft-arp.c
|
|
|
6ef880 |
+++ b/iptables/nft-arp.c
|
|
|
6ef880 |
@@ -303,7 +303,8 @@ static bool nft_arp_parse_devaddr(struct nft_xt_ctx *ctx,
|
|
|
6ef880 |
memcpy(info->mask, ctx->bitwise.mask, ETH_ALEN);
|
|
|
6ef880 |
ctx->flags &= ~NFT_XT_CTX_BITWISE;
|
|
|
6ef880 |
} else {
|
|
|
6ef880 |
- memset(info->mask, 0xff, ETH_ALEN);
|
|
|
6ef880 |
+ memset(info->mask, 0xff,
|
|
|
6ef880 |
+ min(ctx->payload.len, ETH_ALEN));
|
|
|
6ef880 |
}
|
|
|
6ef880 |
|
|
|
6ef880 |
return inv;
|
|
|
6ef880 |
@@ -360,7 +361,9 @@ static void nft_arp_parse_payload(struct nft_xt_ctx *ctx,
|
|
|
6ef880 |
parse_mask_ipv4(ctx, &fw->arp.smsk);
|
|
|
6ef880 |
ctx->flags &= ~NFT_XT_CTX_BITWISE;
|
|
|
6ef880 |
} else {
|
|
|
6ef880 |
- fw->arp.smsk.s_addr = 0xffffffff;
|
|
|
6ef880 |
+ memset(&fw->arp.smsk, 0xff,
|
|
|
6ef880 |
+ min(ctx->payload.len,
|
|
|
6ef880 |
+ sizeof(struct in_addr)));
|
|
|
6ef880 |
}
|
|
|
6ef880 |
|
|
|
6ef880 |
if (inv)
|
|
|
6ef880 |
@@ -380,7 +383,9 @@ static void nft_arp_parse_payload(struct nft_xt_ctx *ctx,
|
|
|
6ef880 |
parse_mask_ipv4(ctx, &fw->arp.tmsk);
|
|
|
6ef880 |
ctx->flags &= ~NFT_XT_CTX_BITWISE;
|
|
|
6ef880 |
} else {
|
|
|
6ef880 |
- fw->arp.tmsk.s_addr = 0xffffffff;
|
|
|
6ef880 |
+ memset(&fw->arp.tmsk, 0xff,
|
|
|
6ef880 |
+ min(ctx->payload.len,
|
|
|
6ef880 |
+ sizeof(struct in_addr)));
|
|
|
6ef880 |
}
|
|
|
6ef880 |
|
|
|
6ef880 |
if (inv)
|
|
|
6ef880 |
diff --git a/iptables/nft-ipv4.c b/iptables/nft-ipv4.c
|
|
|
6ef880 |
index 70634f8fad84d..c84af2df90da7 100644
|
|
|
6ef880 |
--- a/iptables/nft-ipv4.c
|
|
|
6ef880 |
+++ b/iptables/nft-ipv4.c
|
|
|
6ef880 |
@@ -199,7 +199,8 @@ static void nft_ipv4_parse_payload(struct nft_xt_ctx *ctx,
|
|
|
6ef880 |
parse_mask_ipv4(ctx, &cs->fw.ip.smsk);
|
|
|
6ef880 |
ctx->flags &= ~NFT_XT_CTX_BITWISE;
|
|
|
6ef880 |
} else {
|
|
|
6ef880 |
- cs->fw.ip.smsk.s_addr = 0xffffffff;
|
|
|
6ef880 |
+ memset(&cs->fw.ip.smsk, 0xff,
|
|
|
6ef880 |
+ min(ctx->payload.len, sizeof(struct in_addr)));
|
|
|
6ef880 |
}
|
|
|
6ef880 |
|
|
|
6ef880 |
if (inv)
|
|
|
6ef880 |
@@ -212,7 +213,8 @@ static void nft_ipv4_parse_payload(struct nft_xt_ctx *ctx,
|
|
|
6ef880 |
parse_mask_ipv4(ctx, &cs->fw.ip.dmsk);
|
|
|
6ef880 |
ctx->flags &= ~NFT_XT_CTX_BITWISE;
|
|
|
6ef880 |
} else {
|
|
|
6ef880 |
- cs->fw.ip.dmsk.s_addr = 0xffffffff;
|
|
|
6ef880 |
+ memset(&cs->fw.ip.dmsk, 0xff,
|
|
|
6ef880 |
+ min(ctx->payload.len, sizeof(struct in_addr)));
|
|
|
6ef880 |
}
|
|
|
6ef880 |
|
|
|
6ef880 |
if (inv)
|
|
|
6ef880 |
diff --git a/iptables/nft-ipv6.c b/iptables/nft-ipv6.c
|
|
|
6ef880 |
index d01491bfdb689..cfced245a781c 100644
|
|
|
6ef880 |
--- a/iptables/nft-ipv6.c
|
|
|
6ef880 |
+++ b/iptables/nft-ipv6.c
|
|
|
6ef880 |
@@ -146,7 +146,8 @@ static void nft_ipv6_parse_payload(struct nft_xt_ctx *ctx,
|
|
|
6ef880 |
parse_mask_ipv6(ctx, &cs->fw6.ipv6.smsk);
|
|
|
6ef880 |
ctx->flags &= ~NFT_XT_CTX_BITWISE;
|
|
|
6ef880 |
} else {
|
|
|
6ef880 |
- memset(&cs->fw6.ipv6.smsk, 0xff, sizeof(struct in6_addr));
|
|
|
6ef880 |
+ memset(&cs->fw6.ipv6.smsk, 0xff,
|
|
|
6ef880 |
+ min(ctx->payload.len, sizeof(struct in6_addr)));
|
|
|
6ef880 |
}
|
|
|
6ef880 |
|
|
|
6ef880 |
if (inv)
|
|
|
6ef880 |
@@ -159,7 +160,8 @@ static void nft_ipv6_parse_payload(struct nft_xt_ctx *ctx,
|
|
|
6ef880 |
parse_mask_ipv6(ctx, &cs->fw6.ipv6.dmsk);
|
|
|
6ef880 |
ctx->flags &= ~NFT_XT_CTX_BITWISE;
|
|
|
6ef880 |
} else {
|
|
|
6ef880 |
- memset(&cs->fw6.ipv6.dmsk, 0xff, sizeof(struct in6_addr));
|
|
|
6ef880 |
+ memset(&cs->fw6.ipv6.dmsk, 0xff,
|
|
|
6ef880 |
+ min(ctx->payload.len, sizeof(struct in6_addr)));
|
|
|
6ef880 |
}
|
|
|
6ef880 |
|
|
|
6ef880 |
if (inv)
|
|
|
6ef880 |
diff --git a/iptables/nft-shared.c b/iptables/nft-shared.c
|
|
|
6ef880 |
index f60f5df97fb86..b1237049d0a34 100644
|
|
|
6ef880 |
--- a/iptables/nft-shared.c
|
|
|
6ef880 |
+++ b/iptables/nft-shared.c
|
|
|
6ef880 |
@@ -166,16 +166,22 @@ void add_addr(struct nftnl_rule *r, int offset,
|
|
|
6ef880 |
void *data, void *mask, size_t len, uint32_t op)
|
|
|
6ef880 |
{
|
|
|
6ef880 |
const unsigned char *m = mask;
|
|
|
6ef880 |
+ bool bitwise = false;
|
|
|
6ef880 |
int i;
|
|
|
6ef880 |
|
|
|
6ef880 |
- add_payload(r, offset, len, NFT_PAYLOAD_NETWORK_HEADER);
|
|
|
6ef880 |
-
|
|
|
6ef880 |
for (i = 0; i < len; i++) {
|
|
|
6ef880 |
- if (m[i] != 0xff)
|
|
|
6ef880 |
+ if (m[i] != 0xff) {
|
|
|
6ef880 |
+ bitwise = m[i] != 0;
|
|
|
6ef880 |
break;
|
|
|
6ef880 |
+ }
|
|
|
6ef880 |
}
|
|
|
6ef880 |
|
|
|
6ef880 |
- if (i != len)
|
|
|
6ef880 |
+ if (!bitwise)
|
|
|
6ef880 |
+ len = i;
|
|
|
6ef880 |
+
|
|
|
6ef880 |
+ add_payload(r, offset, len, NFT_PAYLOAD_NETWORK_HEADER);
|
|
|
6ef880 |
+
|
|
|
6ef880 |
+ if (bitwise)
|
|
|
6ef880 |
add_bitwise(r, mask, len);
|
|
|
6ef880 |
|
|
|
6ef880 |
add_cmp_ptr(r, op, data, len);
|
|
|
6ef880 |
diff --git a/iptables/nft-shared.h b/iptables/nft-shared.h
|
|
|
6ef880 |
index bee99a7dd0c93..c7f1e366b75ee 100644
|
|
|
6ef880 |
--- a/iptables/nft-shared.h
|
|
|
6ef880 |
+++ b/iptables/nft-shared.h
|
|
|
6ef880 |
@@ -252,4 +252,8 @@ void xtables_restore_parse(struct nft_handle *h,
|
|
|
6ef880 |
const struct nft_xt_restore_parse *p);
|
|
|
6ef880 |
|
|
|
6ef880 |
void nft_check_xt_legacy(int family, bool is_ipt_save);
|
|
|
6ef880 |
+
|
|
|
6ef880 |
+#define min(x, y) ((x) < (y) ? (x) : (y))
|
|
|
6ef880 |
+#define max(x, y) ((x) > (y) ? (x) : (y))
|
|
|
6ef880 |
+
|
|
|
6ef880 |
#endif
|
|
|
6ef880 |
--
|
|
|
6ef880 |
2.28.0
|
|
|
6ef880 |
|