Blame SOURCES/0035-tc-Introduce-tc-ct-action.patch

07a51b
From ef66b6a546f3f1fd517cfa306cc347ad096bd932 Mon Sep 17 00:00:00 2001
07a51b
From: Andrea Claudi <aclaudi@redhat.com>
07a51b
Date: Tue, 9 Jun 2020 15:45:56 +0200
07a51b
Subject: [PATCH] tc: Introduce tc ct action
07a51b
07a51b
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1844637
07a51b
Upstream Status: iproute2.git commit c8a494314c400
07a51b
07a51b
commit c8a494314c400eb023d7555933ba8ab40345519b
07a51b
Author: Paul Blakey <paulb@mellanox.com>
07a51b
Date:   Thu Jul 11 11:14:26 2019 +0300
07a51b
07a51b
    tc: Introduce tc ct action
07a51b
07a51b
    New tc action to send packets to conntrack module, commit
07a51b
    them, and set a zone, labels, mark, and nat on the connection.
07a51b
07a51b
    It can also clear the packet's conntrack state by using clear.
07a51b
07a51b
    Usage:
07a51b
       ct clear
07a51b
       ct commit [force] [zone] [mark] [label] [nat]
07a51b
       ct [nat] [zone]
07a51b
07a51b
    Signed-off-by: Paul Blakey <paulb@mellanox.com>
07a51b
    Signed-off-by: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
07a51b
    Signed-off-by: Yossi Kuperman <yossiku@mellanox.com>
07a51b
    Acked-by: Jiri Pirko <jiri@mellanox.com>
07a51b
    Acked-by: Roi Dayan <roid@mellanox.com>
07a51b
    Signed-off-by: David Ahern <dsahern@gmail.com>
07a51b
---
07a51b
 tc/Makefile  |   1 +
07a51b
 tc/m_ct.c    | 497 +++++++++++++++++++++++++++++++++++++++++++++++++++
07a51b
 tc/tc_util.c |  44 +++++
07a51b
 tc/tc_util.h |   4 +
07a51b
 4 files changed, 546 insertions(+)
07a51b
 create mode 100644 tc/m_ct.c
07a51b
07a51b
diff --git a/tc/Makefile b/tc/Makefile
07a51b
index 09ff3692b1663..14171a28cba5d 100644
07a51b
--- a/tc/Makefile
07a51b
+++ b/tc/Makefile
07a51b
@@ -53,6 +53,7 @@ TCMODULES += m_ctinfo.o
07a51b
 TCMODULES += m_bpf.o
07a51b
 TCMODULES += m_tunnel_key.o
07a51b
 TCMODULES += m_sample.o
07a51b
+TCMODULES += m_ct.o
07a51b
 TCMODULES += p_ip.o
07a51b
 TCMODULES += p_ip6.o
07a51b
 TCMODULES += p_icmp.o
07a51b
diff --git a/tc/m_ct.c b/tc/m_ct.c
07a51b
new file mode 100644
07a51b
index 0000000000000..8589cb9a3c515
07a51b
--- /dev/null
07a51b
+++ b/tc/m_ct.c
07a51b
@@ -0,0 +1,497 @@
07a51b
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
07a51b
+/* -
07a51b
+ * m_ct.c     Connection tracking action
07a51b
+ *
07a51b
+ * Authors:   Paul Blakey <paulb@mellanox.com>
07a51b
+ *            Yossi Kuperman <yossiku@mellanox.com>
07a51b
+ *            Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
07a51b
+ */
07a51b
+
07a51b
+#include <stdio.h>
07a51b
+#include <stdlib.h>
07a51b
+#include <unistd.h>
07a51b
+#include <string.h>
07a51b
+#include "utils.h"
07a51b
+#include "tc_util.h"
07a51b
+#include <linux/tc_act/tc_ct.h>
07a51b
+
07a51b
+static void
07a51b
+usage(void)
07a51b
+{
07a51b
+	fprintf(stderr,
07a51b
+		"Usage: ct clear\n"
07a51b
+		"	ct commit [force] [zone ZONE] [mark MASKED_MARK] [label MASKED_LABEL] [nat NAT_SPEC]\n"
07a51b
+		"	ct [nat] [zone ZONE]\n"
07a51b
+		"Where: ZONE is the conntrack zone table number\n"
07a51b
+		"	NAT_SPEC is {src|dst} addr addr1[-addr2] [port port1[-port2]]\n"
07a51b
+		"\n");
07a51b
+	exit(-1);
07a51b
+}
07a51b
+
07a51b
+static int ct_parse_nat_addr_range(const char *str, struct nlmsghdr *n)
07a51b
+{
07a51b
+	inet_prefix addr = { .family = AF_UNSPEC, };
07a51b
+	char *addr1, *addr2 = 0;
07a51b
+	SPRINT_BUF(buffer);
07a51b
+	int attr;
07a51b
+	int ret;
07a51b
+
07a51b
+	strncpy(buffer, str, sizeof(buffer) - 1);
07a51b
+
07a51b
+	addr1 = buffer;
07a51b
+	addr2 = strchr(addr1, '-');
07a51b
+	if (addr2) {
07a51b
+		*addr2 = '\0';
07a51b
+		addr2++;
07a51b
+	}
07a51b
+
07a51b
+	ret = get_addr(&addr, addr1, AF_UNSPEC);
07a51b
+	if (ret)
07a51b
+		return ret;
07a51b
+	attr = addr.family == AF_INET ? TCA_CT_NAT_IPV4_MIN :
07a51b
+					TCA_CT_NAT_IPV6_MIN;
07a51b
+	addattr_l(n, MAX_MSG, attr, addr.data, addr.bytelen);
07a51b
+
07a51b
+	if (addr2) {
07a51b
+		ret = get_addr(&addr, addr2, addr.family);
07a51b
+		if (ret)
07a51b
+			return ret;
07a51b
+	}
07a51b
+	attr = addr.family == AF_INET ? TCA_CT_NAT_IPV4_MAX :
07a51b
+					TCA_CT_NAT_IPV6_MAX;
07a51b
+	addattr_l(n, MAX_MSG, attr, addr.data, addr.bytelen);
07a51b
+
07a51b
+	return 0;
07a51b
+}
07a51b
+
07a51b
+static int ct_parse_nat_port_range(const char *str, struct nlmsghdr *n)
07a51b
+{
07a51b
+	char *port1, *port2 = 0;
07a51b
+	SPRINT_BUF(buffer);
07a51b
+	__be16 port;
07a51b
+	int ret;
07a51b
+
07a51b
+	strncpy(buffer, str, sizeof(buffer) - 1);
07a51b
+
07a51b
+	port1 = buffer;
07a51b
+	port2 = strchr(port1, '-');
07a51b
+	if (port2) {
07a51b
+		*port2 = '\0';
07a51b
+		port2++;
07a51b
+	}
07a51b
+
07a51b
+	ret = get_be16(&port, port1, 10);
07a51b
+	if (ret)
07a51b
+		return -1;
07a51b
+	addattr16(n, MAX_MSG, TCA_CT_NAT_PORT_MIN, port);
07a51b
+
07a51b
+	if (port2) {
07a51b
+		ret = get_be16(&port, port2, 10);
07a51b
+		if (ret)
07a51b
+			return -1;
07a51b
+	}
07a51b
+	addattr16(n, MAX_MSG, TCA_CT_NAT_PORT_MAX, port);
07a51b
+
07a51b
+	return 0;
07a51b
+}
07a51b
+
07a51b
+
07a51b
+static int ct_parse_u16(char *str, int value_type, int mask_type,
07a51b
+			struct nlmsghdr *n)
07a51b
+{
07a51b
+	__u16 value, mask;
07a51b
+	char *slash = 0;
07a51b
+
07a51b
+	if (mask_type != TCA_CT_UNSPEC) {
07a51b
+		slash = strchr(str, '/');
07a51b
+		if (slash)
07a51b
+			*slash = '\0';
07a51b
+	}
07a51b
+
07a51b
+	if (get_u16(&value, str, 0))
07a51b
+		return -1;
07a51b
+
07a51b
+	if (slash) {
07a51b
+		if (get_u16(&mask, slash + 1, 0))
07a51b
+			return -1;
07a51b
+	} else {
07a51b
+		mask = UINT16_MAX;
07a51b
+	}
07a51b
+
07a51b
+	addattr16(n, MAX_MSG, value_type, value);
07a51b
+	if (mask_type != TCA_CT_UNSPEC)
07a51b
+		addattr16(n, MAX_MSG, mask_type, mask);
07a51b
+
07a51b
+	return 0;
07a51b
+}
07a51b
+
07a51b
+static int ct_parse_u32(char *str, int value_type, int mask_type,
07a51b
+			struct nlmsghdr *n)
07a51b
+{
07a51b
+	__u32 value, mask;
07a51b
+	char *slash;
07a51b
+
07a51b
+	slash = strchr(str, '/');
07a51b
+	if (slash)
07a51b
+		*slash = '\0';
07a51b
+
07a51b
+	if (get_u32(&value, str, 0))
07a51b
+		return -1;
07a51b
+
07a51b
+	if (slash) {
07a51b
+		if (get_u32(&mask, slash + 1, 0))
07a51b
+			return -1;
07a51b
+	} else {
07a51b
+		mask = UINT32_MAX;
07a51b
+	}
07a51b
+
07a51b
+	addattr32(n, MAX_MSG, value_type, value);
07a51b
+	addattr32(n, MAX_MSG, mask_type, mask);
07a51b
+
07a51b
+	return 0;
07a51b
+}
07a51b
+
07a51b
+static int ct_parse_mark(char *str, struct nlmsghdr *n)
07a51b
+{
07a51b
+	return ct_parse_u32(str, TCA_CT_MARK, TCA_CT_MARK_MASK, n);
07a51b
+}
07a51b
+
07a51b
+static int ct_parse_labels(char *str, struct nlmsghdr *n)
07a51b
+{
07a51b
+#define LABELS_SIZE	16
07a51b
+	uint8_t labels[LABELS_SIZE], lmask[LABELS_SIZE];
07a51b
+	char *slash, *mask = NULL;
07a51b
+	size_t slen, slen_mask = 0;
07a51b
+
07a51b
+	slash = index(str, '/');
07a51b
+	if (slash) {
07a51b
+		*slash = 0;
07a51b
+		mask = slash+1;
07a51b
+		slen_mask = strlen(mask);
07a51b
+	}
07a51b
+
07a51b
+	slen = strlen(str);
07a51b
+	if (slen > LABELS_SIZE*2 || slen_mask > LABELS_SIZE*2) {
07a51b
+		char errmsg[128];
07a51b
+
07a51b
+		snprintf(errmsg, sizeof(errmsg),
07a51b
+				"%zd Max allowed size %d",
07a51b
+				slen, LABELS_SIZE*2);
07a51b
+		invarg(errmsg, str);
07a51b
+	}
07a51b
+
07a51b
+	if (hex2mem(str, labels, slen/2) < 0)
07a51b
+		invarg("ct: labels must be a hex string\n", str);
07a51b
+	addattr_l(n, MAX_MSG, TCA_CT_LABELS, labels, slen/2);
07a51b
+
07a51b
+	if (mask) {
07a51b
+		if (hex2mem(mask, lmask, slen_mask/2) < 0)
07a51b
+			invarg("ct: labels mask must be a hex string\n", mask);
07a51b
+	} else {
07a51b
+		memset(lmask, 0xff, sizeof(lmask));
07a51b
+		slen_mask = sizeof(lmask)*2;
07a51b
+	}
07a51b
+	addattr_l(n, MAX_MSG, TCA_CT_LABELS_MASK, lmask, slen_mask/2);
07a51b
+
07a51b
+	return 0;
07a51b
+}
07a51b
+
07a51b
+static int
07a51b
+parse_ct(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
07a51b
+		struct nlmsghdr *n)
07a51b
+{
07a51b
+	struct tc_ct sel = {};
07a51b
+	char **argv = *argv_p;
07a51b
+	struct rtattr *tail;
07a51b
+	int argc = *argc_p;
07a51b
+	int ct_action = 0;
07a51b
+	int ret;
07a51b
+
07a51b
+	tail = addattr_nest(n, MAX_MSG, tca_id);
07a51b
+
07a51b
+	if (argc && matches(*argv, "ct") == 0)
07a51b
+		NEXT_ARG_FWD();
07a51b
+
07a51b
+	while (argc > 0) {
07a51b
+		if (matches(*argv, "zone") == 0) {
07a51b
+			NEXT_ARG();
07a51b
+
07a51b
+			if (ct_parse_u16(*argv,
07a51b
+					 TCA_CT_ZONE, TCA_CT_UNSPEC, n)) {
07a51b
+				fprintf(stderr, "ct: Illegal \"zone\"\n");
07a51b
+				return -1;
07a51b
+			}
07a51b
+		} else if (matches(*argv, "nat") == 0) {
07a51b
+			ct_action |= TCA_CT_ACT_NAT;
07a51b
+
07a51b
+			NEXT_ARG();
07a51b
+			if (matches(*argv, "src") == 0)
07a51b
+				ct_action |= TCA_CT_ACT_NAT_SRC;
07a51b
+			else if (matches(*argv, "dst") == 0)
07a51b
+				ct_action |= TCA_CT_ACT_NAT_DST;
07a51b
+			else
07a51b
+				continue;
07a51b
+
07a51b
+			NEXT_ARG();
07a51b
+			if (matches(*argv, "addr") != 0)
07a51b
+				usage();
07a51b
+
07a51b
+			NEXT_ARG();
07a51b
+			ret = ct_parse_nat_addr_range(*argv, n);
07a51b
+			if (ret) {
07a51b
+				fprintf(stderr, "ct: Illegal nat address range\n");
07a51b
+				return -1;
07a51b
+			}
07a51b
+
07a51b
+			NEXT_ARG_FWD();
07a51b
+			if (matches(*argv, "port") != 0)
07a51b
+				continue;
07a51b
+
07a51b
+			NEXT_ARG();
07a51b
+			ret = ct_parse_nat_port_range(*argv, n);
07a51b
+			if (ret) {
07a51b
+				fprintf(stderr, "ct: Illegal nat port range\n");
07a51b
+				return -1;
07a51b
+			}
07a51b
+		} else if (matches(*argv, "clear") == 0) {
07a51b
+			ct_action |= TCA_CT_ACT_CLEAR;
07a51b
+		} else if (matches(*argv, "commit") == 0) {
07a51b
+			ct_action |= TCA_CT_ACT_COMMIT;
07a51b
+		} else if (matches(*argv, "force") == 0) {
07a51b
+			ct_action |= TCA_CT_ACT_FORCE;
07a51b
+		} else if (matches(*argv, "index") == 0) {
07a51b
+			NEXT_ARG();
07a51b
+			if (get_u32(&sel.index, *argv, 10)) {
07a51b
+				fprintf(stderr, "ct: Illegal \"index\"\n");
07a51b
+				return -1;
07a51b
+			}
07a51b
+		} else if (matches(*argv, "mark") == 0) {
07a51b
+			NEXT_ARG();
07a51b
+
07a51b
+			ret = ct_parse_mark(*argv, n);
07a51b
+			if (ret) {
07a51b
+				fprintf(stderr, "ct: Illegal \"mark\"\n");
07a51b
+				return -1;
07a51b
+			}
07a51b
+		} else if (matches(*argv, "label") == 0) {
07a51b
+			NEXT_ARG();
07a51b
+
07a51b
+			ret = ct_parse_labels(*argv, n);
07a51b
+			if (ret) {
07a51b
+				fprintf(stderr, "ct: Illegal \"label\"\n");
07a51b
+				return -1;
07a51b
+			}
07a51b
+		} else if (matches(*argv, "help") == 0) {
07a51b
+			usage();
07a51b
+		} else {
07a51b
+			break;
07a51b
+		}
07a51b
+		NEXT_ARG_FWD();
07a51b
+	}
07a51b
+
07a51b
+	if (ct_action & TCA_CT_ACT_CLEAR &&
07a51b
+	    ct_action & ~TCA_CT_ACT_CLEAR) {
07a51b
+		fprintf(stderr, "ct: clear can only be used alone\n");
07a51b
+		return -1;
07a51b
+	}
07a51b
+
07a51b
+	if (ct_action & TCA_CT_ACT_NAT_SRC &&
07a51b
+	    ct_action & TCA_CT_ACT_NAT_DST) {
07a51b
+		fprintf(stderr, "ct: src and dst nat can't be used together\n");
07a51b
+		return -1;
07a51b
+	}
07a51b
+
07a51b
+	if ((ct_action & TCA_CT_ACT_COMMIT) &&
07a51b
+	    (ct_action & TCA_CT_ACT_NAT) &&
07a51b
+	    !(ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) {
07a51b
+		fprintf(stderr, "ct: commit and nat must set src or dst\n");
07a51b
+		return -1;
07a51b
+	}
07a51b
+
07a51b
+	if (!(ct_action & TCA_CT_ACT_COMMIT) &&
07a51b
+	    (ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) {
07a51b
+		fprintf(stderr, "ct: src or dst is only valid if commit is set\n");
07a51b
+		return -1;
07a51b
+	}
07a51b
+
07a51b
+	parse_action_control_dflt(&argc, &argv, &sel.action, false,
07a51b
+				  TC_ACT_PIPE);
07a51b
+	NEXT_ARG_FWD();
07a51b
+
07a51b
+	addattr16(n, MAX_MSG, TCA_CT_ACTION, ct_action);
07a51b
+	addattr_l(n, MAX_MSG, TCA_CT_PARMS, &sel, sizeof(sel));
07a51b
+	addattr_nest_end(n, tail);
07a51b
+
07a51b
+	*argc_p = argc;
07a51b
+	*argv_p = argv;
07a51b
+	return 0;
07a51b
+}
07a51b
+
07a51b
+static int ct_sprint_port(char *buf, const char *prefix, struct rtattr *attr)
07a51b
+{
07a51b
+	if (!attr)
07a51b
+		return 0;
07a51b
+
07a51b
+	return sprintf(buf, "%s%d", prefix, rta_getattr_be16(attr));
07a51b
+}
07a51b
+
07a51b
+static int ct_sprint_ip_addr(char *buf, const char *prefix,
07a51b
+			     struct rtattr *attr)
07a51b
+{
07a51b
+	int family;
07a51b
+	size_t len;
07a51b
+
07a51b
+	if (!attr)
07a51b
+		return 0;
07a51b
+
07a51b
+	len = RTA_PAYLOAD(attr);
07a51b
+
07a51b
+	if (len == 4)
07a51b
+		family = AF_INET;
07a51b
+	else if (len == 16)
07a51b
+		family = AF_INET6;
07a51b
+	else
07a51b
+		return 0;
07a51b
+
07a51b
+	return sprintf(buf, "%s%s", prefix, rt_addr_n2a_rta(family, attr));
07a51b
+}
07a51b
+
07a51b
+static void ct_print_nat(int ct_action, struct rtattr **tb)
07a51b
+{
07a51b
+	size_t done = 0;
07a51b
+	char out[256] = "";
07a51b
+	bool nat;
07a51b
+
07a51b
+	if (!(ct_action & TCA_CT_ACT_NAT))
07a51b
+		return;
07a51b
+
07a51b
+	if (ct_action & TCA_CT_ACT_NAT_SRC) {
07a51b
+		nat = true;
07a51b
+		done += sprintf(out + done, "src");
07a51b
+	} else if (ct_action & TCA_CT_ACT_NAT_DST) {
07a51b
+		nat = true;
07a51b
+		done += sprintf(out + done, "dst");
07a51b
+	}
07a51b
+
07a51b
+	if (nat) {
07a51b
+		done += ct_sprint_ip_addr(out + done, " addr ",
07a51b
+					  tb[TCA_CT_NAT_IPV4_MIN]);
07a51b
+		done += ct_sprint_ip_addr(out + done, " addr ",
07a51b
+					  tb[TCA_CT_NAT_IPV6_MIN]);
07a51b
+		if (tb[TCA_CT_NAT_IPV4_MAX] &&
07a51b
+		    memcmp(RTA_DATA(tb[TCA_CT_NAT_IPV4_MIN]),
07a51b
+			   RTA_DATA(tb[TCA_CT_NAT_IPV4_MAX]), 4))
07a51b
+			done += ct_sprint_ip_addr(out + done, "-",
07a51b
+						  tb[TCA_CT_NAT_IPV4_MAX]);
07a51b
+		else if (tb[TCA_CT_NAT_IPV6_MAX] &&
07a51b
+			    memcmp(RTA_DATA(tb[TCA_CT_NAT_IPV6_MIN]),
07a51b
+				   RTA_DATA(tb[TCA_CT_NAT_IPV6_MAX]), 16))
07a51b
+			done += ct_sprint_ip_addr(out + done, "-",
07a51b
+						  tb[TCA_CT_NAT_IPV6_MAX]);
07a51b
+		done += ct_sprint_port(out + done, " port ",
07a51b
+				       tb[TCA_CT_NAT_PORT_MIN]);
07a51b
+		if (tb[TCA_CT_NAT_PORT_MAX] &&
07a51b
+		    memcmp(RTA_DATA(tb[TCA_CT_NAT_PORT_MIN]),
07a51b
+			   RTA_DATA(tb[TCA_CT_NAT_PORT_MAX]), 2))
07a51b
+			done += ct_sprint_port(out + done, "-",
07a51b
+					       tb[TCA_CT_NAT_PORT_MAX]);
07a51b
+	}
07a51b
+
07a51b
+	if (done)
07a51b
+		print_string(PRINT_ANY, "nat", " nat %s", out);
07a51b
+	else
07a51b
+		print_string(PRINT_ANY, "nat", " nat", "");
07a51b
+}
07a51b
+
07a51b
+static void ct_print_labels(struct rtattr *attr,
07a51b
+			    struct rtattr *mask_attr)
07a51b
+{
07a51b
+	const unsigned char *str;
07a51b
+	bool print_mask = false;
07a51b
+	char out[256], *p;
07a51b
+	int data_len, i;
07a51b
+
07a51b
+	if (!attr)
07a51b
+		return;
07a51b
+
07a51b
+	data_len = RTA_PAYLOAD(attr);
07a51b
+	hexstring_n2a(RTA_DATA(attr), data_len, out, sizeof(out));
07a51b
+	p = out + data_len*2;
07a51b
+
07a51b
+	data_len = RTA_PAYLOAD(attr);
07a51b
+	str = RTA_DATA(mask_attr);
07a51b
+	if (data_len != 16)
07a51b
+		print_mask = true;
07a51b
+	for (i = 0; !print_mask && i < data_len; i++) {
07a51b
+		if (str[i] != 0xff)
07a51b
+			print_mask = true;
07a51b
+	}
07a51b
+	if (print_mask) {
07a51b
+		*p++ = '/';
07a51b
+		hexstring_n2a(RTA_DATA(mask_attr), data_len, p,
07a51b
+			      sizeof(out)-(p-out));
07a51b
+		p += data_len*2;
07a51b
+	}
07a51b
+	*p = '\0';
07a51b
+
07a51b
+	print_string(PRINT_ANY, "label", " label %s", out);
07a51b
+}
07a51b
+
07a51b
+static int print_ct(struct action_util *au, FILE *f, struct rtattr *arg)
07a51b
+{
07a51b
+	struct rtattr *tb[TCA_CT_MAX + 1];
07a51b
+	const char *commit;
07a51b
+	struct tc_ct *p;
07a51b
+	int ct_action = 0;
07a51b
+
07a51b
+	if (arg == NULL)
07a51b
+		return -1;
07a51b
+
07a51b
+	parse_rtattr_nested(tb, TCA_CT_MAX, arg);
07a51b
+	if (tb[TCA_CT_PARMS] == NULL) {
07a51b
+		print_string(PRINT_FP, NULL, "%s", "[NULL ct parameters]");
07a51b
+		return -1;
07a51b
+	}
07a51b
+
07a51b
+	p = RTA_DATA(tb[TCA_CT_PARMS]);
07a51b
+
07a51b
+	print_string(PRINT_ANY, "kind", "%s", "ct");
07a51b
+
07a51b
+	if (tb[TCA_CT_ACTION])
07a51b
+		ct_action = rta_getattr_u16(tb[TCA_CT_ACTION]);
07a51b
+	if (ct_action & TCA_CT_ACT_COMMIT) {
07a51b
+		commit = ct_action & TCA_CT_ACT_FORCE ?
07a51b
+			 "commit force" : "commit";
07a51b
+		print_string(PRINT_ANY, "action", " %s", commit);
07a51b
+	} else if (ct_action & TCA_CT_ACT_CLEAR) {
07a51b
+		print_string(PRINT_ANY, "action", " %s", "clear");
07a51b
+	}
07a51b
+
07a51b
+	print_masked_u32("mark", tb[TCA_CT_MARK], tb[TCA_CT_MARK_MASK]);
07a51b
+	print_masked_u16("zone", tb[TCA_CT_ZONE], NULL);
07a51b
+	ct_print_labels(tb[TCA_CT_LABELS], tb[TCA_CT_LABELS_MASK]);
07a51b
+	ct_print_nat(ct_action, tb);
07a51b
+
07a51b
+	print_action_control(f, " ", p->action, "");
07a51b
+
07a51b
+	print_uint(PRINT_ANY, "index", "\n\t index %u", p->index);
07a51b
+	print_int(PRINT_ANY, "ref", " ref %d", p->refcnt);
07a51b
+	print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt);
07a51b
+
07a51b
+	if (show_stats) {
07a51b
+		if (tb[TCA_CT_TM]) {
07a51b
+			struct tcf_t *tm = RTA_DATA(tb[TCA_CT_TM]);
07a51b
+
07a51b
+			print_tm(f, tm);
07a51b
+		}
07a51b
+	}
07a51b
+	print_string(PRINT_FP, NULL, "%s", "\n ");
07a51b
+
07a51b
+	return 0;
07a51b
+}
07a51b
+
07a51b
+struct action_util ct_action_util = {
07a51b
+	.id = "ct",
07a51b
+	.parse_aopt = parse_ct,
07a51b
+	.print_aopt = print_ct,
07a51b
+};
07a51b
diff --git a/tc/tc_util.c b/tc/tc_util.c
07a51b
index b90d256c33a4a..0eb530408d056 100644
07a51b
--- a/tc/tc_util.c
07a51b
+++ b/tc/tc_util.c
07a51b
@@ -914,3 +914,47 @@ compat_xstats:
07a51b
 	if (tb[TCA_XSTATS] && xstats)
07a51b
 		*xstats = tb[TCA_XSTATS];
07a51b
 }
07a51b
+
07a51b
+void print_masked_u32(const char *name, struct rtattr *attr,
07a51b
+		      struct rtattr *mask_attr)
07a51b
+{
07a51b
+	__u32 value, mask;
07a51b
+	SPRINT_BUF(namefrm);
07a51b
+	SPRINT_BUF(out);
07a51b
+	size_t done;
07a51b
+
07a51b
+	if (!attr)
07a51b
+		return;
07a51b
+
07a51b
+	value = rta_getattr_u32(attr);
07a51b
+	mask = mask_attr ? rta_getattr_u32(mask_attr) : UINT32_MAX;
07a51b
+
07a51b
+	done = sprintf(out, "%u", value);
07a51b
+	if (mask != UINT32_MAX)
07a51b
+		sprintf(out + done, "/0x%x", mask);
07a51b
+
07a51b
+	sprintf(namefrm, " %s %%s", name);
07a51b
+	print_string(PRINT_ANY, name, namefrm, out);
07a51b
+}
07a51b
+
07a51b
+void print_masked_u16(const char *name, struct rtattr *attr,
07a51b
+		      struct rtattr *mask_attr)
07a51b
+{
07a51b
+	__u16 value, mask;
07a51b
+	SPRINT_BUF(namefrm);
07a51b
+	SPRINT_BUF(out);
07a51b
+	size_t done;
07a51b
+
07a51b
+	if (!attr)
07a51b
+		return;
07a51b
+
07a51b
+	value = rta_getattr_u16(attr);
07a51b
+	mask = mask_attr ? rta_getattr_u16(mask_attr) : UINT16_MAX;
07a51b
+
07a51b
+	done = sprintf(out, "%u", value);
07a51b
+	if (mask != UINT16_MAX)
07a51b
+		sprintf(out + done, "/0x%x", mask);
07a51b
+
07a51b
+	sprintf(namefrm, " %s %%s", name);
07a51b
+	print_string(PRINT_ANY, name, namefrm, out);
07a51b
+}
07a51b
diff --git a/tc/tc_util.h b/tc/tc_util.h
07a51b
index eb4b60db3fdd7..0c3425abc62fa 100644
07a51b
--- a/tc/tc_util.h
07a51b
+++ b/tc/tc_util.h
07a51b
@@ -127,4 +127,8 @@ int action_a2n(char *arg, int *result, bool allow_num);
07a51b
 
07a51b
 bool tc_qdisc_block_exists(__u32 block_index);
07a51b
 
07a51b
+void print_masked_u32(const char *name, struct rtattr *attr,
07a51b
+		      struct rtattr *mask_attr);
07a51b
+void print_masked_u16(const char *name, struct rtattr *attr,
07a51b
+		      struct rtattr *mask_attr);
07a51b
 #endif
07a51b
-- 
07a51b
2.26.2
07a51b