Skip to contentSkip to conversation

nftables: add typeof support for objref maps

PATCH New Florian Westphal 1 participant 4 messages Feb 29, 2024 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover nftables: add typeof support for objref maps

First patch extends the bison parser to permit typeof with objref maps. Second patch "fixes" nftables listing mode to keep the typeof information for objref maps rather than doing a fallback to 'type'.

Third patch adds a test case for this.

These patches are on top of the two bison parser compaction changes.

author maintainer
#1 lore raw

PATCH 1/3 parser: allow typeof in objref maps

Its currently not possible to declare a map that stores object references with the "typeof" keyword, e.g.

map m {
type ipv4_addr : limit

will work, but

map m {
typeof ip saddr : limit

will give a syntax error ("unexpected limit"). Followup pach will add support for listing side too.

---
 src/parser_bison.y | 9 +++++++++
 1 file changed, 9 insertions(+)
src/parser_bison.y +9 -0
index cba37c686f66..cd1dc658882d 100644
@@ -2340,6 +2340,15 @@ map_block		:	/* empty */	{ $$ = $<set>-1; }
 				$1->flags  |= NFT_SET_OBJECT;
 				$$ = $1;
 			}
+			|	map_block	TYPEOF
+						typeof_expr 	COLON	map_block_obj_type
+						stmt_separator
+			{
+				$1->key = $3;
+				$1->objtype = $5;
+				$1->flags  |= NFT_SET_OBJECT;
+				$$ = $1;
+			}
 			|	map_block	FLAGS		set_flag_list	stmt_separator
 			{
 				$1->flags |= $3;
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/3 netlink: allow typeof keywords with objref maps during listing

In reply to Florian Westphal

Without this,

typeof meta l4proto . ip saddr . tcp sport : limit

... is shown as

type inet_proto . ipv4_addr . inet_service : limit

The "data" element is a value (the object type number). It doesn't support userinfo data.

There is no reason to add it, the value is the object type number that the object-reference map stores.

So, if we have an objref map, DO NOT discard the key part, as we do for normal maps.

For normal maps, we support either typeof notation, i.e.:

typeof meta l4proto . ip saddr . tcp sport : ip saddr

or the data type version:
type inet_proto . ipv4_addr . inet_service : ipv4_addr

... but not a mix, a hyptothetical

typeof meta l4proto . ip saddr . tcp sport : ipv4_addr

... does not work.

If nft finds no udata attached to the data element, for normal map case, it has to fall back to the "type" form.

But for objref maps this is expected, udata for key but not for data. Hence, for objref case, keep the typeof part if its valid.

---
 src/netlink.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
src/netlink.c +7 -1
index 3d685b575e64..0088b742d573 100644
@@ -1044,6 +1044,8 @@ struct set *netlink_delinearize_set(struct netlink_ctx *ctx,
 	}
 	list_splice_tail(&set_parse_ctx.stmt_list, &set->stmt_list);
 
+	set->flags = nftnl_set_get_u32(nls, NFTNL_SET_FLAGS);
+
 	if (datatype) {
 		uint32_t dlen;
 
@@ -1056,6 +1058,11 @@ struct set *netlink_delinearize_set(struct netlink_ctx *ctx,
 			typeof_expr_data->len = klen;
 			set->data = typeof_expr_data;
 			typeof_expr_data = NULL;
+		} else if (set->flags & NFT_SET_OBJECT) {
+			set->data = constant_expr_alloc(&netlink_location,
+							dtype2,
+							databyteorder, klen,
+							NULL);
 		} else {
 			set->data = constant_expr_alloc(&netlink_location,
 							dtype2,
@@ -1084,7 +1091,6 @@ struct set *netlink_delinearize_set(struct netlink_ctx *ctx,
 					       NULL);
 	}
 
-	set->flags   = nftnl_set_get_u32(nls, NFTNL_SET_FLAGS);
 	set->handle.handle.id = nftnl_set_get_u64(nls, NFTNL_SET_HANDLE);
 
 	set->objtype = objtype;
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH 3/3 tests: maps: add a test case for "limit" objref map

In reply to Florian Westphal

check add, delete and removal operations for objref maps.

Also check type vs. typeof declarations and use both interval and interval+concatenation (rbtree, pipapo).

---
 .../maps/dumps/named_limits.json-nft          | 328 ++++++++++++++++++
 .../testcases/maps/dumps/named_limits.nft     |  55 +++
 tests/shell/testcases/maps/named_limits       |  59 ++++
 3 files changed, 442 insertions(+)
 create mode 100644 tests/shell/testcases/maps/dumps/named_limits.json-nft
 create mode 100644 tests/shell/testcases/maps/dumps/named_limits.nft
 create mode 100755 tests/shell/testcases/maps/named_limits
tests/shell/testcases/maps/dumps/named_limits.json-nft new file +328 -0
new file mode 100644
index 000000000000..28a92529c8d2
@@ -0,0 +1,328 @@
+{
+  "nftables": [
+    {
+      "metainfo": {
+        "version": "VERSION",
+        "release_name": "RELEASE_NAME",
+        "json_schema_version": 1
+      }
+    },
+    {
+      "table": {
+        "family": "inet",
+        "name": "filter",
+        "handle": 0
+      }
+    },
+    {
+      "limit": {
+        "family": "inet",
+        "name": "tarpit-pps",
+        "table": "filter",
+        "handle": 0,
+        "rate": 1,
+        "per": "second",
+        "burst": 5
+      }
+    },
+    {
+      "limit": {
+        "family": "inet",
+        "name": "tarpit-bps",
+        "table": "filter",
+        "handle": 0,
+        "rate": 1,
+        "per": "second",
+        "rate_unit": "kbytes"
+      }
+    },
+    {
+      "limit": {
+        "family": "inet",
+        "name": "http-bulk-rl-1m",
+        "table": "filter",
+        "handle": 0,
+        "rate": 1,
+        "per": "second",
+        "rate_unit": "mbytes"
+      }
+    },
+    {
+      "limit": {
+        "family": "inet",
+        "name": "http-bulk-rl-10m",
+        "table": "filter",
+        "handle": 0,
+        "rate": 10,
+        "per": "second",
+        "rate_unit": "mbytes"
+      }
+    },
+    {
+      "set": {
+        "family": "inet",
+        "name": "tarpit4",
+        "table": "filter",
+        "type": "ipv4_addr",
+        "handle": 0,
+        "size": 10000,
+        "flags": [
+          "timeout",
+          "dynamic"
+        ],
+        "timeout": 60
+      }
+    },
+    {
+      "set": {
+        "family": "inet",
+        "name": "tarpit6",
+        "table": "filter",
+        "type": "ipv6_addr",
+        "handle": 0,
+        "size": 10000,
+        "flags": [
+          "timeout",
+          "dynamic"
+        ],
+        "timeout": 60
+      }
+    },
+    {
+      "map": {
+        "family": "inet",
+        "name": "addr4limit",
+        "table": "filter",
+        "type": [
+          "inet_proto",
+          "ipv4_addr",
+          "inet_service"
+        ],
+        "handle": 0,
+        "map": "limit",
+        "flags": [
+          "interval"
+        ],
+        "elem": [
+          [
+            {
+              "concat": [
+                "tcp",
+                {
+                  "prefix": {
+                    "addr": "192.168.0.0",
+                    "len": 16
+                  }
+                },
+                {
+                  "range": [
+                    1,
+                    65535
+                  ]
+                }
+              ]
+            },
+            "tarpit-bps"
+          ],
+          [
+            {
+              "concat": [
+                "udp",
+                {
+                  "prefix": {
+                    "addr": "192.168.0.0",
+                    "len": 16
+                  }
+                },
+                {
+                  "range": [
+                    1,
+                    65535
+                  ]
+                }
+              ]
+            },
+            "tarpit-pps"
+          ],
+          [
+            {
+              "concat": [
+                "tcp",
+                {
+                  "range": [
+                    "127.0.0.1",
+                    "127.1.2.3"
+                  ]
+                },
+                {
+                  "range": [
+                    1,
+                    1024
+                  ]
+                }
+              ]
+            },
+            "tarpit-pps"
+          ],
+          [
+            {
+              "concat": [
+                "tcp",
+                {
+                  "range": [
+                    "10.0.0.1",
+                    "10.0.0.255"
+                  ]
+                },
+                80
+              ]
+            },
+            "http-bulk-rl-1m"
+          ],
+          [
+            {
+              "concat": [
+                "tcp",
+                {
+                  "range": [
+                    "10.0.0.1",
+                    "10.0.0.255"
+                  ]
+                },
+                443
+              ]
+            },
+            "http-bulk-rl-1m"
+          ],
+          [
+            {
+              "concat": [
+                "tcp",
+                {
+                  "prefix": {
+                    "addr": "10.0.1.0",
+                    "len": 24
+                  }
+                },
+                {
+                  "range": [
+                    1024,
+                    65535
+                  ]
+                }
+              ]
+            },
+            "http-bulk-rl-10m"
+          ],
+          [
+            {
+              "concat": [
+                "tcp",
+                "10.0.2.1",
+                22
+              ]
+            },
+            "http-bulk-rl-10m"
+          ]
+        ]
+      }
+    },
+    {
+      "map": {
+        "family": "inet",
+        "name": "saddr6limit",
+        "table": "filter",
+        "type": "ipv6_addr",
+        "handle": 0,
+        "map": "limit",
+        "flags": [
+          "interval"
+        ],
+        "elem": [
+          [
+            {
+              "range": [
+                "dead::beef",
+                "dead::1:aced"
+              ]
+            },
+            "tarpit-pps"
+          ]
+        ]
+      }
+    },
+    {
+      "chain": {
+        "family": "inet",
+        "table": "filter",
+        "name": "input",
+        "handle": 0,
+        "type": "filter",
+        "hook": "input",
+        "prio": 0,
+        "policy": "accept"
+      }
+    },
+    {
+      "rule": {
+        "family": "inet",
+        "table": "filter",
+        "chain": "input",
+        "handle": 0,
+        "expr": [
+          {
+            "limit": {
+              "map": {
+                "key": {
+                  "concat": [
+                    {
+                      "meta": {
+                        "key": "l4proto"
+                      }
+                    },
+                    {
+                      "payload": {
+                        "protocol": "ip",
+                        "field": "saddr"
+                      }
+                    },
+                    {
+                      "payload": {
+                        "protocol": "th",
+                        "field": "sport"
+                      }
+                    }
+                  ]
+                },
+                "data": "@addr4limit"
+              }
+            }
+          }
+        ]
+      }
+    },
+    {
+      "rule": {
+        "family": "inet",
+        "table": "filter",
+        "chain": "input",
+        "handle": 0,
+        "expr": [
+          {
+            "limit": {
+              "map": {
+                "key": {
+                  "payload": {
+                    "protocol": "ip6",
+                    "field": "saddr"
+                  }
+                },
+                "data": "@saddr6limit"
+              }
+            }
+          }
+        ]
+      }
+    }
+  ]
+}
tests/shell/testcases/maps/dumps/named_limits.nft new file +55 -0

55 lines not on this page · raw message

tests/shell/testcases/maps/named_limits new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
2.43.0