Skip to contentSkip to conversation

netfilter: nft_meta: Extend support for NFT_META_TSTAMP_NS

PATCH Superseded Karuna Grewal 2 participants 6 messages Mar 1, 2019 – Mar 20, 2019

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: nft_meta: Extend support for NFT_META_TSTAMP_NS

Meta evaluation function is extended to suport NFT_META_TSTAMP_NS option by exposing the 64 bit timestamp of the packet to two 32 bit registers.

---
 include/uapi/linux/netfilter/nf_tables.h | 2 ++
 net/netfilter/nft_meta.c                 | 9 +++++++++
 2 files changed, 11 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +2 -0
index a66c8de006cc..61f8f604c614 100644
@@ -793,6 +793,7 @@ enum nft_exthdr_attributes {
  * @NFT_META_SECPATH: boolean, secpath_exists (!!skb->sp)
  * @NFT_META_IIFKIND: packet input interface kind name (dev->rtnl_link_ops->kind)
  * @NFT_META_OIFKIND: packet output interface kind name (dev->rtnl_link_ops->kind)
+ * @NFT_META_TSTAMP_NS: packet arrival time (skb->tstamp)
  */
 enum nft_meta_keys {
 	NFT_META_LEN,
@@ -823,6 +824,7 @@ enum nft_meta_keys {
 	NFT_META_SECPATH,
 	NFT_META_IIFKIND,
 	NFT_META_OIFKIND,
+	NFT_META_TSTAMP_NS
 };
 
 /**
net/netfilter/nft_meta.c +9 -0
index 987d2d6ce624..adfa1f221946 100644
@@ -254,6 +254,13 @@ void nft_meta_get_eval(const struct nft_expr *expr,
 			goto err;
 		strncpy((char *)dest, out->rtnl_link_ops->kind, IFNAMSIZ);
 		break;
+	case NFT_META_TSTAMP_NS:
+		if (skb->tstamp == 0)
+			__net_timestamp((struct sk_buff *)skb);
+		u64 timestamp = ktime_to_ns(skb->tstamp);
+		*dest = (u32)(timestamp >> 32);
+		*(dest + 1) = (u32) timestamp;
+		break;
 	default:
 		WARN_ON(1);
 		goto err;
@@ -371,6 +378,8 @@ static int nft_meta_get_init(const struct nft_ctx *ctx,
 		len = IFNAMSIZ;
 		break;
 #endif
+	case NFT_META_TSTAMP_NS:
+		len = sizeof(u64);
 	default:
 		return -EOPNOTSUPP;
 	}

--
2.17.1

maintainer
#1 lore raw
Quoted 2 lines · Karuna Grewal

Meta evaluation function is extended to suport NFT_META_TSTAMP_NS option by exposing the 64 bit timestamp of the packet to two 32 bit registers.

Please test patches first, see below.

Quoted 5 lines
net/netfilter/nft_meta.c +0 -0
index 987d2d6ce624..adfa1f221946 100644

net/netfilter/nft_meta.c void nft_meta_get_eval(const struct nft_expr *expr, · line 263

Quoted 10 lines
net/netfilter/nft_meta.c +7 -0
@@ -254,6 +254,13 @@ void nft_meta_get_eval(const struct nft_expr *expr,
 			goto err;
 		strncpy((char *)dest, out->rtnl_link_ops->kind, IFNAMSIZ);
 		break;
+	case NFT_META_TSTAMP_NS:
+		if (skb->tstamp == 0)
+			__net_timestamp((struct sk_buff *)skb);
+		u64 timestamp = ktime_to_ns(skb->tstamp);
+		*dest = (u32)(timestamp >> 32);
+		*(dest + 1) = (u32) timestamp;
+		break;

Please consider using u64 *dest64 or similar to avoid the need for shifting.

net/netfilter/nft_meta.c static int nft_meta_get_init(const struct nft_ctx *ctx, · line 384

Quoted 11 lines
net/netfilter/nft_meta.c +2 -0
 	default:
 		WARN_ON(1);
 		goto err;
@@ -371,6 +378,8 @@ static int nft_meta_get_init(const struct nft_ctx *ctx,
 		len = IFNAMSIZ;
 		break;
 #endif
+	case NFT_META_TSTAMP_NS:
+		len = sizeof(u64);
 	default:
 		return -EOPNOTSUPP;

This is missing a break statement after len assignment.

Other than this, this patch looks good. Please consider sending v3 once you have nftables patches ready as well.

#2 lore raw
Quoted 7 lines · Florian Westphal
Quoted 2 lines · Karuna Grewal

Meta evaluation function is extended to suport NFT_META_TSTAMP_NS option by exposing the 64 bit timestamp of the packet to two 32 bit registers.

Other than this, this patch looks good. Please consider sending v3 once you have nftables patches ready as well.

I've a doubt in the nftables implementation for implementing the -m time support. I'm unable to get a proper idea of where the start and stop time comparison with the dest register's value takes place. From my understanding of implementation, I've noticed that after parsing the rule and the meta expression is allocated, expression's primary evaluation function is invoked.
Meanwhile, the kernel has the nft_meta_get_eval function setting the register with the relevant field and in nf_tables_core.h the registering of different nft_expr_types is accomplished. Could someone please give me some pointers about where this processing of the data set in the meta registers takes place?
Thanks
Karuna

maintainer
#3 lore raw
Quoted 2 lines · Karuna Grewal

I've a doubt in the nftables implementation for implementing the -m time support.

Full -m time is complicated, do not worry about this yet.

Quoted 2 lines

I'm unable to get a proper idea of where the start and stop time comparison with the dest register's value takes place.

Via the nftables evaluation loop. You only need to worry about placing the value (timestamp) in the dst register (on kernel side), so your patch looks pretty complete aside from the missing 'break'.

Its userspace (nftables) responsibility to tell kernel to do something with the register, such as a compare or range.

Have a look at
http://git.netfilter.org/nftables/commit/src/meta.c?id=512795a673f999fb04b84dbbbe41174e9c581430

It should be enough to follow this approach, adding e.g. META_TEMPLATE("timestamp", ..

we have TYPE_TIME already, even though its a relative one, it would/should work for a quick prototype.

Quoted 8 lines

From my understanding of implementation, I've noticed that after parsing the rule and the meta expression is allocated, expression's primary evaluation function is invoked.
Meanwhile, the kernel has the nft_meta_get_eval function setting the register with the relevant field and in nf_tables_core.h the registering of different nft_expr_types is accomplished. Could someone please give me some pointers about where this processing of the data set in the meta registers takes place?

For instance in nft_cmp.c, but thats just one possibility, there are other consumers, such as nft_range.c, nft_lookup.c, and so on.

#4 lore raw
Quoted 17 lines · Florian Westphal
Quoted 2 lines · Karuna Grewal

I've a doubt in the nftables implementation for implementing the -m time support.

Full -m time is complicated, do not worry about this yet.

Quoted 2 lines

I'm unable to get a proper idea of where the start and stop time comparison with the dest register's value takes place.

Via the nftables evaluation loop. You only need to worry about placing the value (timestamp) in the dst register (on kernel side), so your patch looks pretty complete aside from the missing 'break'.

Its userspace (nftables) responsibility to tell kernel to do something with the register, such as a compare or range.

Quoted 8 lines

Have a look at
http://git.netfilter.org/nftables/commit/src/meta.c?id=512795a673f999fb04b84dbbbe41174e9c581430

It should be enough to follow this approach, adding e.g. META_TEMPLATE("timestamp", ..

we have TYPE_TIME already, even though its a relative one, it would/should work for a quick prototype.

I had sent the patch for this basic case of simply comparing one timestamp. As the startdate and stopdate options need to implemented completely, should I make use of an interval
or is using two different tokens viz. START_TSTAMP and STOP_TSTAMP the preferred option.

#5 lore raw
Quoted 25 lines · Karuna Grewal
Quoted 23 lines · Florian Westphal
Quoted 2 lines · Karuna Grewal

I've a doubt in the nftables implementation for implementing the -m time support.

Full -m time is complicated, do not worry about this yet.

Quoted 2 lines

I'm unable to get a proper idea of where the start and stop time comparison with the dest register's value takes place.

Via the nftables evaluation loop. You only need to worry about placing the value (timestamp) in the dst register (on kernel side), so your patch looks pretty complete aside from the missing 'break'.

Its userspace (nftables) responsibility to tell kernel to do something with the register, such as a compare or range.

Have a look at
http://git.netfilter.org/nftables/commit/src/meta.c?id=512795a673f999fb04b84dbbbe41174e9c581430

It should be enough to follow this approach, adding e.g. META_TEMPLATE("timestamp", ..

Quoted 8 lines
Quoted 2 lines

we have TYPE_TIME already, even though its a relative one, it would/should work for a quick prototype.

I had sent the patch for this basic case of simply comparing one timestamp. As the startdate and stopdate options need to implemented completely, should I make use of an interval
or is using two different tokens viz. START_TSTAMP and STOP_TSTAMP the preferred option.

Sorry, I had mistaken the way intervals were scanned in the rule. This above doubt is cleared.
Moving on to the part of not using relative times would it be required to first implement it in the nftables datatypes or is there another available workaround using the existing nftables code?