summaryrefslogtreecommitdiffstats
path: root/tc/em_cmp.c
blob: dfd123df1e10aa65a7ee1c74741f527dd8a1c6be (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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * em_cmp.c		Simple comparison Ematch
 *
 * Authors:	Thomas Graf <tgraf@suug.ch>
 */

#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 <errno.h>

#include "m_ematch.h"
#include <linux/tc_ematch/tc_em_cmp.h>

extern struct ematch_util cmp_ematch_util;

static void cmp_print_usage(FILE *fd)
{
	fprintf(fd,
	    "Usage: cmp(ALIGN at OFFSET [ ATTRS ] { eq | lt | gt } VALUE)\n" \
	    "where: ALIGN  := { u8 | u16 | u32 }\n" \
	    "       ATTRS  := [ layer LAYER ] [ mask MASK ] [ trans ]\n" \
	    "       LAYER  := { link | network | transport | 0..%d }\n" \
	    "\n" \
	    "Example: cmp(u16 at 3 layer 2 mask 0xff00 gt 20)\n",
	    TCF_LAYER_MAX);
}

static int cmp_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
			  struct bstr *args)
{
	struct bstr *a;
	int align, opnd = 0;
	unsigned long offset = 0, layer = TCF_LAYER_NETWORK, mask = 0, value = 0;
	int offset_present = 0, value_present = 0;
	struct tcf_em_cmp cmp = {};

#define PARSE_ERR(CARG, FMT, ARGS...) \
	em_parse_error(EINVAL, args, CARG, &cmp_ematch_util, FMT, ##ARGS)

	if (args == NULL)
		return PARSE_ERR(args, "cmp: missing arguments");

	if (!bstrcmp(args, "u8"))
		align = TCF_EM_ALIGN_U8;
	else if (!bstrcmp(args, "u16"))
		align = TCF_EM_ALIGN_U16;
	else if (!bstrcmp(args, "u32"))
		align = TCF_EM_ALIGN_U32;
	else
		return PARSE_ERR(args, "cmp: invalid alignment");

	for (a = bstr_next(args); a; a = bstr_next(a)) {
		if (!bstrcmp(a, "at")) {
			if (a->next == NULL)
				return PARSE_ERR(a, "cmp: missing argument");
			a = bstr_next(a);

			offset = bstrtoul(a);
			if (offset == ULONG_MAX)
				return PARSE_ERR(a, "cmp: invalid offset, " \
				    "must be numeric");

			offset_present = 1;
		} else if (!bstrcmp(a, "layer")) {
			if (a->next == NULL)
				return PARSE_ERR(a, "cmp: missing argument");
			a = bstr_next(a);

			layer = parse_layer(a);
			if (layer == INT_MAX) {
				layer = bstrtoul(a);
				if (layer == ULONG_MAX)
					return PARSE_ERR(a, "cmp: invalid " \
					    "layer");
			}

			if (layer > TCF_LAYER_MAX)
				return PARSE_ERR(a, "cmp: illegal layer, " \
				    "must be in 0..%d", TCF_LAYER_MAX);
		} else if (!bstrcmp(a, "mask")) {
			if (a->next == NULL)
				return PARSE_ERR(a, "cmp: missing argument");
			a = bstr_next(a);

			mask = bstrtoul(a);
			if (mask == ULONG_MAX)
				return PARSE_ERR(a, "cmp: invalid mask");
		} else if (!bstrcmp(a, "trans")) {
			cmp.flags |= TCF_EM_CMP_TRANS;
		} else if (!bstrcmp(a, "eq") || !bstrcmp(a, "gt") ||
		    !bstrcmp(a, "lt")) {

			if (!bstrcmp(a, "eq"))
				opnd = TCF_EM_OPND_EQ;
			else if (!bstrcmp(a, "gt"))
				opnd = TCF_EM_OPND_GT;
			else if (!bstrcmp(a, "lt"))
				opnd = TCF_EM_OPND_LT;

			if (a->next == NULL)
				return PARSE_ERR(a, "cmp: missing argument");
			a = bstr_next(a);

			value = bstrtoul(a);
			if (value == ULONG_MAX)
				return PARSE_ERR(a, "cmp: invalid value");

			value_present = 1;
		} else
			return PARSE_ERR(a, "nbyte: unknown parameter");
	}

	if (offset_present == 0 || value_present == 0)
		return PARSE_ERR(a, "cmp: offset and value required");

	cmp.val = (__u32) value;
	cmp.mask = (__u32) mask;
	cmp.off = (__u16) offset;
	cmp.align = (__u8) align;
	cmp.layer = (__u8) layer;
	cmp.opnd = (__u8) opnd;

	addraw_l(n, MAX_MSG, hdr, sizeof(*hdr));
	addraw_l(n, MAX_MSG, &cmp, sizeof(cmp));

#undef PARSE_ERR
	return 0;
}

static int cmp_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
			  int data_len)
{
	struct tcf_em_cmp *cmp = data;

	if (data_len < sizeof(*cmp)) {
		fprintf(stderr, "CMP header size mismatch\n");
		return -1;
	}

	if (cmp->align == TCF_EM_ALIGN_U8)
		fprintf(fd, "u8 ");
	else if (cmp->align == TCF_EM_ALIGN_U16)
		fprintf(fd, "u16 ");
	else if (cmp->align == TCF_EM_ALIGN_U32)
		fprintf(fd, "u32 ");

	fprintf(fd, "at %d layer %d ", cmp->off, cmp->layer);

	if (cmp->mask)
		fprintf(fd, "mask 0x%x ", cmp->mask);

	if (cmp->flags & TCF_EM_CMP_TRANS)
		fprintf(fd, "trans ");

	if (cmp->opnd == TCF_EM_OPND_EQ)
		fprintf(fd, "eq ");
	else if (cmp->opnd == TCF_EM_OPND_LT)
		fprintf(fd, "lt ");
	else if (cmp->opnd == TCF_EM_OPND_GT)
		fprintf(fd, "gt ");

	fprintf(fd, "%d", cmp->val);

	return 0;
}

struct ematch_util cmp_ematch_util = {
	.kind = "cmp",
	.kind_num = TCF_EM_CMP,
	.parse_eopt = cmp_parse_eopt,
	.print_eopt = cmp_print_eopt,
	.print_usage = cmp_print_usage
};