summaryrefslogtreecommitdiffstats
path: root/lib/route/cls/basic.c
blob: 835fd08c5dd69638029e490babffbca25dd42035 (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
/*
 * lib/route/cls/basic.c	Basic Classifier
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2008-2010 Thomas Graf <tgraf@suug.ch>
 */

/**
 * @ingroup cls
 * @defgroup basic Basic Classifier
 *
 * @par Introduction
 * The basic classifier is the simplest form of a classifier. It does
 * not have any special classification capabilities, instead it can be
 * used to classify exclusively based on extended matches or to
 * create a "catch-all" filter.
 *
 * @{
 */

#include <netlink-local.h>
#include <netlink-tc.h>
#include <netlink/netlink.h>
#include <netlink/route/classifier.h>
#include <netlink/route/classifier-modules.h>
#include <netlink/route/cls/basic.h>
#include <netlink/route/cls/ematch.h>

struct rtnl_basic
{
	uint32_t			b_target;
	struct rtnl_ematch_tree *	b_ematch;
	int				b_mask;
};

/** @cond SKIP */
#define BASIC_ATTR_TARGET	0x001
#define BASIC_ATTR_EMATCH	0x002
/** @endcond */

static struct nla_policy basic_policy[TCA_BASIC_MAX+1] = {
	[TCA_BASIC_CLASSID]	= { .type = NLA_U32 },
	[TCA_BASIC_EMATCHES]	= { .type = NLA_NESTED },
};

static int basic_clone(struct rtnl_cls *_dst, struct rtnl_cls *_src)
{
	return -NLE_OPNOTSUPP;
}

static void basic_free_data(struct rtnl_cls *cls)
{
	struct rtnl_basic *basic = rtnl_cls_data(cls);

	rtnl_ematch_tree_free(basic->b_ematch);
}

static int basic_msg_parser(struct rtnl_cls *cls)
{
	struct nlattr *tb[TCA_BASIC_MAX + 1];
	struct rtnl_basic *basic = rtnl_cls_data(cls);
	int err;

	err = tca_parse(tb, TCA_BASIC_MAX, (struct rtnl_tc *) cls, basic_policy);
	if (err < 0)
		return err;

	if (tb[TCA_BASIC_CLASSID]) {
		basic->b_target = nla_get_u32(tb[TCA_BASIC_CLASSID]);
		basic->b_mask |= BASIC_ATTR_TARGET;
	}

	if (tb[TCA_BASIC_EMATCHES]) {
		if ((err = rtnl_ematch_parse_attr(tb[TCA_BASIC_EMATCHES],
					     &basic->b_ematch)) < 0)
			return err;

		if (basic->b_ematch)
			basic->b_mask |= BASIC_ATTR_EMATCH;
	}

	return 0;
}

static void basic_dump_line(struct rtnl_cls *cls, struct nl_dump_params *p)
{
	struct rtnl_basic *b = rtnl_cls_data(cls);
	char buf[32];

	if (b->b_mask & BASIC_ATTR_EMATCH)
		nl_dump(p, " ematch");
	else
		nl_dump(p, " match-all");

	if (b->b_mask & BASIC_ATTR_TARGET)
		nl_dump(p, " target %s",
			rtnl_tc_handle2str(b->b_target, buf, sizeof(buf)));
}

static void basic_dump_details(struct rtnl_cls *cls, struct nl_dump_params *p)
{
	struct rtnl_basic *b = rtnl_cls_data(cls);

	if (b->b_mask & BASIC_ATTR_EMATCH) {
		nl_dump_line(p, "    ematch ");
		rtnl_ematch_tree_dump(b->b_ematch, p);
	} else
		nl_dump(p, "no options.\n");
}

static int basic_get_opts(struct rtnl_cls *cls, struct nl_msg *msg)
{
	struct rtnl_basic *b = rtnl_cls_data(cls);

	if (!(b->b_mask & BASIC_ATTR_TARGET))
		return -NLE_MISSING_ATTR;

	NLA_PUT_U32(msg, TCA_BASIC_CLASSID, b->b_target);

	return rtnl_ematch_fill_attr(msg, TCA_BASIC_EMATCHES, b->b_ematch);

nla_put_failure:
	return -NLE_NOMEM;
}

/**
 * @name Attribute Modifications
 * @{
 */

void rtnl_basic_set_target(struct rtnl_cls *cls, uint32_t target)
{
	struct rtnl_basic *b = rtnl_cls_data(cls);

	b->b_target = target;
	b->b_mask |= BASIC_ATTR_TARGET;
}

uint32_t rtnl_basic_get_target(struct rtnl_cls *cls)
{
	struct rtnl_basic *b = rtnl_cls_data(cls);

	return b->b_target;
}

void rtnl_basic_set_ematch(struct rtnl_cls *cls, struct rtnl_ematch_tree *tree)
{
	struct rtnl_basic *b = rtnl_cls_data(cls);

	if (b->b_ematch) {
		rtnl_ematch_tree_free(b->b_ematch);
		b->b_mask &= ~BASIC_ATTR_EMATCH;
	}

	b->b_ematch = tree;

	if (tree)
		b->b_mask |= BASIC_ATTR_EMATCH;
}

struct rtnl_ematch_tree *rtnl_basic_get_ematch(struct rtnl_cls *cls)
{
	return ((struct rtnl_basic *) rtnl_cls_data(cls))->b_ematch;
}

/** @} */

static struct rtnl_cls_ops basic_ops = {
	.co_kind		= "basic",
	.co_size		= sizeof(struct rtnl_basic),
	.co_msg_parser		= basic_msg_parser,
	.co_clone		= basic_clone,
	.co_free_data		= basic_free_data,
	.co_get_opts		= basic_get_opts,
	.co_dump = {
	    [NL_DUMP_LINE]	= basic_dump_line,
	    [NL_DUMP_DETAILS]	= basic_dump_details,
	},
};

static void __init basic_init(void)
{
	rtnl_cls_register(&basic_ops);
}

static void __exit basic_exit(void)
{
	rtnl_cls_unregister(&basic_ops);
}

/** @} */