summaryrefslogtreecommitdiffstats
path: root/lib/route/cls_obj.c
blob: dcf97ed47ef133b49406dcb1dd2eec9bdf5d285a (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
/*
 * lib/route/cls_api.c       Classifier Object
 *
 *	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) 2003-2010 Thomas Graf <tgraf@suug.ch>
 */

/**
 * @ingroup cls
 * @defgroup cls_obj Classifier Object
 * @{
 */

#include <netlink-local.h>
#include <netlink-tc.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/route/tc.h>
#include <netlink/route/classifier.h>
#include <netlink/route/classifier-modules.h>
#include <netlink/route/link.h>

/** @cond SKIP */
#define CLS_ATTR_PRIO		(TCA_ATTR_MAX << 1)
#define CLS_ATTR_PROTOCOL	(TCA_ATTR_MAX << 2)
/** @endcond */

static void cls_free_data(struct nl_object *obj)
{
	struct rtnl_cls *cls = (struct rtnl_cls *) obj;
	struct rtnl_cls_ops *cops;
	
	tca_free_data((struct rtnl_tc *) cls);

	cops = rtnl_cls_lookup_ops(cls);
	if (cops && cops->co_free_data)
		cops->co_free_data(cls);

	nl_data_free(cls->c_subdata);
}

static int cls_clone(struct nl_object *_dst, struct nl_object *_src)
{
	struct rtnl_cls *dst = nl_object_priv(_dst);
	struct rtnl_cls *src = nl_object_priv(_src);
	struct rtnl_cls_ops *cops;
	int err;
	
	err = tca_clone((struct rtnl_tc *) dst, (struct rtnl_tc *) src);
	if (err < 0)
		goto errout;

	if (src->c_subdata) {
		if (!(dst->c_subdata = nl_data_clone(src->c_subdata))) {
			err = -NLE_NOMEM;
			goto errout;
		}
	}

	cops = rtnl_cls_lookup_ops(src);
	if (cops && cops->co_clone)
		err = cops->co_clone(dst, src);
errout:
	return err;
}

static void cls_dump_line(struct nl_object *obj, struct nl_dump_params *p)
{
	char buf[32];
	struct rtnl_cls *cls = (struct rtnl_cls *) obj;
	struct rtnl_cls_ops *cops;

	tca_dump_line((struct rtnl_tc *) cls, "cls", p);

	nl_dump(p, " prio %u protocol %s", cls->c_prio,
		nl_ether_proto2str(cls->c_protocol, buf, sizeof(buf)));

	cops = rtnl_cls_lookup_ops(cls);
	if (cops && cops->co_dump[NL_DUMP_LINE])
		cops->co_dump[NL_DUMP_LINE](cls, p);
	nl_dump(p, "\n");
}

static void cls_dump_details(struct nl_object *obj, struct nl_dump_params *p)
{
	struct rtnl_cls *cls = (struct rtnl_cls *) obj;
	struct rtnl_cls_ops *cops;

	cls_dump_line(obj, p);
	tca_dump_details((struct rtnl_tc *) cls, p);

	cops = rtnl_cls_lookup_ops(cls);
	if (cops && cops->co_dump[NL_DUMP_DETAILS])
		cops->co_dump[NL_DUMP_DETAILS](cls, p);
	else
		nl_dump(p, "no options\n");
}

static void cls_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
{
	struct rtnl_cls *cls = (struct rtnl_cls *) obj;
	struct rtnl_cls_ops *cops;

	cls_dump_details(obj, p);
	tca_dump_stats((struct rtnl_tc *) cls, p);
	nl_dump(p, "\n");

	cops = rtnl_cls_lookup_ops(cls);
	if (cops && cops->co_dump[NL_DUMP_STATS])
		cops->co_dump[NL_DUMP_STATS](cls, p);
}

/**
 * @name Allocation/Freeing
 * @{
 */

struct rtnl_cls *rtnl_cls_alloc(void)
{
	return (struct rtnl_cls *) nl_object_alloc(&cls_obj_ops);
}

void rtnl_cls_put(struct rtnl_cls *cls)
{
	nl_object_put((struct nl_object *) cls);
}

/** @} */


/**
 * @name Attributes
 * @{
 */

int rtnl_cls_set_kind(struct rtnl_cls *cls, const char *kind)
{
	if (cls->ce_mask & TCA_ATTR_KIND)
		return -NLE_EXIST;

	tca_set_kind((struct rtnl_tc *) cls, kind);

	/* Force allocation of data */
	rtnl_cls_data(cls);

	return 0;
}

struct rtnl_cls_ops *rtnl_cls_get_ops(struct rtnl_cls *cls)
{
	return cls->c_ops;
}

void rtnl_cls_set_prio(struct rtnl_cls *cls, uint16_t prio)
{
	cls->c_prio = prio;
	cls->ce_mask |= CLS_ATTR_PRIO;
}

uint16_t rtnl_cls_get_prio(struct rtnl_cls *cls)
{
	if (cls->ce_mask & CLS_ATTR_PRIO)
		return cls->c_prio;
	else
		return 0;
}

void rtnl_cls_set_protocol(struct rtnl_cls *cls, uint16_t protocol)
{
	cls->c_protocol = protocol;
	cls->ce_mask |= CLS_ATTR_PROTOCOL;
}

uint16_t rtnl_cls_get_protocol(struct rtnl_cls *cls)
{
	if (cls->ce_mask & CLS_ATTR_PROTOCOL)
		return cls->c_protocol;
	else
		return ETH_P_ALL;
}

void *rtnl_cls_data(struct rtnl_cls *cls)
{
	if (!cls->c_subdata) {
		struct rtnl_cls_ops *ops = cls->c_ops;

		if (!ops) {
			if (!cls->c_kind[0])
				BUG();

			ops = __rtnl_cls_lookup_ops(cls->c_kind);
			if (ops == NULL)
				return NULL;

			cls->c_ops = ops;
		}

		if (!ops->co_size)
			BUG();

		if (!(cls->c_subdata = nl_data_alloc(NULL, ops->co_size)))
			return NULL;
	}

	return nl_data_get(cls->c_subdata);
}

/** @} */

struct nl_object_ops cls_obj_ops = {
	.oo_name		= "route/cls",
	.oo_size		= sizeof(struct rtnl_cls),
	.oo_free_data		= cls_free_data,
	.oo_clone		= cls_clone,
	.oo_dump = {
	    [NL_DUMP_LINE]	= cls_dump_line,
	    [NL_DUMP_DETAILS]	= cls_dump_details,
	    [NL_DUMP_STATS]	= cls_dump_stats,
	},
	.oo_compare		= tca_compare,
	.oo_id_attrs		= (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
};

/** @} */