Skip to contentSkip to conversation

netfilter: nft_meta: support for time matching

PATCH In review Ander Juaristi 4 participants 7 messages Aug 2, 2019 – Aug 9, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 netfilter: nft_meta: support for time matching

This patch introduces meta matches in the kernel for time (a UNIX timestamp), day (a day of week, represented as an integer between 0-6), and hour (an hour in the current day, or: number of seconds since midnight).

All values are taken as unsigned 64-bit integers.

The 'time' keyword is internally converted to nanoseconds by nft in userspace, and hence the timestamp is taken in nanoseconds as well.

This patch also introduces a new function, nft_reg_store64, to store 64-bit values in the register for comparison.

---
 include/net/netfilter/nf_tables.h        |  6 ++++
 include/uapi/linux/netfilter/nf_tables.h |  8 +++--
 net/netfilter/nft_meta.c                 | 39 ++++++++++++++++++++++++
 3 files changed, 51 insertions(+), 2 deletions(-)
include/net/netfilter/nf_tables.h +6 -0
index 9b624566b82d..f635b9c2e221 100644
@@ -2,6 +2,7 @@
 #ifndef _NET_NF_TABLES_H
 #define _NET_NF_TABLES_H
 
+#include <asm/unaligned.h>
 #include <linux/list.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter/nfnetlink.h>
@@ -119,6 +120,11 @@ static inline void nft_reg_store8(u32 *dreg, u8 val)
 	*(u8 *)dreg = val;
 }
 
+static inline void nft_reg_store64(u64 *dreg, u64 val)
+{
+	put_unaligned(val, dreg);
+}
+
 static inline u16 nft_reg_load16(u32 *sreg)
 {
 	return *(u16 *)sreg;
include/uapi/linux/netfilter/nf_tables.h +6 -2
index 82abaa183fc3..6d9dd120b466 100644
@@ -799,6 +799,9 @@ enum nft_exthdr_attributes {
  * @NFT_META_OIFKIND: packet output interface kind name (dev->rtnl_link_ops->kind)
  * @NFT_META_BRI_IIFPVID: packet input bridge port pvid
  * @NFT_META_BRI_IIFVPROTO: packet input bridge vlan proto
+ * @NFT_META_TIME: a UNIX timestamp
+ * @NFT_META_TIME_DAY: day of week
+ * @NFT_META_TIME_HOUR: hour of day
  */
 enum nft_meta_keys {
 	NFT_META_LEN,
@@ -829,8 +832,9 @@ enum nft_meta_keys {
 	NFT_META_SECPATH,
 	NFT_META_IIFKIND,
 	NFT_META_OIFKIND,
-	NFT_META_BRI_IIFPVID,
-	NFT_META_BRI_IIFVPROTO,
+	NFT_META_TIME,
+	NFT_META_TIME_DAY,
+	NFT_META_TIME_HOUR,
 };
 
 /**
net/netfilter/nft_meta.c +39 -0
index f1b1d948c07b..57ac1f9aab56 100644
@@ -28,6 +28,27 @@
 
 static DEFINE_PER_CPU(struct rnd_state, nft_prandom_state);
 
+static u8 nft_meta_weekday(u64 secs)
+{
+	u8 wday;
+	unsigned int dse;
+
+	secs -= 60 * sys_tz.tz_minuteswest;
+	dse = secs / 86400;
+	wday = (4 + dse) % 7;
+
+	return ++wday % 7;
+}
+
+static u32 nft_meta_hour(u64 secs)
+{
+	struct tm tm;
+
+	time64_to_tm(secs, 0, &tm);
+
+	return tm.tm_hour * 3600 + tm.tm_min * 60 + tm.tm_sec;
+}
+
 void nft_meta_get_eval(const struct nft_expr *expr,
 		       struct nft_regs *regs,
 		       const struct nft_pktinfo *pkt)
@@ -226,6 +247,15 @@ 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_TIME:
+		nft_reg_store64((u64 *)dest, ktime_get_real_ns());
+		break;
+	case NFT_META_TIME_DAY:
+		nft_reg_store8(dest, nft_meta_weekday(get_seconds()));
+		break;
+	case NFT_META_TIME_HOUR:
+		*dest = nft_meta_hour(get_seconds());
+		break;
 	default:
 		WARN_ON(1);
 		goto err;
@@ -338,6 +368,15 @@ int nft_meta_get_init(const struct nft_ctx *ctx,
 		len = sizeof(u8);
 		break;
 #endif
+	case NFT_META_TIME:
+		len = sizeof(u64);
+		break;
+	case NFT_META_TIME_DAY:
+		len = sizeof(u8);
+		break;
+	case NFT_META_TIME_HOUR:
+		len = sizeof(u32);
+		break;
 	default:
 		return -EOPNOTSUPP;
 	}
Signature · 1 line
2.17.1
#1 lore raw
Quoted 15 lines · Ander Juaristi
include/uapi/linux/netfilter/nf_tables.h +3 -0
index 82abaa183fc3..6d9dd120b466 100644
@@ -799,6 +799,9 @@ enum nft_exthdr_attributes {
   * @NFT_META_OIFKIND: packet output interface kind name (dev->rtnl_link_ops->kind)
   * @NFT_META_BRI_IIFPVID: packet input bridge port pvid
   * @NFT_META_BRI_IIFVPROTO: packet input bridge vlan proto
+ * @NFT_META_TIME: a UNIX timestamp
+ * @NFT_META_TIME_DAY: day of week
+ * @NFT_META_TIME_HOUR: hour of day
   */
  enum nft_meta_keys {
  	NFT_META_LEN,

include/uapi/linux/netfilter/nf_tables.h enum nft_meta_keys { · line 833

Quoted 5 lines
include/uapi/linux/netfilter/nf_tables.h +0 -2
@@ -829,8 +832,9 @@ enum nft_meta_keys {
  	NFT_META_SECPATH,
  	NFT_META_IIFKIND,
  	NFT_META_OIFKIND,
-	NFT_META_BRI_IIFPVID,
-	NFT_META_BRI_IIFVPROTO,

I needed to remove these two so that the next three constants take the correct values (otherwise it won't work because the meta keys sent by userspace and those expected by the kernel don't match).

Those two constants NFT_META_BRI_IIFPVID and NFT_META_BRI_IIFVPROTO aren't defined in nftables, I don't know why.

I leave up to you to decide how to merge this: either manually give NFT_META_TIME the correct value, or replicate NFT_META_BRI_IIFPVID and NFT_META_BRI_IIFVPROTO in nftables.

Quoted 6 lines
include/uapi/linux/netfilter/nf_tables.h +3 -0
+	NFT_META_TIME,
+	NFT_META_TIME_DAY,
+	NFT_META_TIME_HOUR,
  };
 
  /**
maintainer
#2 lore raw
Quoted 19 lines · Ander Juaristi
Quoted 16 lines · Ander Juaristi
include/uapi/linux/netfilter/nf_tables.h +3 -0
index 82abaa183fc3..6d9dd120b466 100644
@@ -799,6 +799,9 @@ enum nft_exthdr_attributes {
   * @NFT_META_OIFKIND: packet output interface kind name (dev->rtnl_link_ops->kind)
   * @NFT_META_BRI_IIFPVID: packet input bridge port pvid
   * @NFT_META_BRI_IIFVPROTO: packet input bridge vlan proto
+ * @NFT_META_TIME: a UNIX timestamp
+ * @NFT_META_TIME_DAY: day of week
+ * @NFT_META_TIME_HOUR: hour of day
   */
  enum nft_meta_keys {
  	NFT_META_LEN,
@@ -829,8 +832,9 @@ enum nft_meta_keys {
  	NFT_META_SECPATH,
Quoted 8 lines

include/uapi/linux/netfilter/nf_tables.h enum nft_meta_keys { · line 833

Quoted 4 lines
include/uapi/linux/netfilter/nf_tables.h +0 -2
  	NFT_META_IIFKIND,
  	NFT_META_OIFKIND,
-	NFT_META_BRI_IIFPVID,
-	NFT_META_BRI_IIFVPROTO,

I needed to remove these two so that the next three constants take the correct values (otherwise it won't work because the meta keys sent by userspace and those expected by the kernel don't match).

This breaks the build.

Quoted 2 lines

Those two constants NFT_META_BRI_IIFPVID and NFT_META_BRI_IIFVPROTO aren't defined in nftables, I don't know why.

The userspace patch has not been applied yet, only the kernel one.

You can include a pre-patch in your series that adds the enums.

"sync meta keys with kernel" or similar.

#3 lore raw
In reply to Ander Juaristi
Thank you for the patch! Yet something to improve:

Hi Ander,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[cannot apply to v5.3-rc3 next-20190807]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Ander-Juaristi/netfilter-nft_meta-support-for-time-matching/20190804-141253
config: m68k-defconfig (attached as .config)
compiler: m68k-linux-gcc (GCC) 7.4.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=m68k

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

>> ERROR: "__udivdi3" [net/netfilter/nf_tables.ko] undefined!

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#4 lore raw
In reply to Ander Juaristi
Thank you for the patch! Perhaps something to improve:

Hi Ander,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[cannot apply to v5.3-rc3 next-20190807]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Ander-Juaristi/netfilter-nft_meta-support-for-time-matching/20190804-141253
reproduce:

        # apt-get install sparse
        # sparse version: v0.6.1-rc1-7-g2b96cd8-dirty
        make ARCH=x86_64 allmodconfig
        make C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__'

If you fix the issue, kindly add following tag

sparse warnings: (new ones prefixed by >>)

include/linux/sched.h:609:43: sparse: sparse: bad integer constant expression include/linux/sched.h:609:73: sparse: sparse: invalid named zero-width bitfield value' include/linux/sched.h:610:43: sparse: sparse: bad integer constant expression include/linux/sched.h:610:67: sparse: sparse: invalid named zero-width bitfield bucket_id' net/bridge/netfilter/nft_meta_bridge.c:41:14: sparse: sparse: undefined identifier 'NFT_META_BRI_IIFPVID' net/bridge/netfilter/nft_meta_bridge.c:52:14: sparse: sparse: undefined identifier 'NFT_META_BRI_IIFVPROTO'

>> net/bridge/netfilter/nft_meta_bridge.c:41:14: sparse: sparse: incompatible types for 'case' statement

net/bridge/netfilter/nft_meta_bridge.c:52:14: sparse: sparse: incompatible types for 'case' statement net/bridge/netfilter/nft_meta_bridge.c:88:14: sparse: sparse: undefined identifier 'NFT_META_BRI_IIFPVID' net/bridge/netfilter/nft_meta_bridge.c:89:14: sparse: sparse: undefined identifier 'NFT_META_BRI_IIFVPROTO' net/bridge/netfilter/nft_meta_bridge.c:88:14: sparse: sparse: incompatible types for 'case' statement net/bridge/netfilter/nft_meta_bridge.c:89:14: sparse: sparse: incompatible types for 'case' statement net/bridge/netfilter/nft_meta_bridge.c:41:14: sparse: sparse: Expected constant expression in case statement net/bridge/netfilter/nft_meta_bridge.c:52:14: sparse: sparse: Expected constant expression in case statement net/bridge/netfilter/nft_meta_bridge.c:88:14: sparse: sparse: Expected constant expression in case statement net/bridge/netfilter/nft_meta_bridge.c:89:14: sparse: sparse: Expected constant expression in case statement

vim +/case +41 net/bridge/netfilter/nft_meta_bridge.c

30e103fe24debc wenxu 2019-07-05  20  
30e103fe24debc wenxu 2019-07-05  21  static void nft_meta_bridge_get_eval(const struct nft_expr *expr,
30e103fe24debc wenxu 2019-07-05  22  				     struct nft_regs *regs,
30e103fe24debc wenxu 2019-07-05  23  				     const struct nft_pktinfo *pkt)
30e103fe24debc wenxu 2019-07-05  24  {
30e103fe24debc wenxu 2019-07-05  25  	const struct nft_meta *priv = nft_expr_priv(expr);
30e103fe24debc wenxu 2019-07-05  26  	const struct net_device *in = nft_in(pkt), *out = nft_out(pkt);
30e103fe24debc wenxu 2019-07-05  27  	u32 *dest = &regs->data[priv->dreg];
9d6a1ecdc99717 wenxu 2019-07-05  28  	const struct net_device *br_dev;
30e103fe24debc wenxu 2019-07-05  29  
30e103fe24debc wenxu 2019-07-05  30  	switch (priv->key) {
30e103fe24debc wenxu 2019-07-05  31  	case NFT_META_BRI_IIFNAME:
9d6a1ecdc99717 wenxu 2019-07-05  32  		br_dev = nft_meta_get_bridge(in);
9d6a1ecdc99717 wenxu 2019-07-05  33  		if (!br_dev)
30e103fe24debc wenxu 2019-07-05  34  			goto err;
30e103fe24debc wenxu 2019-07-05  35  		break;
30e103fe24debc wenxu 2019-07-05  36  	case NFT_META_BRI_OIFNAME:
9d6a1ecdc99717 wenxu 2019-07-05  37  		br_dev = nft_meta_get_bridge(out);
9d6a1ecdc99717 wenxu 2019-07-05  38  		if (!br_dev)
30e103fe24debc wenxu 2019-07-05  39  			goto err;
30e103fe24debc wenxu 2019-07-05  40  		break;
c54c7c685494fc wenxu 2019-07-05 @41  	case NFT_META_BRI_IIFPVID: {
c54c7c685494fc wenxu 2019-07-05  42  		u16 p_pvid;
c54c7c685494fc wenxu 2019-07-05  43  
c54c7c685494fc wenxu 2019-07-05  44  		br_dev = nft_meta_get_bridge(in);
c54c7c685494fc wenxu 2019-07-05  45  		if (!br_dev || !br_vlan_enabled(br_dev))
c54c7c685494fc wenxu 2019-07-05  46  			goto err;
c54c7c685494fc wenxu 2019-07-05  47  
c54c7c685494fc wenxu 2019-07-05  48  		br_vlan_get_pvid_rcu(in, &p_pvid);
c54c7c685494fc wenxu 2019-07-05  49  		nft_reg_store16(dest, p_pvid);
c54c7c685494fc wenxu 2019-07-05  50  		return;
c54c7c685494fc wenxu 2019-07-05  51  	}
2a3a93ef0ba516 wenxu 2019-07-05  52  	case NFT_META_BRI_IIFVPROTO: {
2a3a93ef0ba516 wenxu 2019-07-05  53  		u16 p_proto;
2a3a93ef0ba516 wenxu 2019-07-05  54  
2a3a93ef0ba516 wenxu 2019-07-05  55  		br_dev = nft_meta_get_bridge(in);
2a3a93ef0ba516 wenxu 2019-07-05  56  		if (!br_dev || !br_vlan_enabled(br_dev))
2a3a93ef0ba516 wenxu 2019-07-05  57  			goto err;
2a3a93ef0ba516 wenxu 2019-07-05  58  
2a3a93ef0ba516 wenxu 2019-07-05  59  		br_vlan_get_proto(br_dev, &p_proto);
2a3a93ef0ba516 wenxu 2019-07-05  60  		nft_reg_store16(dest, p_proto);
2a3a93ef0ba516 wenxu 2019-07-05  61  		return;
2a3a93ef0ba516 wenxu 2019-07-05  62  	}
30e103fe24debc wenxu 2019-07-05  63  	default:
30e103fe24debc wenxu 2019-07-05  64  		goto out;
30e103fe24debc wenxu 2019-07-05  65  	}
30e103fe24debc wenxu 2019-07-05  66  
9d6a1ecdc99717 wenxu 2019-07-05  67  	strncpy((char *)dest, br_dev->name, IFNAMSIZ);
30e103fe24debc wenxu 2019-07-05  68  	return;
30e103fe24debc wenxu 2019-07-05  69  out:
30e103fe24debc wenxu 2019-07-05  70  	return nft_meta_get_eval(expr, regs, pkt);
30e103fe24debc wenxu 2019-07-05  71  err:
30e103fe24debc wenxu 2019-07-05  72  	regs->verdict.code = NFT_BREAK;
30e103fe24debc wenxu 2019-07-05  73  }
30e103fe24debc wenxu 2019-07-05  74  

:::::: The code at line 41 was first introduced by commit
:::::: c54c7c685494fc0f1662091d4d0c4fc26e810471 netfilter: nft_meta_bridge: add NFT_META_BRI_IIFPVID support

:::::: TO: wenxu <[email protected]>
:::::: CC: Pablo Neira Ayuso <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#5 lore raw
In reply to Ander Juaristi
Thank you for the patch! Yet something to improve:

Hi Ander,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[cannot apply to v5.3-rc3 next-20190807]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Ander-Juaristi/netfilter-nft_meta-support-for-time-matching/20190804-141253
config: i386-randconfig-g003-201931 (attached as .config)
compiler: gcc-7 (Debian 7.4.0-10) 7.4.0 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

ld: net/netfilter/nft_meta.o: in function `nft_meta_weekday':

>> net/netfilter/nft_meta.c:37: undefined reference to `__udivdi3'

vim +37 net/netfilter/nft_meta.c

    30	
    31	static u8 nft_meta_weekday(u64 secs)
    32	{
    33		u8 wday;
    34		unsigned int dse;
    35	
    36		secs -= 60 * sys_tz.tz_minuteswest;
  > 37		dse = secs / 86400;
    38		wday = (4 + dse) % 7;
    39	
    40		return ++wday % 7;
    41	}
    42	

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#6 lore raw
In reply to Ander Juaristi
Quoted 13 lines · Ander Juaristi
include/net/netfilter/nf_tables.h +1 -0
index 9b624566b82d..f635b9c2e221 100644
@@ -2,6 +2,7 @@
 #ifndef _NET_NF_TABLES_H
 #define _NET_NF_TABLES_H
 
+#include <asm/unaligned.h>
 #include <linux/list.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter/nfnetlink.h>

include/net/netfilter/nf_tables.h static inline void nft_reg_store8(u32 *dreg, u8 val) · line 126

Quoted 7 lines
include/net/netfilter/nf_tables.h +4 -0
@@ -119,6 +120,11 @@ static inline void nft_reg_store8(u32 *dreg, u8 val)
 	*(u8 *)dreg = val;
 }
 
+static inline void nft_reg_store64(u64 *dreg, u64 val)
+{
+	put_unaligned(val, dreg);
+}

Could you make an initial patch to add nft_reg_load64() and nft_reg_store64()? That patch should also update nft_byteorder to use it. That would be patch 1/2.

Then, you place this patch 2/2 to add time support after 1/2.

Thanks.