net: sched: introduce em_ipt ematch
Subsystems (4)
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
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 2/2 net: sched: add em_ipt ematch for calling xtables matches
In reply to Eyal BirgerFrom: 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
@@ -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 9enum {TCF_EM_PROG_TC
include/uapi/linux/tc_ematch/tc_em_ipt.h new file +19 -0
@@ -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
@@ -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
@@ -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
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
Signed-off-by Eyal Birger <[email protected]>---
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.
David Miller
maintainerRe: [PATCH net-next 2/2] net: sched: add em_ipt ematch for calling xtables matches
In reply to Eyal BirgerFrom: 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.
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 +0200Quoted 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.
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 +0200Quoted 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->datapoints to the network header, which is what the xtables matches expect, so these are actually noops, therefore,skb_pull()andskb_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.