36cfb7
From 26ab66d7c43c3ef60ab058d4c3da8989a5c1dd46 Mon Sep 17 00:00:00 2001
36cfb7
From: Kamal Heib <kheib@redhat.com>
36cfb7
Date: Thu, 9 Nov 2017 04:44:32 -0500
36cfb7
Subject: [PATCH] pedit: Introduce ipv6 support
36cfb7
36cfb7
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1456539
36cfb7
36cfb7
commit f3e1b2448a95baef587965b08f48d49b6e1ec2cb
36cfb7
Author: Amir Vadai <amir@vadai.me>
36cfb7
Date:   Sun May 14 11:17:46 2017 +0300
36cfb7
36cfb7
    pedit: Introduce ipv6 support
36cfb7
36cfb7
    Add support for modifying IPv6 headers using pedit.
36cfb7
36cfb7
    Signed-off-by: Amir Vadai <amir@vadai.me>
36cfb7
36cfb7
Signed-off-by: Kamal Heib <kheib@redhat.com>
36cfb7
---
e138d9
 man/man8/tc-pedit.8 | 30 +++++++++++++++
36cfb7
 tc/Makefile         |  1 +
e138d9
 tc/m_pedit.c        | 43 ++++++++++++++++++++-
e138d9
 tc/p_ip.c           | 17 +--------
e138d9
 tc/p_ip6.c          | 91 +++++++++++++++++++++++++++++++++++++++++++++
36cfb7
 5 files changed, 164 insertions(+), 18 deletions(-)
36cfb7
 create mode 100644 tc/p_ip6.c
36cfb7
36cfb7
diff --git a/man/man8/tc-pedit.8 b/man/man8/tc-pedit.8
e138d9
index 9c4d57b972cc8..82d4217bc9589 100644
36cfb7
--- a/man/man8/tc-pedit.8
36cfb7
+++ b/man/man8/tc-pedit.8
36cfb7
@@ -33,6 +33,8 @@ pedit - generic packet editor action
36cfb7
 |
36cfb7
 .BI ip " EX_IPHDR_FIELD"
36cfb7
 |
36cfb7
+.BI ip6 " IP6HDR_FIELD"
36cfb7
+|
36cfb7
 .BI tcp " TCPHDR_FIELD"
36cfb7
 |
36cfb7
 .BI udp " UDPHDR_FIELD"
36cfb7
@@ -55,6 +57,12 @@ pedit - generic packet editor action
36cfb7
 .IR EX_IPHDR_FIELD " := { "
36cfb7
 .BR ttl " }"
36cfb7
 
36cfb7
+
36cfb7
+.ti -8
36cfb7
+.IR IP6HDR_FIELD " := { "
36cfb7
+.BR src " | " dst " | " flow_lbl " | " payload_len " | " nexthdr " |"
36cfb7
+.BR hoplimit " }"
36cfb7
+
36cfb7
 .ti -8
36cfb7
 .IR TCPHDR_FIELD " := { "
36cfb7
 .BR sport " | " dport " | " flags " }"
36cfb7
@@ -211,6 +219,25 @@ are:
36cfb7
 .B ttl
36cfb7
 .RE
36cfb7
 .TP
36cfb7
+.BI ip6 " IP6HDR_FIELD"
36cfb7
+The supported keywords for
36cfb7
+.I IP6HDR_FIELD
36cfb7
+are:
36cfb7
+.RS
36cfb7
+.TP
36cfb7
+.B src
36cfb7
+.TQ
36cfb7
+.B dst
36cfb7
+.TQ
36cfb7
+.B flow_lbl
36cfb7
+.TQ
36cfb7
+.B payload_len
36cfb7
+.TQ
36cfb7
+.B nexthdr
36cfb7
+.TQ
36cfb7
+.B hoplimit
36cfb7
+.RE
36cfb7
+.TP
36cfb7
 .BI tcp " TCPHDR_FIELD"
36cfb7
 The supported keywords for
36cfb7
 .I TCPHDR_FIELD
e138d9
@@ -329,6 +356,9 @@ tc filter add dev eth0 parent ffff: u32 \\
36cfb7
 tc filter add dev eth0 parent ffff: u32 \\
36cfb7
 	match ip sport 22 0xffff \\
e138d9
 	action pedit ex munge ip dst set 192.168.1.199
36cfb7
+tc filter add dev eth0 parent ffff: u32 \\
36cfb7
+	match ip sport 22 0xffff \\
e138d9
+	action pedit ex munge ip6 dst set fe80::dacb:8aff:fec7:320e
36cfb7
 tc filter add dev eth0 parent ffff: u32 \\
e138d9
 	match ip sport 22 0xffff \\
e138d9
 	action pedit ex munge eth dst set 11:22:33:44:55:66
36cfb7
diff --git a/tc/Makefile b/tc/Makefile
e138d9
index 446a11391ad70..9a6bb1ddea57e 100644
36cfb7
--- a/tc/Makefile
36cfb7
+++ b/tc/Makefile
36cfb7
@@ -53,6 +53,7 @@ TCMODULES += m_bpf.o
36cfb7
 TCMODULES += m_tunnel_key.o
36cfb7
 TCMODULES += m_sample.o
36cfb7
 TCMODULES += p_ip.o
36cfb7
+TCMODULES += p_ip6.o
36cfb7
 TCMODULES += p_icmp.o
36cfb7
 TCMODULES += p_eth.o
36cfb7
 TCMODULES += p_tcp.o
36cfb7
diff --git a/tc/m_pedit.c b/tc/m_pedit.c
e138d9
index 9b74c965932e0..dfa6b2c4835e9 100644
36cfb7
--- a/tc/m_pedit.c
36cfb7
+++ b/tc/m_pedit.c
36cfb7
@@ -257,6 +257,32 @@ static int pack_mac(struct m_pedit_sel *sel, struct m_pedit_key *tkey,
36cfb7
 	return ret;
36cfb7
 }
36cfb7
 
36cfb7
+static int pack_ipv6(struct m_pedit_sel *sel, struct m_pedit_key *tkey,
36cfb7
+		     __u32 *ipv6)
36cfb7
+{
36cfb7
+	int ret = 0;
36cfb7
+	int i;
36cfb7
+
36cfb7
+	if (tkey->off & 0x3) {
36cfb7
+		fprintf(stderr,
36cfb7
+			"pack_ipv6: IPv6 offsets must begin in 32bit boundaries\n");
36cfb7
+		return -1;
36cfb7
+	}
36cfb7
+
36cfb7
+	for (i = 0; i < 4; i++) {
36cfb7
+		tkey->mask = 0;
36cfb7
+		tkey->val = ntohl(ipv6[i]);
36cfb7
+
36cfb7
+		ret = pack_key32(~0, sel, tkey);
36cfb7
+		if (ret)
36cfb7
+			return ret;
36cfb7
+
36cfb7
+		tkey->off += 4;
36cfb7
+	}
36cfb7
+
36cfb7
+	return 0;
36cfb7
+}
36cfb7
+
36cfb7
 int parse_val(int *argc_p, char ***argv_p, __u32 *val, int type)
36cfb7
 {
36cfb7
 	int argc = *argc_p;
36cfb7
@@ -281,8 +307,16 @@ int parse_val(int *argc_p, char ***argv_p, __u32 *val, int type)
36cfb7
 		return 0;
36cfb7
 	}
36cfb7
 
36cfb7
-	if (type == TIPV6)
36cfb7
-		return -1; /* not implemented yet */
36cfb7
+	if (type == TIPV6) {
36cfb7
+		inet_prefix addr;
36cfb7
+
36cfb7
+		if (get_prefix_1(&addr, *argv, AF_INET6))
36cfb7
+			return -1;
36cfb7
+
36cfb7
+		memcpy(val, addr.data, addr.bytelen);
36cfb7
+
36cfb7
+		return 0;
36cfb7
+	}
36cfb7
 
36cfb7
 	if (type == TMAC) {
36cfb7
 #define MAC_ALEN 6
36cfb7
@@ -364,6 +398,11 @@ int parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain,
36cfb7
 		goto done;
36cfb7
 	}
36cfb7
 
36cfb7
+	if (type == TIPV6) {
36cfb7
+		res = pack_ipv6(sel, tkey, val);
36cfb7
+		goto done;
36cfb7
+	}
36cfb7
+
36cfb7
 	tkey->val = *v;
36cfb7
 	tkey->mask = *m;
36cfb7
 
36cfb7
diff --git a/tc/p_ip.c b/tc/p_ip.c
e138d9
index 22fe6505e4271..0272a6eaaf48b 100644
36cfb7
--- a/tc/p_ip.c
36cfb7
+++ b/tc/p_ip.c
36cfb7
@@ -1,5 +1,5 @@
36cfb7
 /*
36cfb7
- * m_pedit.c		packet editor: IPV4/6 header
36cfb7
+ * p_ip.c		packet editor: IPV4 header
36cfb7
  *
36cfb7
  *		This program is free software; you can distribute it and/or
36cfb7
  *		modify it under the terms of the GNU General Public License
36cfb7
@@ -156,23 +156,8 @@ done:
36cfb7
 	return res;
36cfb7
 }
36cfb7
 
36cfb7
-static int
36cfb7
-parse_ip6(int *argc_p, char ***argv_p,
36cfb7
-	  struct m_pedit_sel *sel, struct m_pedit_key *tkey)
36cfb7
-{
36cfb7
-	int res = -1;
36cfb7
-	return res;
36cfb7
-}
36cfb7
-
36cfb7
 struct m_pedit_util p_pedit_ip = {
36cfb7
 	NULL,
36cfb7
 	"ip",
36cfb7
 	parse_ip,
36cfb7
 };
36cfb7
-
36cfb7
-
36cfb7
-struct m_pedit_util p_pedit_ip6 = {
36cfb7
-	NULL,
36cfb7
-	"ip6",
36cfb7
-	parse_ip6,
36cfb7
-};
36cfb7
diff --git a/tc/p_ip6.c b/tc/p_ip6.c
36cfb7
new file mode 100644
e138d9
index 0000000000000..a4824bda90e81
36cfb7
--- /dev/null
36cfb7
+++ b/tc/p_ip6.c
36cfb7
@@ -0,0 +1,91 @@
36cfb7
+/*
36cfb7
+ * p_ip6.c		packet editor: IPV6 header
36cfb7
+ *
36cfb7
+ *		This program is free software; you can distribute it and/or
36cfb7
+ *		modify it under the terms of the GNU General Public License
36cfb7
+ *		as published by the Free Software Foundation; either version
36cfb7
+ *		2 of the License, or (at your option) any later version.
36cfb7
+ *
36cfb7
+ * Authors:  Amir Vadai <amir@vadai.me>
36cfb7
+ *
36cfb7
+ */
36cfb7
+
36cfb7
+#include <stdio.h>
36cfb7
+#include <stdlib.h>
36cfb7
+#include <unistd.h>
36cfb7
+#include <syslog.h>
36cfb7
+#include <fcntl.h>
36cfb7
+#include <sys/socket.h>
36cfb7
+#include <netinet/in.h>
36cfb7
+#include <arpa/inet.h>
36cfb7
+#include <string.h>
36cfb7
+#include "utils.h"
36cfb7
+#include "tc_util.h"
36cfb7
+#include "m_pedit.h"
36cfb7
+
36cfb7
+static int
36cfb7
+parse_ip6(int *argc_p, char ***argv_p,
36cfb7
+	  struct m_pedit_sel *sel, struct m_pedit_key *tkey)
36cfb7
+{
36cfb7
+	int res = -1;
36cfb7
+	int argc = *argc_p;
36cfb7
+	char **argv = *argv_p;
36cfb7
+
36cfb7
+	if (argc < 2)
36cfb7
+		return -1;
36cfb7
+
36cfb7
+	if (!sel->extended)
36cfb7
+		return -1;
36cfb7
+
36cfb7
+	tkey->htype = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6;
36cfb7
+
36cfb7
+	if (strcmp(*argv, "src") == 0) {
36cfb7
+		NEXT_ARG();
36cfb7
+		tkey->off = 8;
36cfb7
+		res = parse_cmd(&argc, &argv, 16, TIPV6, RU32, sel, tkey);
36cfb7
+		goto done;
36cfb7
+	}
36cfb7
+	if (strcmp(*argv, "dst") == 0) {
36cfb7
+		NEXT_ARG();
36cfb7
+		tkey->off = 24;
36cfb7
+		res = parse_cmd(&argc, &argv, 16, TIPV6, RU32, sel, tkey);
36cfb7
+		goto done;
36cfb7
+	}
36cfb7
+	if (strcmp(*argv, "flow_lbl") == 0) {
36cfb7
+		NEXT_ARG();
36cfb7
+		tkey->off = 0;
36cfb7
+		res = parse_cmd(&argc, &argv, 4, TU32, 0x0007ffff, sel, tkey);
36cfb7
+		goto done;
36cfb7
+	}
36cfb7
+	if (strcmp(*argv, "payload_len") == 0) {
36cfb7
+		NEXT_ARG();
36cfb7
+		tkey->off = 4;
36cfb7
+		res = parse_cmd(&argc, &argv, 2, TU32, RU16, sel, tkey);
36cfb7
+		goto done;
36cfb7
+	}
36cfb7
+	if (strcmp(*argv, "nexthdr") == 0) {
36cfb7
+		NEXT_ARG();
36cfb7
+		tkey->off = 6;
36cfb7
+		res = parse_cmd(&argc, &argv, 1, TU32, RU8, sel, tkey);
36cfb7
+		goto done;
36cfb7
+	}
36cfb7
+	if (strcmp(*argv, "hoplimit") == 0) {
36cfb7
+		NEXT_ARG();
36cfb7
+		tkey->off = 7;
36cfb7
+		res = parse_cmd(&argc, &argv, 1, TU32, RU8, sel, tkey);
36cfb7
+		goto done;
36cfb7
+	}
36cfb7
+
36cfb7
+	return -1;
36cfb7
+
36cfb7
+done:
36cfb7
+	*argc_p = argc;
36cfb7
+	*argv_p = argv;
36cfb7
+	return res;
36cfb7
+}
36cfb7
+
36cfb7
+struct m_pedit_util p_pedit_ip6 = {
36cfb7
+	NULL,
36cfb7
+	"ipv6",
36cfb7
+	parse_ip6,
36cfb7
+};
36cfb7
-- 
e138d9
2.21.0
36cfb7