Skip to contentSkip to conversation

nf_tables encapsulation/decapsulation support

RFC PATCH In review Pablo Neira Ayuso 2 participants 8 messages Oct 22, 2019 – Oct 23, 2019

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover nf_tables encapsulation/decapsulation support

Hi,

This is a RFC patchset, untested, to introduce new infrastructure to specify protocol decapsulation and encapsulation actions. This patchset comes with initial support for VLAN, eg.

1) VLAN decapsulation:

	... meta iif . vlan id { eth0 . 10, eth1 . 11} decap vlan

The decapsulation is a single statement with no extra options.

2) VLAN encapsulation:

	add vlan "network0" { type push; id 100; proto 0x8100; }
        add vlan "network1" { type update; id 101; }
	... encap vlan set ip daddr map { 192.168.0.0/24 : "network0",
					  192.168.1.0/24 : "network1" }

The idea is that the user specifies the vlan policy through object definition, eg. "network0" and "network1", then it applies this policy via the "encap vlan set" statement.

This infrastructure should allow for more encapsulation protocols with little work, eg. MPLS.

I have places the encap object and the decap expression in the same nft_encap module.

I'm still considering to extend the object infrastructure to specify the operation type through the rule, ie.

	add vlan "network0" { id 100; proto 0x8100; }
        add vlan "network1" { id 101; }
	... encap vlan push ip daddr map { 192.168.0.0/24 : "network0",
					   192.168.1.0/24 : "network1" }

So the VLAN object does not come with the operation type, instead this is specified through the encap statement, that would require a bit more work on the object infrastructure which is probably a good idea.

This is work-in-progress, syntax is tentative, comments welcome.

Thanks.

Pablo Neira Ayuso (2):
netfilter: nf_tables: add decapsulation support
netfilter: nf_tables: add encapsulation support

 include/uapi/linux/netfilter/nf_tables.h |  56 ++++-
 net/netfilter/Kconfig                    |   6 +
 net/netfilter/Makefile                   |   1 +
 net/netfilter/nft_encap.c                | 341 +++++++++++++++++++++++++++++++
 4 files changed, 403 insertions(+), 1 deletion(-)
 create mode 100644 net/netfilter/nft_encap.c

--
2.11.0

author maintainer
#1 lore raw

RFC PATCH 1/2 netfilter: nf_tables: add decapsulation support

This patch adds support for the decapsulation infrastructure, including VLAN support for this.

---
 include/uapi/linux/netfilter/nf_tables.h |  16 +++++
 net/netfilter/Kconfig                    |   6 ++
 net/netfilter/Makefile                   |   1 +
 net/netfilter/nft_encap.c                | 119 +++++++++++++++++++++++++++++++
 4 files changed, 142 insertions(+)
 create mode 100644 net/netfilter/nft_encap.c
include/uapi/linux/netfilter/nf_tables.h +16 -0
index 81fed16fe2b2..25e26340a0ba 100644
@@ -1625,6 +1625,22 @@ enum nft_xfrm_keys {
 };
 #define NFT_XFRM_KEY_MAX (__NFT_XFRM_KEY_MAX - 1)
 
+enum nft_encap_type {
+	NFT_ENCAP_VLAN	= 0,
+};
+
+/**
+ * enum nft_decap_attributes - nf_tables decapsulation expression netlink attributes
+ *
+ * @NFTA_DECAP_TYPE: decapsulation type (NLA_U32)
+ */
+enum nft_decap_attributes {
+	NFTA_DECAP_UNSPEC,
+	NFTA_DECAP_TYPE,
+	__NFTA_DECAP_MAX,
+};
+#define NFTA_DECAP_MAX	(__NFTA_DECAP_MAX - 1)
+
 /**
  * enum nft_trace_attributes - nf_tables trace netlink attributes
  *
net/netfilter/Kconfig +6 -0
index 91efae88e8c2..573ea56aecfe 100644
@@ -489,6 +489,12 @@ config NFT_CT
 	  This option adds the "ct" expression that you can use to match
 	  connection tracking information such as the flow state.
 
+config NFT_ENCAP
+	tristate "Netfilter nf_tables encapsulation/decapsulation module"
+	help
+	  This option adds the encapsulation expression used to decapsulate
+	  and to encapsulate packets through VLAN.
+
 config NFT_FLOW_OFFLOAD
 	depends on NF_CONNTRACK && NF_FLOW_TABLE
 	tristate "Netfilter nf_tables hardware flow offload module"
net/netfilter/Makefile +1 -0
index 4fc075b612fe..be8345c14a6e 100644
@@ -89,6 +89,7 @@ obj-$(CONFIG_NFT_COMPAT)	+= nft_compat.o
 obj-$(CONFIG_NFT_CONNLIMIT)	+= nft_connlimit.o
 obj-$(CONFIG_NFT_NUMGEN)	+= nft_numgen.o
 obj-$(CONFIG_NFT_CT)		+= nft_ct.o
+obj-$(CONFIG_NFT_ENCAP)		+= nft_encap.o
 obj-$(CONFIG_NFT_FLOW_OFFLOAD)	+= nft_flow_offload.o
 obj-$(CONFIG_NFT_LIMIT)		+= nft_limit.o
 obj-$(CONFIG_NFT_NAT)		+= nft_nat.o
net/netfilter/nft_encap.c new file +119 -0

119 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#2 lore raw

RFC PATCH 2/2 netfilter: nf_tables: add encapsulation support

In reply to Pablo Neira Ayuso

This patch adds encapsulation support through the encapsulation object, that specifies the encapsulation policy.

---
 include/uapi/linux/netfilter/nf_tables.h |  40 +++++-
 net/netfilter/nft_encap.c                | 224 ++++++++++++++++++++++++++++++-
 2 files changed, 262 insertions(+), 2 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +39 -1
index 25e26340a0ba..e5997a13ba71 100644
@@ -1484,7 +1484,8 @@ enum nft_ct_expectation_attributes {
 #define NFT_OBJECT_SECMARK	8
 #define NFT_OBJECT_CT_EXPECT	9
 #define NFT_OBJECT_SYNPROXY	10
-#define __NFT_OBJECT_MAX	11
+#define NFT_OBJECT_ENCAP	11
+#define __NFT_OBJECT_MAX	12
 #define NFT_OBJECT_MAX		(__NFT_OBJECT_MAX - 1)
 
 /**
@@ -1629,6 +1630,43 @@ enum nft_encap_type {
 	NFT_ENCAP_VLAN	= 0,
 };
 
+enum nft_encap_op {
+	NFT_ENCAP_ADD	= 0,
+	NFT_ENCAP_UPDATE,
+};
+
+/**
+ * enum nft_encap_vlan_attributes - nf_tables VLAN encapsulation expression netlink attributes
+ *
+ * @NFTA_ENCAP_VLAN_ID: VLAN id (NLA_U16)
+ * @NFTA_ENCAP_VLAN_PROTO: VLAN protocol (NLA_U16)
+ * @NFTA_ENCAP_VLAN_PRIO: VLAN priority (NLA_U8)
+ */
+enum nft_encap_vlan_attributes {
+	NFTA_ENCAP_VLAN_UNSPEC,
+	NFTA_ENCAP_VLAN_ID,
+	NFTA_ENCAP_VLAN_PROTO,
+	NFTA_ENCAP_VLAN_PRIO,
+	__NFTA_ENCAP_VLAN_MAX
+};
+#define NFTA_ENCAP_VLAN_MAX	(__NFTA_ENCAP_VLAN_MAX - 1)
+
+/**
+ * enum nft_encap_attributes - nf_tables encapsulation expression netlink attributes
+ *
+ * @NFTA_ENCAP_TYPE: encapsulation type (NLA_U32)
+ * @NFTA_ENCAP_OP: encapsulation operation (NLA_U32)
+ * @NFTA_ENCAP_DATA: encapsulation data (NLA_NESTED)
+ */
+enum nft_encap_attributes {
+	NFTA_ENCAP_UNSPEC,
+	NFTA_ENCAP_TYPE,
+	NFTA_ENCAP_OP,
+	NFTA_ENCAP_DATA,
+	__NFTA_ENCAP_MAX
+};
+#define NFTA_ENCAP_MAX	(__NFTA_ENCAP_MAX - 1)
+
 /**
  * enum nft_decap_attributes - nf_tables decapsulation expression netlink attributes
  *
net/netfilter/nft_encap.c +223 -1

243 lines not on this page · raw message

Signature · 1 line
2.11.0
#3 lore raw

Re: [PATCH nf-next,RFC 2/2] netfilter: nf_tables: add encapsulation support

Quoted 179 lines · Pablo Neira Ayuso

This patch adds encapsulation support through the encapsulation object, that specifies the encapsulation policy.

---
 include/uapi/linux/netfilter/nf_tables.h |  40 +++++-
 net/netfilter/nft_encap.c                | 224 ++++++++++++++++++++++++++++++-
 2 files changed, 262 insertions(+), 2 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +39 -1
index 25e26340a0ba..e5997a13ba71 100644
@@ -1484,7 +1484,8 @@ enum nft_ct_expectation_attributes {
 #define NFT_OBJECT_SECMARK	8
 #define NFT_OBJECT_CT_EXPECT	9
 #define NFT_OBJECT_SYNPROXY	10
-#define __NFT_OBJECT_MAX	11
+#define NFT_OBJECT_ENCAP	11
+#define __NFT_OBJECT_MAX	12
 #define NFT_OBJECT_MAX		(__NFT_OBJECT_MAX - 1)
 
 /**
@@ -1629,6 +1630,43 @@ enum nft_encap_type {
 	NFT_ENCAP_VLAN	= 0,
 };
 
+enum nft_encap_op {
+	NFT_ENCAP_ADD	= 0,
+	NFT_ENCAP_UPDATE,
+};
+
+/**
+ * enum nft_encap_vlan_attributes - nf_tables VLAN encapsulation expression netlink attributes
+ *
+ * @NFTA_ENCAP_VLAN_ID: VLAN id (NLA_U16)
+ * @NFTA_ENCAP_VLAN_PROTO: VLAN protocol (NLA_U16)
+ * @NFTA_ENCAP_VLAN_PRIO: VLAN priority (NLA_U8)
+ */
+enum nft_encap_vlan_attributes {
+	NFTA_ENCAP_VLAN_UNSPEC,
+	NFTA_ENCAP_VLAN_ID,
+	NFTA_ENCAP_VLAN_PROTO,
+	NFTA_ENCAP_VLAN_PRIO,
+	__NFTA_ENCAP_VLAN_MAX
+};
+#define NFTA_ENCAP_VLAN_MAX	(__NFTA_ENCAP_VLAN_MAX - 1)
+
+/**
+ * enum nft_encap_attributes - nf_tables encapsulation expression netlink attributes
+ *
+ * @NFTA_ENCAP_TYPE: encapsulation type (NLA_U32)
+ * @NFTA_ENCAP_OP: encapsulation operation (NLA_U32)
+ * @NFTA_ENCAP_DATA: encapsulation data (NLA_NESTED)
+ */
+enum nft_encap_attributes {
+	NFTA_ENCAP_UNSPEC,
+	NFTA_ENCAP_TYPE,
+	NFTA_ENCAP_OP,
+	NFTA_ENCAP_DATA,
+	__NFTA_ENCAP_MAX
+};
+#define NFTA_ENCAP_MAX	(__NFTA_ENCAP_MAX - 1)
+
 /**
  * enum nft_decap_attributes - nf_tables decapsulation expression netlink attributes
  *
net/netfilter/nft_encap.c +97 -0
index 657a62e4c283..13643b3daf85 100644
@@ -2,6 +2,7 @@
 #include <linux/kernel.h>
 #include <linux/init.h>
 #include <linux/module.h>
+#include <linux/if_vlan.h>
 #include <linux/netlink.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter/nf_tables.h>
@@ -101,14 +102,235 @@ static struct nft_expr_type nft_decap_type __read_mostly = {
 	.owner		= THIS_MODULE,
 };
 
+struct nft_encap {
+	enum nft_encap_type	type;
+	enum nft_encap_op	op;
+
+	union {
+		struct {
+			__u16	id;
+			__be16	proto;
+			__u8	prio;
+		} vlan;
+	};
+};
+
+static u16 nft_encap_vlan_tci(struct nft_encap *priv)
+{
+	return priv->vlan.id | (priv->vlan.prio << VLAN_PRIO_SHIFT);
+}
+
+static int nft_encap_vlan_eval(struct nft_encap *priv,
+			       struct nft_regs *regs,
+			       const struct nft_pktinfo *pkt)
+{
+	struct sk_buff *skb = pkt->skb;
+	int err;
+	u16 tci;
+
+	switch (priv->op) {
+	case NFT_ENCAP_ADD:
+		err = skb_vlan_push(skb, priv->vlan.proto,
+				    nft_encap_vlan_tci(priv));
+		if (err)
+			return err;
+		break;
+	case NFT_ENCAP_UPDATE:
+		if (!skb_vlan_tagged(skb))
+			return -1;
+
+		err = 0;
+		if (skb_vlan_tag_present(skb)) {
+			tci = skb_vlan_tag_get(skb);
+			__vlan_hwaccel_clear_tag(skb);
+		} else {
+			err = __skb_vlan_pop(skb, &tci);
+		}
+		if (err)
+			return err;
+
+		tci = (tci & ~VLAN_VID_MASK) | priv->vlan.id;
+		if (priv->vlan.prio) {
+			tci &= ~VLAN_PRIO_MASK;
+			tci |= priv->vlan.prio << VLAN_PRIO_SHIFT;
+		}
+
+		__vlan_hwaccel_put_tag(skb, priv->vlan.proto, tci);
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+
+static void nft_encap_obj_eval(struct nft_object *obj,
+			       struct nft_regs *regs,
+			       const struct nft_pktinfo *pkt)
+{
+	struct nft_encap *priv = nft_obj_data(obj);
+	int err;
+
+	switch (priv->type) {
+	case NFT_ENCAP_VLAN:
+		err = nft_encap_vlan_eval(priv, regs, pkt);
+		break;
+	default:
+		WARN_ON_ONCE(1);
+		err = -1;
+	}
+
+	if (err < 0)
+		regs->verdict.code = NFT_BREAK;
+}
+
+static const struct nla_policy nft_encap_vlan_policy[NFTA_ENCAP_VLAN_MAX + 1] = {
+	[NFTA_ENCAP_VLAN_ID]	= { .type = NLA_U16 },
+	[NFTA_ENCAP_VLAN_PROTO]	= { .type = NLA_U16 },
+	[NFTA_ENCAP_VLAN_PRIO]	= { .type = NLA_U8 },
+};
+
+static int nft_encap_vlan_parse(const struct nlattr *attr,
+				struct nft_encap *priv)
+{
+	struct nlattr *tb[NFTA_ENCAP_VLAN_MAX + 1];
+	int err;
+
+	err = nla_parse_nested_deprecated(tb, NFTA_ENCAP_VLAN_MAX, attr,
+					  nft_encap_vlan_policy, NULL);

net/netfilter/nft_encap.c static struct nft_expr_type nft_decap_type __read_mostly = { · line 210

Quoted 10 lines
net/netfilter/nft_encap.c +10 -0
+	if (err < 0)
+		return err;
+
+	if (!tb[NFTA_ENCAP_VLAN_PRIO] ||
+	    !tb[NFTA_ENCAP_VLAN_PROTO] ||
+	    !tb[NFTA_ENCAP_VLAN_ID])
+		return -EINVAL;
+
+	priv->vlan.id = ntohs(nla_get_be16(tb[NFTA_ENCAP_VLAN_ID]));
+	priv->vlan.proto = nla_get_be16(tb[NFTA_ENCAP_VLAN_PROTO]);

Maybe it is better to check the vlan.proto is ETH_P_8021Q or ETH_P_8021AD?

Quoted 35 lines
net/netfilter/nft_encap.c +34 -0
+	priv->vlan.prio = nla_get_u8(tb[NFTA_ENCAP_VLAN_PRIO]);
+
+	return 0;
+}
+
+static int nft_encap_obj_init(const struct nft_ctx *ctx,
+			      const struct nlattr * const tb[],
+			      struct nft_object *obj)
+{
+	struct nft_encap *priv = nft_obj_data(obj);
+	int err = 0;
+
+	if (!tb[NFTA_ENCAP_TYPE] ||
+	    !tb[NFTA_ENCAP_OP])
+		return -EINVAL;
+
+	priv->type = ntohl(nla_get_be32(tb[NFTA_ENCAP_TYPE]));
+	priv->op = ntohl(nla_get_be32(tb[NFTA_ENCAP_OP]));
+
+	switch (priv->type) {
+	case NFT_ENCAP_VLAN:
+		if (priv->op != NFT_ENCAP_ADD &&
+		    priv->op != NFT_ENCAP_UPDATE)
+			return -EOPNOTSUPP;
+
+		err = nft_encap_vlan_parse(tb[NFTA_ENCAP_DATA], priv);
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	return err;
+}
+
 
#4 lore raw
In reply to Pablo Neira Ayuso
Quoted 11 lines · Pablo Neira Ayuso

Hi,

This is a RFC patchset, untested, to introduce new infrastructure to specify protocol decapsulation and encapsulation actions. This patchset comes with initial support for VLAN, eg.

1) VLAN decapsulation:

	... meta iif . vlan id { eth0 . 10, eth1 . 11} decap vlan

The decapsulation is a single statement with no extra options.

Currently there is no vlan meta match expr.  So it is better to extend the meta expr or add new

ntf_vlan_get_expr?

Quoted 8 lines

2) VLAN encapsulation:

	add vlan "network0" { type push; id 100; proto 0x8100; }
        add vlan "network1" { type update; id 101; }
	... encap vlan set ip daddr map { 192.168.0.0/24 : "network0",
					  192.168.1.0/24 : "network1" }
Quoted 6 lines

The idea is that the user specifies the vlan policy through object definition, eg. "network0" and "network1", then it applies this policy via the "encap vlan set" statement.

This infrastructure should allow for more encapsulation protocols with little work, eg. MPLS.

So the tunnel already exist in nft_tunnel also can add in this encapsulation protocols

as ip.

like ip-route

encap ip id 100 dst 10.0.0.1?

Quoted 35 lines

I have places the encap object and the decap expression in the same nft_encap module.

I'm still considering to extend the object infrastructure to specify the operation type through the rule, ie.

	add vlan "network0" { id 100; proto 0x8100; }
        add vlan "network1" { id 101; }
	... encap vlan push ip daddr map { 192.168.0.0/24 : "network0",
					   192.168.1.0/24 : "network1" }

So the VLAN object does not come with the operation type, instead this is specified through the encap statement, that would require a bit more work on the object infrastructure which is probably a good idea.

This is work-in-progress, syntax is tentative, comments welcome.

Thanks.

Pablo Neira Ayuso (2):
netfilter: nf_tables: add decapsulation support
netfilter: nf_tables: add encapsulation support

 include/uapi/linux/netfilter/nf_tables.h |  56 ++++-
 net/netfilter/Kconfig                    |   6 +
 net/netfilter/Makefile                   |   1 +
 net/netfilter/nft_encap.c                | 341 +++++++++++++++++++++++++++++++
 4 files changed, 403 insertions(+), 1 deletion(-)
 create mode 100644 net/netfilter/nft_encap.c

--
2.11.0

author maintainer
#5 lore raw
Quoted 8 lines · wenxu
Quoted 6 lines · Pablo Neira Ayuso

Hi,

This is a RFC patchset, untested, to introduce new infrastructure to specify protocol decapsulation and encapsulation actions. This patchset comes with initial support for VLAN, eg.

Quoted 8 lines
Quoted 5 lines

1) VLAN decapsulation:

	... meta iif . vlan id { eth0 . 10, eth1 . 11} decap vlan

The decapsulation is a single statement with no extra options.

Currently there is no vlan meta match expr.  So it is better to extend the meta expr or add new ntf_vlan_get_expr?

There's nft_payload to get the vlan information.

Quoted 14 lines
Quoted 7 lines

2) VLAN encapsulation:

	add vlan "network0" { type push; id 100; proto 0x8100; }
        add vlan "network1" { type update; id 101; }
	... encap vlan set ip daddr map { 192.168.0.0/24 : "network0",
					  192.168.1.0/24 : "network1" }
Quoted 6 lines

The idea is that the user specifies the vlan policy through object definition, eg. "network0" and "network1", then it applies this policy via the "encap vlan set" statement.

This infrastructure should allow for more encapsulation protocols with little work, eg. MPLS.

Quoted 6 lines

So the tunnel already exist in nft_tunnel also can add in this encapsulation protocols as ip.

like ip-route

encap ip id 100 dst 10.0.0.1?

Not sure what you mean, please, extend your coment.

#6 lore raw
Quoted 13 lines · Pablo Neira Ayuso
Quoted 13 lines
Quoted 13 lines

2) VLAN encapsulation:

	add vlan "network0" { type push; id 100; proto 0x8100; }
        add vlan "network1" { type update; id 101; }
	... encap vlan set ip daddr map { 192.168.0.0/24 : "network0",
					  192.168.1.0/24 : "network1" }

The idea is that the user specifies the vlan policy through object definition, eg. "network0" and "network1", then it applies this policy via the "encap vlan set" statement.

This infrastructure should allow for more encapsulation protocols with little work, eg. MPLS.

Quoted 7 lines
Quoted 6 lines

So the tunnel already exist in nft_tunnel also can add in this encapsulation protocols as ip.

like ip-route

encap ip id 100 dst 10.0.0.1?

Not sure what you mean, please, extend your coment.

For the future there maybe can add a new nft_encap_type NFT_ENCAP_IP which contain all

the ip tunnels such as vxlan, gre etc. This type of encap already in the nft_tunnel.

Quoted 1 line
#7 lore raw
In reply to Pablo Neira Ayuso
Quoted 8 lines · Pablo Neira Ayuso
Quoted 7 lines · wenxu
Quoted 6 lines · Pablo Neira Ayuso

Hi,

This is a RFC patchset, untested, to introduce new infrastructure to specify protocol decapsulation and encapsulation actions. This patchset comes with initial support for VLAN, eg.

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

1) VLAN decapsulation:

	... meta iif . vlan id { eth0 . 10, eth1 . 11} decap vlan

The decapsulation is a single statement with no extra options.

Currently there is no vlan meta match expr.  So it is better to extend the meta expr or add new ntf_vlan_get_expr?

There's nft_payload to get the vlan information.

There are some limtaion for geting the vlan information through nft_payload

1. It can't get the inner vlan(cvlan) information

2. geting the vlan information is based on offset on link header, There is no good way

to offload the vlan match expr.