aboutsummaryrefslogtreecommitdiffstats
path: root/tc/m_ife.c
blob: 90bc7a5b04f64a02b4ff41050ae44133caef75b3 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * m_ife.c	IFE actions module
 *
 * Authors:  J Hadi Salim (jhs@mojatatu.com)
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <linux/netdevice.h>

#include "rt_names.h"
#include "utils.h"
#include "tc_util.h"
#include <linux/tc_act/tc_ife.h>

static void ife_explain(void)
{
	fprintf(stderr,
		"Usage:... ife {decode|encode} [{ALLOW|USE} ATTR] [dst DMAC] [src SMAC] [type TYPE] [CONTROL] [index INDEX]\n"
		"\tALLOW := Encode direction. Allows encoding specified metadata\n"
		"\t\t e.g \"allow mark\"\n"
		"\tUSE := Encode direction. Enforce Static encoding of specified metadata\n"
		"\t\t e.g \"use mark 0x12\"\n"
		"\tATTR := mark (32-bit), prio (32-bit), tcindex (16-bit)\n"
		"\tDMAC := 6 byte Destination MAC address to encode\n"
		"\tSMAC := optional 6 byte Source MAC address to encode\n"
		"\tTYPE := optional 16 bit ethertype to encode\n"
		"\tCONTROL := reclassify|pipe|drop|continue|ok\n"
		"\tINDEX := optional IFE table index value used\n"
		"encode is used for sending IFE packets\n"
		"decode is used for receiving IFE packets\n");
}

static void ife_usage(void)
{
	ife_explain();
	exit(-1);
}

static int parse_ife(const struct action_util *a, int *argc_p, char ***argv_p,
		     int tca_id, struct nlmsghdr *n)
{
	int argc = *argc_p;
	char **argv = *argv_p;
	int ok = 0;
	struct tc_ife p = { 0 };
	struct rtattr *tail;
	struct rtattr *tail2;
	char dbuf[ETH_ALEN];
	char sbuf[ETH_ALEN];
	__u16 ife_type = 0;
	int user_type = 0;
	__u32 ife_prio = 0;
	__u32 ife_prio_v = 0;
	__u32 ife_mark = 0;
	__u32 ife_mark_v = 0;
	__u16 ife_tcindex = 0;
	__u16 ife_tcindex_v = 0;
	char *daddr = NULL;
	char *saddr = NULL;

	if (argc <= 0)
		return -1;

	while (argc > 0) {
		if (matches(*argv, "ife") == 0) {
			NEXT_ARG();
			continue;
		} else if (matches(*argv, "decode") == 0) {
			p.flags = IFE_DECODE; /* readability aid */
			ok++;
		} else if (matches(*argv, "encode") == 0) {
			p.flags = IFE_ENCODE;
			ok++;
		} else if (matches(*argv, "allow") == 0) {
			NEXT_ARG();
			if (matches(*argv, "mark") == 0) {
				ife_mark = IFE_META_SKBMARK;
			} else if (matches(*argv, "prio") == 0) {
				ife_prio = IFE_META_PRIO;
			} else if (matches(*argv, "tcindex") == 0) {
				ife_tcindex = IFE_META_TCINDEX;
			} else {
				invarg("Illegal meta define", *argv);
			}
		} else if (matches(*argv, "use") == 0) {
			NEXT_ARG();
			if (matches(*argv, "mark") == 0) {
				NEXT_ARG();
				if (get_u32(&ife_mark_v, *argv, 0))
					invarg("ife mark val is invalid",
					       *argv);
			} else if (matches(*argv, "prio") == 0) {
				NEXT_ARG();
				if (get_u32(&ife_prio_v, *argv, 0))
					invarg("ife prio val is invalid",
					       *argv);
			} else if (matches(*argv, "tcindex") == 0) {
				NEXT_ARG();
				if (get_u16(&ife_tcindex_v, *argv, 0))
					invarg("ife tcindex val is invalid",
					       *argv);
			} else {
				invarg("Illegal meta use type", *argv);
			}
		} else if (matches(*argv, "type") == 0) {
			NEXT_ARG();
			if (get_u16(&ife_type, *argv, 0))
				invarg("ife type is invalid", *argv);
			fprintf(stderr, "IFE type 0x%04X\n", ife_type);
			user_type = 1;
		} else if (matches(*argv, "dst") == 0) {
			NEXT_ARG();
			daddr = *argv;
			if (sscanf(daddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
				   dbuf, dbuf + 1, dbuf + 2,
				   dbuf + 3, dbuf + 4, dbuf + 5) != 6) {
				invarg("Invalid mac address", *argv);
			}
			fprintf(stderr, "dst MAC address <%s>\n", daddr);

		} else if (matches(*argv, "src") == 0) {
			NEXT_ARG();
			saddr = *argv;
			if (sscanf(saddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
				   sbuf, sbuf + 1, sbuf + 2,
				   sbuf + 3, sbuf + 4, sbuf + 5) != 6) {
				invarg("Invalid mac address", *argv);
			}
			fprintf(stderr, "src MAC address <%s>\n", saddr);
		} else if (matches(*argv, "help") == 0) {
			ife_usage();
		} else {
			break;
		}

		argc--;
		argv++;
	}

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

	if (argc) {
		if (matches(*argv, "index") == 0) {
			NEXT_ARG();
			if (get_u32(&p.index, *argv, 0)) {
				fprintf(stderr, "ife: Illegal \"index\"\n");
				return -1;
			}
			ok++;
			argc--;
			argv++;
		}
	}

	if (!ok) {
		fprintf(stderr, "IFE requires decode/encode specified\n");
		ife_usage();
	}

	tail = addattr_nest(n, MAX_MSG, tca_id);
	addattr_l(n, MAX_MSG, TCA_IFE_PARMS, &p, sizeof(p));

	if (!(p.flags & IFE_ENCODE))
		goto skip_encode;

	if (daddr)
		addattr_l(n, MAX_MSG, TCA_IFE_DMAC, dbuf, ETH_ALEN);
	if (user_type)
		addattr_l(n, MAX_MSG, TCA_IFE_TYPE, &ife_type, 2);
	else
		fprintf(stderr, "IFE type 0x%04X\n", ETH_P_IFE);
	if (saddr)
		addattr_l(n, MAX_MSG, TCA_IFE_SMAC, sbuf, ETH_ALEN);

	tail2 = addattr_nest(n, MAX_MSG, TCA_IFE_METALST);
	if (ife_mark || ife_mark_v) {
		if (ife_mark_v)
			addattr_l(n, MAX_MSG, IFE_META_SKBMARK, &ife_mark_v, 4);
		else
			addattr_l(n, MAX_MSG, IFE_META_SKBMARK, NULL, 0);
	}
	if (ife_prio || ife_prio_v) {
		if (ife_prio_v)
			addattr_l(n, MAX_MSG, IFE_META_PRIO, &ife_prio_v, 4);
		else
			addattr_l(n, MAX_MSG, IFE_META_PRIO, NULL, 0);
	}
	if (ife_tcindex || ife_tcindex_v) {
		if (ife_tcindex_v)
			addattr_l(n, MAX_MSG, IFE_META_TCINDEX, &ife_tcindex_v,
				  2);
		else
			addattr_l(n, MAX_MSG, IFE_META_TCINDEX, NULL, 0);
	}

	addattr_nest_end(n, tail2);

skip_encode:
	addattr_nest_end(n, tail);

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

static int print_ife(const struct action_util *au, FILE *f, struct rtattr *arg)
{
	struct tc_ife *p;
	struct rtattr *tb[TCA_IFE_MAX + 1];
	__u16 ife_type = 0;
	__u32 mmark = 0;
	__u16 mtcindex = 0;
	__u32 mprio = 0;
	int has_optional = 0;
	SPRINT_BUF(b2);

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

	parse_rtattr_nested(tb, TCA_IFE_MAX, arg);

	if (tb[TCA_IFE_PARMS] == NULL) {
		fprintf(stderr, "Missing ife parameters\n");
		return -1;
	}
	p = RTA_DATA(tb[TCA_IFE_PARMS]);

	print_string(PRINT_ANY, "mode", "%s ",
		     p->flags & IFE_ENCODE ? "encode" : "decode");
	print_action_control("action ", p->action, " ");

	if (tb[TCA_IFE_TYPE]) {
		ife_type = rta_getattr_u16(tb[TCA_IFE_TYPE]);
		has_optional = 1;
		print_0xhex(PRINT_ANY, "type", "type %#llX ", ife_type);
	}

	if (has_optional)
		print_string(PRINT_FP, NULL, "%s\t", _SL_);

	if (tb[TCA_IFE_METALST]) {
		struct rtattr *metalist[IFE_META_MAX + 1];
		int len = 0;

		parse_rtattr_nested(metalist, IFE_META_MAX,
				    tb[TCA_IFE_METALST]);

		if (metalist[IFE_META_SKBMARK]) {
			len = RTA_PAYLOAD(metalist[IFE_META_SKBMARK]);
			if (len) {
				mmark = rta_getattr_u32(metalist[IFE_META_SKBMARK]);
				print_uint(PRINT_ANY, "mark", "use mark %u ",
					   mmark);
			} else
				print_string(PRINT_ANY, "mark", "%s mark ",
					     "allow");
		}

		if (metalist[IFE_META_TCINDEX]) {
			len = RTA_PAYLOAD(metalist[IFE_META_TCINDEX]);
			if (len) {
				mtcindex =
					rta_getattr_u16(metalist[IFE_META_TCINDEX]);
				print_uint(PRINT_ANY, "tcindex",
					   "use tcindex %u ", mtcindex);
			} else
				print_string(PRINT_ANY, "tcindex",
					     "%s tcindex ", "allow");
		}

		if (metalist[IFE_META_PRIO]) {
			len = RTA_PAYLOAD(metalist[IFE_META_PRIO]);
			if (len) {
				mprio = rta_getattr_u32(metalist[IFE_META_PRIO]);
				print_uint(PRINT_ANY, "prio", "use prio %u ",
					   mprio);
			} else
				print_string(PRINT_ANY, "prio", "%s prio ",
					     "allow");
		}

	}

	if (tb[TCA_IFE_DMAC]) {
		has_optional = 1;
		print_string(PRINT_ANY, "dst", "dst %s ",
			     ll_addr_n2a(RTA_DATA(tb[TCA_IFE_DMAC]),
					 RTA_PAYLOAD(tb[TCA_IFE_DMAC]), 0, b2,
					 sizeof(b2)));
	}

	if (tb[TCA_IFE_SMAC]) {
		has_optional = 1;
		print_string(PRINT_ANY, "src", "src %s ",
			     ll_addr_n2a(RTA_DATA(tb[TCA_IFE_SMAC]),
					 RTA_PAYLOAD(tb[TCA_IFE_SMAC]), 0, b2,
					 sizeof(b2)));
	}

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

	if (show_stats) {
		if (tb[TCA_IFE_TM]) {
			struct tcf_t *tm = RTA_DATA(tb[TCA_IFE_TM]);

			print_tm(tm);
		}
	}

	print_nl();

	return 0;
}

struct action_util ife_action_util = {
	.id = "ife",
	.parse_aopt = parse_ife,
	.print_aopt = print_ife,
};