Skip to contentSkip to conversation

proto: revisit short-circuit loops over upper protocols

PATCH In review Pablo Neira Ayuso 2 participants 3 messages Dec 15, 2021 – Dec 16, 2021 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 proto: revisit short-circuit loops over upper protocols

Move the check for NULL protocol description away from the loop to avoid too long line.

---
 src/proto.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
src/proto.c +9 -7
index 31a2f38065ad..a013a00d2c7b 100644
@@ -59,8 +59,9 @@ proto_find_upper(const struct proto_desc *base, unsigned int num)
 {
 	unsigned int i;
 
-	for (i = 0; i < array_size(base->protocols) && base->protocols[i].desc;
-	     i++) {
+	for (i = 0; i < array_size(base->protocols); i++) {
+		if (!base->protocols[i].desc)
+			break;
 		if (base->protocols[i].num == num)
 			return base->protocols[i].desc;
 	}
@@ -78,8 +79,9 @@ int proto_find_num(const struct proto_desc *base,
 {
 	unsigned int i;
 
-	for (i = 0; i < array_size(base->protocols) && base->protocols[i].desc;
-	     i++) {
+	for (i = 0; i < array_size(base->protocols); i++) {
+		if (!base->protocols[i].desc)
+			break;
 		if (base->protocols[i].desc == desc)
 			return base->protocols[i].num;
 	}
@@ -107,9 +109,9 @@ int proto_dev_type(const struct proto_desc *desc, uint16_t *res)
 			*res = dev_proto_desc[i].type;
 			return 0;
 		}
-		for (j = 0; j < array_size(base->protocols) &&
-			     base->protocols[j].desc;
-		     j++) {
+		for (j = 0; j < array_size(base->protocols); j++) {
+			if (!base->protocols[j].desc)
+				break;
 			if (base->protocols[j].desc == desc) {
 				*res = dev_proto_desc[i].type;
 				return 0;
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 1/1 tests: shell: extend catchall tests for maps

Add a few tests for the catchall features and maps.

---
 tests/shell/testcases/sets/0064map_catchall_0           | 5 +++++
 tests/shell/testcases/sets/dumps/0064map_catchall_0.nft | 6 ++++++
 2 files changed, 11 insertions(+)
tests/shell/testcases/sets/0064map_catchall_0 +5 -0
index 6f2a7c6f0249..436851604e34 100755
@@ -17,3 +17,8 @@ RULESET="table ip x {
 $NFT -f - <<< $RULESET
 $NFT delete element x y { \* : 192.168.0.3 }
 $NFT add element x y { \* : 192.168.0.4 }
+
+$NFT add chain x y
+$NFT add rule x y snat to ip saddr map @z
+$NFT 'add rule x y snat to ip saddr map { 10.141.0.0/24 : 192.168.0.2, * : 192.168.0.3 }'
+$NFT 'add rule x y snat to ip saddr . ip daddr map { 10.141.0.0/24 . 10.0.0.0/8 : 192.168.0.2, 192.168.9.0/24 . 192.168.10.0/24 : 192.168.0.4, * : 192.168.0.3 }'
tests/shell/testcases/sets/dumps/0064map_catchall_0.nft +6 -0
index 286683a05df9..890ed2aab080 100644
@@ -9,4 +9,10 @@ table ip x {
 		flags interval
 		elements = { 10.141.0.0/24 : 192.168.0.2, * : 192.168.0.3 }
 	}
+
+	chain y {
+		snat to ip saddr map @z
+		snat to ip saddr map { 10.141.0.0/24 : 192.168.0.2, * : 192.168.0.3 }
+		snat to ip saddr . ip daddr map { 10.141.0.0/24 . 10.0.0.0/8 : 192.168.0.2, 192.168.9.0/24 . 192.168.10.0/24 : 192.168.0.4, * : 192.168.0.3 }
+	}
 }
Signature · 1 line
2.30.2
#2 lore raw
In reply to Pablo Neira Ayuso
Quoted 2 lines · Pablo Neira Ayuso

Move the check for NULL protocol description away from the loop to avoid too long line.

LGTM.

J.

Quoted 51 lines
---
 src/proto.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
src/proto.c +9 -8
index 31a2f38065ad..a013a00d2c7b 100644
@@ -59,8 +59,9 @@ proto_find_upper(const struct proto_desc *base, unsigned int num)
 {
 	unsigned int i;
 
-	for (i = 0; i < array_size(base->protocols) && base->protocols[i].desc;
-	     i++) {
+	for (i = 0; i < array_size(base->protocols); i++) {
+		if (!base->protocols[i].desc)
+			break;
 		if (base->protocols[i].num == num)
 			return base->protocols[i].desc;
 	}
@@ -78,8 +79,9 @@ int proto_find_num(const struct proto_desc *base,
 {
 	unsigned int i;
 
-	for (i = 0; i < array_size(base->protocols) && base->protocols[i].desc;
-	     i++) {
+	for (i = 0; i < array_size(base->protocols); i++) {
+		if (!base->protocols[i].desc)
+			break;
 		if (base->protocols[i].desc == desc)
 			return base->protocols[i].num;
 	}
@@ -107,9 +109,9 @@ int proto_dev_type(const struct proto_desc *desc, uint16_t *res)
 			*res = dev_proto_desc[i].type;
 			return 0;
 		}
-		for (j = 0; j < array_size(base->protocols) &&
-			     base->protocols[j].desc;
-		     j++) {
+		for (j = 0; j < array_size(base->protocols); j++) {
+			if (!base->protocols[j].desc)
+				break;
 			if (base->protocols[j].desc == desc) {
 				*res = dev_proto_desc[i].type;
 				return 0;
--

2.30.2