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