Skip to contentSkip to conversation

conntrack: Fix CIDR to mask conversion on Big Endian

PATCH In review Phil Sutter 2 participants 2 messages Sep 4, 2019 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH v2 1/1 conntrack: Fix CIDR to mask conversion on Big Endian

Code assumed host architecture to be Little Endian. Instead produce a proper mask by pushing the set bits into most significant position and apply htonl() on the result.

Fixes 3f6a2e90936bb ("conntrack: add support for CIDR notation")

---
Changes since v1:
- Simplify bitshift operation as per Florian's suggestion.

---
 src/conntrack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/conntrack.c +1 -1
index c980a13f33d2c..f65926b298ad3 100644
@@ -2210,7 +2210,7 @@ nfct_build_netmask(uint32_t *dst, int b, int n)
 			dst[i] = 0xffffffff;
 			b -= 32;
 		} else if (b > 0) {
-			dst[i] = (1 << b) - 1;
+			dst[i] = htonl(~0u << (32 - b));
 			b = 0;
 		} else {
 			dst[i] = 0;
Signature · 1 line
2.22.0