Skip to contentSkip to conversation

net: sched: introduce em_ipt ematch

PATCH Superseded Eyal Birger 3 participants 7 messages Jan 23, 2018 – Jan 26, 2018

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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/

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 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 2f8f16a..929b117 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 df3110d..1cd5983 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 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).

---

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
Acked-by on v1 2/2
#3 lore raw

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

Quoted 8 lines · 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).

Quoted 8 lines

---

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.

Thanks! No objection to the Netfilter bits.

So I leave it up to the tc maintainers to decide what to do with this.

maintainer
#4 lore raw

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

In reply to Eyal Birger

From: Eyal Birger <[email protected]>
Date: Tue, 23 Jan 2018 11:17:32 +0200

Quoted 10 lines
net/sched/em_ipt.c new file +10 -0
+	network_offset = skb_network_offset(skb);
+	skb_pull(skb, network_offset);
+
+	rcu_read_lock();
+
+	if (skb->skb_iif)
+		indev = dev_get_by_index_rcu(em->net, skb->skb_iif);
+
+	nf_hook_state_init(&state, im->hook, im->nfproto, indev ?: skb->dev,
+			   skb->dev, NULL, em->net, NULL);

net/sched/em_ipt.c line 195

Quoted 10 lines
net/sched/em_ipt.c new file +10 -0
+
+	acpar.match = im->match;
+	acpar.matchinfo = im->match_data;
+	acpar.state = &state;
+
+	ret = im->match->match(skb, &acpar);
+
+	rcu_read_unlock();
+
+	skb_push(skb, network_offset);

If the SKB is shared in any way, this pull/push around the NF hook invocation is illegal.

#5 lore raw

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

Quoted 18 lines · David Miller

From: Eyal Birger <[email protected]>
Date: Tue, 23 Jan 2018 11:17:32 +0200

Quoted 15 lines
net/sched/em_ipt.c new file +15 -0
+	network_offset = skb_network_offset(skb);
+	skb_pull(skb, network_offset);
+
+	rcu_read_lock();
+
+	if (skb->skb_iif)
+		indev = dev_get_by_index_rcu(em->net, skb->skb_iif);
+
+	nf_hook_state_init(&state, im->hook, im->nfproto, indev ?: skb->dev,
+			   skb->dev, NULL, em->net, NULL);
+
+	acpar.match = im->match;
+	acpar.matchinfo = im->match_data;
+	acpar.state = &state;
+
Quoted 8 lines

net/sched/em_ipt.c line 195

Quoted 5 lines
net/sched/em_ipt.c new file +5 -0
+	ret = im->match->match(skb, &acpar);
+
+	rcu_read_unlock();
+
+	skb_push(skb, network_offset);

If the SKB is shared in any way, this pull/push around the NF hook invocation is illegal.

At ingress, skb->data points to the network header, which is what the xtables matches expect, so these are actually noops, therefore, skb_pull() and skb_push() can be removed.

#6 lore raw

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

Quoted 28 lines · Pablo Neira Ayuso
Quoted 18 lines · David Miller

From: Eyal Birger <[email protected]>
Date: Tue, 23 Jan 2018 11:17:32 +0200

Quoted 15 lines
net/sched/em_ipt.c new file +15 -0
+   network_offset = skb_network_offset(skb);
+   skb_pull(skb, network_offset);
+
+   rcu_read_lock();
+
+   if (skb->skb_iif)
+           indev = dev_get_by_index_rcu(em->net, skb->skb_iif);
+
+   nf_hook_state_init(&state, im->hook, im->nfproto, indev ?: skb->dev,
+                      skb->dev, NULL, em->net, NULL);
+
+   acpar.match = im->match;
+   acpar.matchinfo = im->match_data;
+   acpar.state = &state;
+
Quoted 8 lines

net/sched/em_ipt.c line 195

Quoted 5 lines
net/sched/em_ipt.c new file +5 -0
+   ret = im->match->match(skb, &acpar);
+
+   rcu_read_unlock();
+
+   skb_push(skb, network_offset);

If the SKB is shared in any way, this pull/push around the NF hook invocation is illegal.

Quoted 3 lines

At ingress, skb->data points to the network header, which is what the xtables matches expect, so these are actually noops, therefore, skb_pull() and skb_push() can be removed.

Right. I added those for completeness in supporting the xmit path. In the xt_policy use-case it is irrelevant as tc is invoked after encapsulation.

I will submit a v2 without these, asserting the ingress direction.

Note I have followed the example in em_ipset.c, so that might be a problem there too...

Thanks!
Eyal.