summaryrefslogtreecommitdiffstats
path: root/tc/m_ctinfo.c
blob: 996a36217dfed6ccecf0bfa96e8ec2997314477c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * m_ctinfo.c		netfilter ctinfo mark action
 *
 * Copyright (c) 2019 Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "utils.h"
#include "tc_util.h"
#include <linux/tc_act/tc_ctinfo.h>

static void
explain(void)
{
	fprintf(stderr,
		"Usage: ... ctinfo [dscp mask [statemask]] [cpmark [mask]] [zone ZONE] [CONTROL] [index <INDEX>]\n"
		"where :\n"
		"\tdscp   MASK bitmask location of stored DSCP\n"
		"\t       STATEMASK bitmask to determine conditional restoring\n"
		"\tcpmark MASK mask applied to mark on restoration\n"
		"\tZONE is the conntrack zone\n"
		"\tCONTROL := reclassify | pipe | drop | continue | ok |\n"
		"\t           goto chain <CHAIN_INDEX>\n");
}

static void
usage(void)
{
	explain();
	exit(-1);
}

static int
parse_ctinfo(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
	     struct nlmsghdr *n)
{
	unsigned int cpmarkmask = 0, dscpmask = 0, dscpstatemask = 0;
	struct tc_ctinfo sel = {};
	unsigned short zone = 0;
	char **argv = *argv_p;
	struct rtattr *tail;
	int argc = *argc_p;
	int ok = 0;
	__u8 i;

	while (argc > 0) {
		if (matches(*argv, "ctinfo") == 0) {
			ok = 1;
			NEXT_ARG_FWD();
		} else if (matches(*argv, "help") == 0) {
			usage();
		} else {
			break;
		}

	}

	if (!ok) {
		explain();
		return -1;
	}

	if (argc) {
		if (matches(*argv, "dscp") == 0) {
			NEXT_ARG();
			if (get_u32(&dscpmask, *argv, 0)) {
				fprintf(stderr,
					"ctinfo: Illegal dscp \"mask\"\n");
				return -1;
			}
			if (NEXT_ARG_OK()) {
				NEXT_ARG_FWD();
				if (!get_u32(&dscpstatemask, *argv, 0))
					NEXT_ARG_FWD(); /* was a statemask */
			} else {
				NEXT_ARG_FWD();
			}
		}
	}

	/* cpmark has optional mask parameter, so the next arg might not  */
	/* exist, or it might be the next option, or it may actually be a */
	/* 32bit mask */
	if (argc) {
		if (matches(*argv, "cpmark") == 0) {
			cpmarkmask = ~0;
			if (NEXT_ARG_OK()) {
				NEXT_ARG_FWD();
				if (!get_u32(&cpmarkmask, *argv, 0))
					NEXT_ARG_FWD(); /* was a mask */
			} else {
				NEXT_ARG_FWD();
			}
		}
	}

	if (argc) {
		if (matches(*argv, "zone") == 0) {
			NEXT_ARG();
			if (get_u16(&zone, *argv, 10)) {
				fprintf(stderr, "ctinfo: Illegal \"zone\"\n");
				return -1;
			}
			NEXT_ARG_FWD();
		}
	}

	parse_action_control_dflt(&argc, &argv, &sel.action,
				  false, TC_ACT_PIPE);

	if (argc) {
		if (matches(*argv, "index") == 0) {
			NEXT_ARG();
			if (get_u32(&sel.index, *argv, 10)) {
				fprintf(stderr, "ctinfo: Illegal \"index\"\n");
				return -1;
			}
			NEXT_ARG_FWD();
		}
	}

	if (dscpmask & dscpstatemask) {
		fprintf(stderr,
			"ctinfo: dscp mask & statemask must NOT overlap\n");
		return -1;
	}

	i = ffs(dscpmask);
	if (i && ((~0 & (dscpmask >> (i - 1))) != 0x3f)) {
		fprintf(stderr,
			"ctinfo: dscp mask must be 6 contiguous bits long\n");
		return -1;
	}

	tail = addattr_nest(n, MAX_MSG, tca_id);
	addattr_l(n, MAX_MSG, TCA_CTINFO_ACT, &sel, sizeof(sel));
	addattr16(n, MAX_MSG, TCA_CTINFO_ZONE, zone);

	if (dscpmask)
		addattr32(n, MAX_MSG,
			  TCA_CTINFO_PARMS_DSCP_MASK, dscpmask);

	if (dscpstatemask)
		addattr32(n, MAX_MSG,
			  TCA_CTINFO_PARMS_DSCP_STATEMASK, dscpstatemask);

	if (cpmarkmask)
		addattr32(n, MAX_MSG,
			  TCA_CTINFO_PARMS_CPMARK_MASK, cpmarkmask);

	addattr_nest_end(n, tail);

	*argc_p = argc;
	*argv_p = argv;
	return 0;
}

static void print_ctinfo_stats(FILE *f, struct rtattr *tb[TCA_CTINFO_MAX + 1])
{
	struct tcf_t *tm;

	if (tb[TCA_CTINFO_TM]) {
		tm = RTA_DATA(tb[TCA_CTINFO_TM]);

		print_tm(f, tm);
	}

	if (tb[TCA_CTINFO_STATS_DSCP_SET])
		print_lluint(PRINT_ANY, "dscpset", " DSCP set %llu",
			     rta_getattr_u64(tb[TCA_CTINFO_STATS_DSCP_SET]));
	if (tb[TCA_CTINFO_STATS_DSCP_ERROR])
		print_lluint(PRINT_ANY, "dscperror", " error %llu",
			     rta_getattr_u64(tb[TCA_CTINFO_STATS_DSCP_ERROR]));

	if (tb[TCA_CTINFO_STATS_CPMARK_SET])
		print_lluint(PRINT_ANY, "cpmarkset", " CPMARK set %llu",
			     rta_getattr_u64(tb[TCA_CTINFO_STATS_CPMARK_SET]));
}

static int print_ctinfo(struct action_util *au, FILE *f, struct rtattr *arg)
{
	unsigned int cpmarkmask = ~0, dscpmask = 0, dscpstatemask = 0;
	struct rtattr *tb[TCA_CTINFO_MAX + 1];
	unsigned short zone = 0;
	struct tc_ctinfo *ci;

	print_string(PRINT_ANY, "kind", "%s ", "ctinfo");
	if (arg == NULL)
		return 0;

	parse_rtattr_nested(tb, TCA_CTINFO_MAX, arg);
	if (!tb[TCA_CTINFO_ACT]) {
		print_string(PRINT_FP, NULL, "%s",
			     "[NULL ctinfo action parameters]");
		return -1;
	}

	ci = RTA_DATA(tb[TCA_CTINFO_ACT]);

	if (tb[TCA_CTINFO_PARMS_DSCP_MASK]) {
		if (RTA_PAYLOAD(tb[TCA_CTINFO_PARMS_DSCP_MASK]) >=
		    sizeof(__u32))
			dscpmask = rta_getattr_u32(
					tb[TCA_CTINFO_PARMS_DSCP_MASK]);
		else
			print_string(PRINT_FP, NULL, "%s",
				     "[invalid dscp mask parameter]");
	}

	if (tb[TCA_CTINFO_PARMS_DSCP_STATEMASK]) {
		if (RTA_PAYLOAD(tb[TCA_CTINFO_PARMS_DSCP_STATEMASK]) >=
		    sizeof(__u32))
			dscpstatemask = rta_getattr_u32(
					tb[TCA_CTINFO_PARMS_DSCP_STATEMASK]);
		else
			print_string(PRINT_FP, NULL, "%s",
				     "[invalid dscp statemask parameter]");
	}

	if (tb[TCA_CTINFO_PARMS_CPMARK_MASK]) {
		if (RTA_PAYLOAD(tb[TCA_CTINFO_PARMS_CPMARK_MASK]) >=
		    sizeof(__u32))
			cpmarkmask = rta_getattr_u32(
					tb[TCA_CTINFO_PARMS_CPMARK_MASK]);
		else
			print_string(PRINT_FP, NULL, "%s",
				     "[invalid cpmark mask parameter]");
	}

	if (tb[TCA_CTINFO_ZONE] && RTA_PAYLOAD(tb[TCA_CTINFO_ZONE]) >=
	    sizeof(__u16))
		zone = rta_getattr_u16(tb[TCA_CTINFO_ZONE]);

	print_hu(PRINT_ANY, "zone", "zone %u", zone);
	print_action_control(f, " ", ci->action, "");

	print_nl();
	print_uint(PRINT_ANY, "index", "\t index %u", ci->index);
	print_int(PRINT_ANY, "ref", " ref %d", ci->refcnt);
	print_int(PRINT_ANY, "bind", " bind %d", ci->bindcnt);

	if (tb[TCA_CTINFO_PARMS_DSCP_MASK]) {
		print_0xhex(PRINT_ANY, "dscpmask", " dscp %#010llx", dscpmask);
		print_0xhex(PRINT_ANY, "dscpstatemask", " %#010llx",
			    dscpstatemask);
	}

	if (tb[TCA_CTINFO_PARMS_CPMARK_MASK])
		print_0xhex(PRINT_ANY, "cpmark", " cpmark %#010llx",
			    cpmarkmask);

	if (show_stats)
		print_ctinfo_stats(f, tb);

	print_nl();

	return 0;
}

struct action_util ctinfo_action_util = {
	.id = "ctinfo",
	.parse_aopt = parse_ctinfo,
	.print_aopt = print_ctinfo,
};