summaryrefslogtreecommitdiffstats
path: root/tc/m_skbedit.c
blob: b55c3249e6fafe906888ef395f857932f90acb07 (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
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * m_skbedit.c		SKB Editing module
 *
 * Copyright (c) 2008, Intel Corporation.
 *
 * Authors:	Alexander Duyck <alexander.h.duyck@intel.com>
 */

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

static void explain(void)
{
	fprintf(stderr, "Usage: ... skbedit <[QM] [PM] [MM] [PT] [IF]>\n"
		"QM = queue_mapping QUEUE_MAPPING\n"
		"PM = priority PRIORITY\n"
		"MM = mark MARK[/MASK]\n"
		"PT = ptype PACKETYPE\n"
		"IF = inheritdsfield\n"
		"PACKETYPE = is one of:\n"
		"  host, otherhost, broadcast, multicast\n"
		"QUEUE_MAPPING = device transmit queue to use\n"
		"PRIORITY = classID to assign to priority field\n"
		"MARK = firewall mark to set\n"
		"MASK = mask applied to firewall mark (0xffffffff by default)\n"
		"note: inheritdsfield maps DS field to skb->priority\n");
}

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

static int
parse_skbedit(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 rtattr *tail;
	unsigned int tmp;
	__u16 queue_mapping, ptype;
	__u32 flags = 0, priority, mark, mask;
	__u64 pure_flags = 0;
	struct tc_skbedit sel = { 0 };

	if (matches(*argv, "skbedit") != 0)
		return -1;

	NEXT_ARG();

	while (argc > 0) {
		if (matches(*argv, "queue_mapping") == 0) {
			flags |= SKBEDIT_F_QUEUE_MAPPING;
			NEXT_ARG();
			if (get_unsigned(&tmp, *argv, 10) || tmp > 65535) {
				fprintf(stderr, "Illegal queue_mapping\n");
				return -1;
			}
			queue_mapping = tmp;
			ok++;
		} else if (matches(*argv, "priority") == 0) {
			flags |= SKBEDIT_F_PRIORITY;
			NEXT_ARG();
			if (get_tc_classid(&priority, *argv)) {
				fprintf(stderr, "Illegal priority\n");
				return -1;
			}
			ok++;
		} else if (matches(*argv, "mark") == 0) {
			char *slash;

			NEXT_ARG();
			slash = strchr(*argv, '/');
			if (slash)
				*slash = '\0';

			flags |= SKBEDIT_F_MARK;
			if (get_u32(&mark, *argv, 0)) {
				fprintf(stderr, "Illegal mark\n");
				return -1;
			}

			if (slash) {
				if (get_u32(&mask, slash + 1, 0)) {
					fprintf(stderr, "Illegal mask\n");
					return -1;
				}
				flags |= SKBEDIT_F_MASK;
			}
			ok++;
		} else if (matches(*argv, "ptype") == 0) {

			NEXT_ARG();
			if (matches(*argv, "host") == 0) {
				ptype = PACKET_HOST;
			} else if (matches(*argv, "broadcast") == 0) {
				ptype = PACKET_BROADCAST;
			} else if (matches(*argv, "multicast") == 0) {
				ptype = PACKET_MULTICAST;
			} else if (matches(*argv, "otherhost") == 0) {
				ptype = PACKET_OTHERHOST;
			} else {
				fprintf(stderr, "Illegal ptype (%s)\n",
					*argv);
				return -1;
			}
			flags |= SKBEDIT_F_PTYPE;
			ok++;
		} else if (matches(*argv, "inheritdsfield") == 0) {
			pure_flags |= SKBEDIT_F_INHERITDSFIELD;
			ok++;
		} else if (matches(*argv, "help") == 0) {
			usage();
		} else {
			break;
		}
		argc--;
		argv++;
	}

	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, "skbedit: Illegal \"index\"\n");
				return -1;
			}
			argc--;
			argv++;
			ok++;
		}
	}

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


	tail = addattr_nest(n, MAX_MSG, tca_id);
	addattr_l(n, MAX_MSG, TCA_SKBEDIT_PARMS, &sel, sizeof(sel));
	if (flags & SKBEDIT_F_QUEUE_MAPPING)
		addattr_l(n, MAX_MSG, TCA_SKBEDIT_QUEUE_MAPPING,
			  &queue_mapping, sizeof(queue_mapping));
	if (flags & SKBEDIT_F_PRIORITY)
		addattr_l(n, MAX_MSG, TCA_SKBEDIT_PRIORITY,
			  &priority, sizeof(priority));
	if (flags & SKBEDIT_F_MARK)
		addattr_l(n, MAX_MSG, TCA_SKBEDIT_MARK,
			  &mark, sizeof(mark));
	if (flags & SKBEDIT_F_MASK)
		addattr_l(n, MAX_MSG, TCA_SKBEDIT_MASK,
			  &mask, sizeof(mask));
	if (flags & SKBEDIT_F_PTYPE)
		addattr_l(n, MAX_MSG, TCA_SKBEDIT_PTYPE,
			  &ptype, sizeof(ptype));
	if (pure_flags != 0)
		addattr64(n, MAX_MSG, TCA_SKBEDIT_FLAGS, pure_flags);
	addattr_nest_end(n, tail);

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

static int print_skbedit(const struct action_util *au, FILE *f, struct rtattr *arg)
{
	struct rtattr *tb[TCA_SKBEDIT_MAX + 1];

	SPRINT_BUF(b1);
	__u32 priority;
	__u16 ptype;
	struct tc_skbedit *p;

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

	parse_rtattr_nested(tb, TCA_SKBEDIT_MAX, arg);

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

	if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
		print_uint(PRINT_ANY, "queue_mapping", "queue_mapping %u",
			   rta_getattr_u16(tb[TCA_SKBEDIT_QUEUE_MAPPING]));
	}
	if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
		priority = rta_getattr_u32(tb[TCA_SKBEDIT_PRIORITY]);
		print_string(PRINT_ANY, "priority", " priority %s",
			     sprint_tc_classid(priority, b1));
	}
	if (tb[TCA_SKBEDIT_MARK] != NULL) {
		print_uint(PRINT_ANY, "mark", " mark %u",
			   rta_getattr_u32(tb[TCA_SKBEDIT_MARK]));
	}
	if (tb[TCA_SKBEDIT_MASK]) {
		print_hex(PRINT_ANY, "mask", "/%#x",
			  rta_getattr_u32(tb[TCA_SKBEDIT_MASK]));
	}
	if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
		ptype = rta_getattr_u16(tb[TCA_SKBEDIT_PTYPE]);
		if (ptype == PACKET_HOST)
			print_string(PRINT_ANY, "ptype", " ptype %s", "host");
		else if (ptype == PACKET_BROADCAST)
			print_string(PRINT_ANY, "ptype", " ptype %s",
				     "broadcast");
		else if (ptype == PACKET_MULTICAST)
			print_string(PRINT_ANY, "ptype", " ptype %s",
				     "multicast");
		else if (ptype == PACKET_OTHERHOST)
			print_string(PRINT_ANY, "ptype", " ptype %s",
				     "otherhost");
		else
			print_uint(PRINT_ANY, "ptype", " ptype %u", ptype);
	}
	if (tb[TCA_SKBEDIT_FLAGS] != NULL) {
		__u64 flags = rta_getattr_u64(tb[TCA_SKBEDIT_FLAGS]);

		if (flags & SKBEDIT_F_INHERITDSFIELD)
			print_null(PRINT_ANY, "inheritdsfield", " %s",
				     "inheritdsfield");
	}

	print_action_control(f, " ", p->action, "");

	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_SKBEDIT_TM]) {
			struct tcf_t *tm = RTA_DATA(tb[TCA_SKBEDIT_TM]);

			print_tm(f, tm);
		}
	}

	print_nl();

	return 0;
}

struct action_util skbedit_action_util = {
	.id = "skbedit",
	.parse_aopt = parse_skbedit,
	.print_aopt = print_skbedit,
};