Skip to contentSkip to conversation

tc: add ipt ematch

PATCH In review Eyal Birger 2 participants 4 messages Feb 23, 2018 – Feb 27, 2018

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

cover tc: add ipt ematch

This patchset extends tc to support the ipt ematch.

The first patch adds the ability for ematch cmdline parsers to receive argc,argv parameters.
The second patch adds the em_ipt module.

Eyal Birger (2):
tc: ematch: add parse_eopt_argv() method for providing ematches with
argv parameters
tc: add em_ipt ematch for calling xtables matches from tc matching
context

 etc/iproute2/ematch_map |   1 +
 man/man8/tc-ematch.8    |  15 ++++
 tc/Makefile             |   7 ++
 tc/em_ipt.c             | 208 ++++++++++++++++++++++++++++++++++++++++++++++++
 tc/m_ematch.c           |  27 ++++++-
 tc/m_ematch.h           |   2 +
 6 files changed, 259 insertions(+), 1 deletion(-)
 create mode 100644 tc/em_ipt.c
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/2 tc: ematch: add parse_eopt_argv() method for providing ematches with argv parameters

ematche uses YACC to parse ematch arguments and places them in struct bstr linked lists.

It is useful to be able to receive parameters as argc,argv in order to use getopt (and alike) argument parsers.

---
 tc/m_ematch.c | 27 ++++++++++++++++++++++++++-
 tc/m_ematch.h |  2 ++
 2 files changed, 28 insertions(+), 1 deletion(-)
tc/m_ematch.c +26 -1
index 7dbc2e7..61f8fb3 100644
@@ -169,6 +169,31 @@ static struct ematch_util *get_ematch_kind_num(__u16 kind)
 	return get_ematch_kind(name);
 }
 
+static int em_parse_call(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
+			 struct ematch_util *e, struct ematch *t)
+{
+	if (e->parse_eopt_argv) {
+		int argc = 0, i = 0, ret;
+		struct bstr *args;
+		char **argv;
+
+		for (args = t->args; args; args = bstr_next(args))
+			argc++;
+		argv = calloc(argc, sizeof(char *));
+		if (!argv)
+			return -1;
+		for (args = t->args; args; args = bstr_next(args))
+			argv[i++] = args->data;
+
+		ret = e->parse_eopt_argv(n, hdr, argc, argv);
+
+		free(argv);
+		return ret;
+	}
+
+	return e->parse_eopt(n, hdr, t->args->next);
+}
+
 static int parse_tree(struct nlmsghdr *n, struct ematch *tree)
 {
 	int index = 1;
@@ -212,7 +237,7 @@ static int parse_tree(struct nlmsghdr *n, struct ematch *tree)
 			}
 
 			hdr.kind = num;
-			if (e->parse_eopt(n, &hdr, t->args->next) < 0)
+			if (em_parse_call(n, &hdr, e, t) < 0)
 				return -1;
 		}
 
tc/m_ematch.h +2 -0
index fa6e214..f634f19 100644
@@ -88,6 +88,8 @@ struct ematch_util
 	int			kind_num;
 	int	(*parse_eopt)(struct nlmsghdr *,struct tcf_ematch_hdr *,
 			      struct bstr *);
+	int	(*parse_eopt_argv)(struct nlmsghdr *, struct tcf_ematch_hdr *,
+				   int, char **);
 	int	(*print_eopt)(FILE *, struct tcf_ematch_hdr *, void *, int);
 	void	(*print_usage)(FILE *);
 	struct ematch_util	*next;
Signature · 1 line
2.7.4
#2 lore raw

PATCH 2/2 tc: add em_ipt ematch for calling xtables matches from tc matching context

In reply to Eyal Birger

The commit calls a new tc ematch for using netfilter xtable matches.

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

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

The matcher uses libxtables for parsing the input parameters.

Example use for matching an IPSec state with reqid 1:

tc qdisc add dev eth0 ingress
tc filter add dev eth0 protocol ip parent ffff: \

    basic match 'ipt(-m policy --dir in --pol ipsec --reqid 1)' \
    action drop

This is the user-space counter part of kernel commit ccc007e4a746 ("net: sched: add em_ipt ematch for calling xtables matches")

---
 etc/iproute2/ematch_map |   1 +
 man/man8/tc-ematch.8    |  15 ++++
 tc/Makefile             |   7 ++
 tc/em_ipt.c             | 208 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 231 insertions(+)
 create mode 100644 tc/em_ipt.c
etc/iproute2/ematch_map +1 -0
index 1823983..4d6bb2f 100644
@@ -5,3 +5,4 @@
 4	meta
 7	canid
 8	ipset
+9	ipt
man/man8/tc-ematch.8 +15 -0
index b9bf70c..042f840 100644
@@ -98,6 +98,17 @@ When using the ipset ematch with the "ip_set_hash:net,iface" set type,
 the interface can be queried using "src,dst (source ip address, outgoing interface) or
 "src,src" (source ip address, incoming interface) syntax.
 
+.SS ipt
+test packet against xtables matches
+
+.IR ipt "( " [-6] " "-m " " MATCH_NAME " " FLAGS " )
+
+.IR MATCH_NAME " := " string
+
+.IR FLAGS " := { " FLAG " [, " FLAGS "] }
+
+The flag options are the same as those used by the xtable match used.
+
 .SH CAVEATS
 
 The ematch syntax uses '(' and ')' to group expressions. All braces need to be
@@ -127,6 +138,10 @@ Check if packet source ip and the interface the packet arrived on is member of "
 
 # 'ipset(interactive src,src)'
 
+Check if packet matches an IPSec state with reqid 1:
+
+# 'ipt(-m policy --dir in --pol ipsec --reqid 1)'
+
 .SH "AUTHOR"
 
 The extended match infrastructure was added by Thomas Graf.
tc/Makefile +7 -0
index 3716dd6..dfd0026 100644
@@ -80,6 +80,7 @@ endif
 ifneq ($(TC_CONFIG_NO_XT),y)
   ifeq ($(TC_CONFIG_XT),y)
     TCSO += m_xt.so
+    TCMODULES += em_ipt.o
     ifeq ($(TC_CONFIG_IPSET),y)
       TCMODULES += em_ipset.o
     endif
@@ -163,6 +164,12 @@ m_xt_old.so: m_xt_old.c
 
 em_ipset.o: CFLAGS += $$($(PKG_CONFIG) xtables --cflags)
 
+em_ipt.o: CFLAGS += $$($(PKG_CONFIG) xtables --cflags)
+
+ifeq ($(TC_CONFIG_XT),y)
+  LDFLAGS += $$($(PKG_CONFIG) xtables --libs)
+endif
+
 %.yacc.c: %.y
 	$(QUIET_YACC)$(YACC) $(YACCFLAGS) -o $@ $<
 
tc/em_ipt.c new file +208 -0

208 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw
In reply to Eyal Birger
Quoted 12 lines · Eyal Birger

This patchset extends tc to support the ipt ematch.

The first patch adds the ability for ematch cmdline parsers to receive argc,argv parameters.
The second patch adds the em_ipt module.

Eyal Birger (2):
tc: ematch: add parse_eopt_argv() method for providing ematches with
argv parameters
tc: add em_ipt ematch for calling xtables matches from tc matching
context

Quoted 9 lines
 etc/iproute2/ematch_map |   1 +
 man/man8/tc-ematch.8    |  15 ++++
 tc/Makefile             |   7 ++
 tc/em_ipt.c             | 208 ++++++++++++++++++++++++++++++++++++++++++++++++
 tc/m_ematch.c           |  27 ++++++-
 tc/m_ematch.h           |   2 +
 6 files changed, 259 insertions(+), 1 deletion(-)
 create mode 100644 tc/em_ipt.c

series applied to iproute2-next.