Skip to contentSkip to conversation

net: sched: introduce em_ipt ematch

PATCH In review Eyal Birger 3 participants 7 messages Jan 26, 2018 – Jan 30, 2018

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

v2 cover net: sched: introduce em_ipt ematch

From: Eyal Birger <[email protected]>

The following patchset introduces a new tc ematch for matching using netfilter matches.

This allows early classification as well as mirroning/redirecting traffic based on logic implemented in netfilter extensions.

Example use case is classification based on the incoming IPSec policy used during decpsulation using the 'policy' iptables extension (xt_policy).

This patchset is an enhancement of a former series ([1]) which allowed only policy matching following a suggestion by Pablo Neira Ayuso ([2]).

[1] https://patchwork.ozlabs.org/cover/859887/
[2] https://patchwork.ozlabs.org/patch/859888/

v2:
Remove skb push/pull and limit functionality to ingress

Eyal Birger (2):
net: sched: ematch: pass protocol to ematch 'change()' handlers
net: sched: add em_ipt ematch for calling xtables matches

 include/net/pkt_cls.h                    |   2 +-
 include/uapi/linux/pkt_cls.h             |   3 +-
 include/uapi/linux/tc_ematch/tc_em_ipt.h |  19 +++
 net/sched/Kconfig                        |  10 ++
 net/sched/Makefile                       |   1 +
 net/sched/em_canid.c                     |   4 +-
 net/sched/em_ipset.c                     |   4 +-
 net/sched/em_ipt.c                       | 244 +++++++++++++++++++++++++++++++
 net/sched/em_meta.c                      |   2 +-
 net/sched/em_nbyte.c                     |   4 +-
 net/sched/em_text.c                      |   2 +-
 net/sched/ematch.c                       |   3 +-
 12 files changed, 287 insertions(+), 11 deletions(-)
 create mode 100644 include/uapi/linux/tc_ematch/tc_em_ipt.h
 create mode 100644 net/sched/em_ipt.c
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/2 net: sched: ematch: pass protocol to ematch 'change()' handlers

From: Eyal Birger <[email protected]>

In order to allow ematches to create their internal state based on the L3 protocol specified when creating the filter.

---
 include/net/pkt_cls.h | 2 +-
 net/sched/em_canid.c  | 4 ++--
 net/sched/em_ipset.c  | 4 ++--
 net/sched/em_meta.c   | 2 +-
 net/sched/em_nbyte.c  | 4 ++--
 net/sched/em_text.c   | 2 +-
 net/sched/ematch.c    | 3 ++-
 7 files changed, 11 insertions(+), 10 deletions(-)
include/net/pkt_cls.h +1 -1
index 8740625..ff5fcb1 100644
@@ -474,7 +474,7 @@ struct tcf_ematch_tree {
 struct tcf_ematch_ops {
 	int			kind;
 	int			datalen;
-	int			(*change)(struct net *net, void *,
+	int			(*change)(struct net *net, __be16, void *,
 					  int, struct tcf_ematch *);
 	int			(*match)(struct sk_buff *, struct tcf_ematch *,
 					 struct tcf_pkt_info *);
net/sched/em_canid.c +2 -2
index ddd883c..445c10d 100644
@@ -120,8 +120,8 @@ static int em_canid_match(struct sk_buff *skb, struct tcf_ematch *m,
 	return match;
 }
 
-static int em_canid_change(struct net *net, void *data, int len,
-			  struct tcf_ematch *m)
+static int em_canid_change(struct net *net, __be16 protocol, void *data,
+			   int len, struct tcf_ematch *m)
 {
 	struct can_filter *conf = data; /* Array with rules */
 	struct canid_match *cm;
net/sched/em_ipset.c +2 -2
index c1b23e3..50f7282 100644
@@ -19,8 +19,8 @@
 #include <net/ip.h>
 #include <net/pkt_cls.h>
 
-static int em_ipset_change(struct net *net, void *data, int data_len,
-			   struct tcf_ematch *em)
+static int em_ipset_change(struct net *net, __be16 protocol, void *data,
+			   int data_len, struct tcf_ematch *em)
 {
 	struct xt_set_info *set = data;
 	ip_set_id_t index;
net/sched/em_meta.c +1 -1
index d6e9711..6892efc 100644
@@ -904,7 +904,7 @@ static const struct nla_policy meta_policy[TCA_EM_META_MAX + 1] = {
 	[TCA_EM_META_HDR]	= { .len = sizeof(struct tcf_meta_hdr) },
 };
 
-static int em_meta_change(struct net *net, void *data, int len,
+static int em_meta_change(struct net *net, __be16 protocol, void *data, int len,
 			  struct tcf_ematch *m)
 {
 	int err;
net/sched/em_nbyte.c +2 -2
index 07c10ba..62a7a2e 100644
@@ -23,8 +23,8 @@ struct nbyte_data {
 	char			pattern[0];
 };
 
-static int em_nbyte_change(struct net *net, void *data, int data_len,
-			   struct tcf_ematch *em)
+static int em_nbyte_change(struct net *net, __be16 protocol, void *data,
+			   int data_len, struct tcf_ematch *em)
 {
 	struct tcf_em_nbyte *nbyte = data;
 
net/sched/em_text.c +1 -1
index 73e2ed5..b5d9e21 100644
@@ -44,7 +44,7 @@ static int em_text_match(struct sk_buff *skb, struct tcf_ematch *m,
 	return skb_find_text(skb, from, to, tm->config) != UINT_MAX;
 }
 
-static int em_text_change(struct net *net, void *data, int len,
+static int em_text_change(struct net *net, __be16 protocol, void *data, int len,
 			  struct tcf_ematch *m)
 {
 	struct text_match *tm;
net/sched/ematch.c +2 -1
index 1331a4c..a69abd8 100644
@@ -242,7 +242,8 @@ static int tcf_em_validate(struct tcf_proto *tp,
 			goto errout;
 
 		if (em->ops->change) {
-			err = em->ops->change(net, data, data_len, em);
+			err = em->ops->change(net, tp->protocol, data, data_len,
+			                      em);
 			if (err < 0)
 				goto errout;
 		} else if (data_len > 0) {
Signature · 1 line
2.7.4
#2 lore raw

PATCH v2 2/2 net: sched: add em_ipt ematch for calling xtables matches

In reply to Eyal Birger

From: Eyal Birger <[email protected]>

This module allows performing tc classification based on data structures and implementations provided by netfilter extensions.

Example use case is classification based on the incoming IPSec policy used during decpsulation using the 'policy' iptables extension (xt_policy).

Only tc ingress is supported in order to avoid modifying the skb at egress to suit xtable matches skb->data expectations.

---

This ematch tries its best to provide matches with a similar environment to the one they are used to; Due to the wide range of criteria supported by matches, I am not able to test every combination of match and traffic.

---
 include/uapi/linux/pkt_cls.h             |   3 +-
 include/uapi/linux/tc_ematch/tc_em_ipt.h |  19 +++
 net/sched/Kconfig                        |  10 ++
 net/sched/Makefile                       |   1 +
 net/sched/em_ipt.c                       | 244 +++++++++++++++++++++++++++++++
 5 files changed, 276 insertions(+), 1 deletion(-)
 create mode 100644 include/uapi/linux/tc_ematch/tc_em_ipt.h
 create mode 100644 net/sched/em_ipt.c
include/uapi/linux/pkt_cls.h +2 -1
index 46c5066..7cafb26d 100644
@@ -555,7 +555,8 @@ enum {
 #define	TCF_EM_VLAN		6
 #define	TCF_EM_CANID		7
 #define	TCF_EM_IPSET		8
-#define	TCF_EM_MAX		8
+#define	TCF_EM_IPT		9
+#define	TCF_EM_MAX		9
 
 enum {
 	TCF_EM_PROG_TC
include/uapi/linux/tc_ematch/tc_em_ipt.h new file +19 -0
new file mode 100644
index 0000000..aecd252
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef __LINUX_TC_EM_IPT_H
+#define __LINUX_TC_EM_IPT_H
+
+#include <linux/types.h>
+#include <linux/pkt_cls.h>
+
+enum {
+	TCA_EM_IPT_UNSPEC,
+	TCA_EM_IPT_HOOK,
+	TCA_EM_IPT_MATCH_NAME,
+	TCA_EM_IPT_MATCH_REVISION,
+	TCA_EM_IPT_MATCH_DATA,
+	__TCA_EM_IPT_MAX
+};
+
+#define TCA_EM_IPT_MAX (__TCA_EM_IPT_MAX - 1)
+
+#endif
net/sched/Kconfig +10 -0
index c03d86a..203e7f7 100644
@@ -658,6 +658,16 @@ config NET_EMATCH_IPSET
 	  To compile this code as a module, choose M here: the
 	  module will be called em_ipset.
 
+config NET_EMATCH_IPT
+	tristate "IPtables Matches"
+	depends on NET_EMATCH && NETFILTER && NETFILTER_XTABLES
+	---help---
+	  Say Y here to be able to classify packets based on iptables
+	  matches.
+
+	  To compile this code as a module, choose M here: the
+	  module will be called em_ipt.
+
 config NET_CLS_ACT
 	bool "Actions"
 	select NET_CLS
net/sched/Makefile +1 -0
index 5b63544..8811d38 100644
@@ -75,3 +75,4 @@ obj-$(CONFIG_NET_EMATCH_META)	+= em_meta.o
 obj-$(CONFIG_NET_EMATCH_TEXT)	+= em_text.o
 obj-$(CONFIG_NET_EMATCH_CANID)	+= em_canid.o
 obj-$(CONFIG_NET_EMATCH_IPSET)	+= em_ipset.o
+obj-$(CONFIG_NET_EMATCH_IPT)	+= em_ipt.o
net/sched/em_ipt.c new file +244 -0

244 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw

Re: [PATCH net-next,v2 2/2] net: sched: add em_ipt ematch for calling xtables matches

Quoted 5 lines · Eyal Birger
net/sched/em_ipt.c new file +0 -0
new file mode 100644
index 0000000..2103b30

[...]

net/sched/em_ipt.c line 156

Quoted 11 lines
net/sched/em_ipt.c new file +11 -0
+static int em_ipt_match(struct sk_buff *skb, struct tcf_ematch *em,
+			struct tcf_pkt_info *info)
+{
+	const struct em_ipt_match *im = (const void *)em->data;
+	struct xt_action_param acpar = {};
+	struct net_device *indev = NULL;
+	struct nf_hook_state state;
+	int ret;
+
+	if (unlikely(!skb_at_tc_ingress(skb))) {
+		pr_notice_once("ipt match must not be used at egress\n");

Isn't there a way to reject the use of this from ->change()? ie. from control plane configuration.

Quoted 2 lines
net/sched/em_ipt.c new file +2 -0
+		return 0;
+	}
#4 lore raw

Re: [PATCH net-next,v2 2/2] net: sched: add em_ipt ematch for calling xtables matches

Quoted 7 lines · Pablo Neira Ayuso
Quoted 5 lines · Eyal Birger
net/sched/em_ipt.c new file +0 -0
new file mode 100644
index 0000000..2103b30

[...]

Quoted 14 lines

net/sched/em_ipt.c line 156

Quoted 11 lines
net/sched/em_ipt.c new file +11 -0
+static int em_ipt_match(struct sk_buff *skb, struct tcf_ematch *em,
+                     struct tcf_pkt_info *info)
+{
+     const struct em_ipt_match *im = (const void *)em->data;
+     struct xt_action_param acpar = {};
+     struct net_device *indev = NULL;
+     struct nf_hook_state state;
+     int ret;
+
+     if (unlikely(!skb_at_tc_ingress(skb))) {
+             pr_notice_once("ipt match must not be used at egress\n");

Isn't there a way to reject the use of this from ->change()? ie. from control plane configuration.

I wasn't able to find a simple way of doing so:

- AFAIU tc filters are detached from the qdiscs they operate on via tcf_block instances
that may be shared by different qdiscs. I was not able to be sure that filters attached to ingress qdiscs via tcf_blocks at configuration time
cannot be later be shared
with non ingress qdiscs. Nor was I able to find another classifier
making the ingress/egress
distinction at configuration time.

- ematches are not provided with 'ingress/egress' information at 'change()' invocation, though
of course the infrastructure could be extended to provide this,
given the distinction is available.

Eyal.

#5 lore raw

Re: [PATCH net-next,v2 2/2] net: sched: add em_ipt ematch for calling xtables matches

Quoted 15 lines · Eyal Birger
Quoted 2 lines · Pablo Neira Ayuso

Isn't there a way to reject the use of this from ->change()? ie. from control plane configuration.

I wasn't able to find a simple way of doing so:

- AFAIU tc filters are detached from the qdiscs they operate on via tcf_block instances
that may be shared by different qdiscs. I was not able to be sure that filters attached to ingress qdiscs via tcf_blocks at configuration time
cannot be later be shared
with non ingress qdiscs. Nor was I able to find another classifier
making the ingress/egress
distinction at configuration time.

Quoted 5 lines

- ematches are not provided with 'ingress/egress' information at 'change()' invocation, though
of course the infrastructure could be extended to provide this,
given the distinction is available.

In the past you can check tp->q, but now we support shared tc filter block, so it is hard. I think your v1 is okay, which just silently passes the match on egress side. Or maybe we can just add a pr_info() unconditionally in em_ipt_change() saying only ingress is supported.

#6 lore raw

Re: [PATCH net-next,v2 2/2] net: sched: add em_ipt ematch for calling xtables matches

Quoted 24 lines · On Sun, 28 Jan 2018 19:22:12 -0800 Cong Wang <[email protected]> wrote:
Quoted 16 lines · Eyal Birger
Quoted 2 lines · Pablo Neira Ayuso

Isn't there a way to reject the use of this from ->change()? ie. from control plane configuration.

I wasn't able to find a simple way of doing so:

- AFAIU tc filters are detached from the qdiscs they operate on via tcf_block instances
that may be shared by different qdiscs. I was not able to be sure
that filters attached to ingress qdiscs via tcf_blocks at configuration time cannot be later be shared
with non ingress qdiscs. Nor was I able to find another classifier
making the ingress/egress
distinction at configuration time.

Quoted 5 lines

- ematches are not provided with 'ingress/egress' information at 'change()' invocation, though
of course the infrastructure could be extended to provide this,
given the distinction is available.

Quoted 4 lines

In the past you can check tp->q, but now we support shared tc filter block, so it is hard. I think your v1 is okay, which just silently passes the match on egress side. Or maybe we can just add a pr_info() unconditionally in em_ipt_change() saying only ingress is supported.

Thanks!

The motivation for allowing only ingress was to avoid skb modifications on egress as when running the match on egress, skb->data must point to the L3 header. Looking again at the calling flow e.g. from __dev_queue_xmit(), I don't see a case where skb may be shared.

Similarly on ingress flow, sch_handle_ingress() modifies the skb, and tc actions perform skb modification without share checking.

So as far as I can tell skb_pull() on the match is safe. Is there a different code path I should be looking for?

If that is the case, perhaps the v1 approach supporting both directions including skb_pull() can be resubmitted without the pr_notice once net-next is open.

Eyal.