libnl 3.8.0
basic.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2008-2013 Thomas Graf <tgraf@suug.ch>
4 */
5
6/**
7 * @ingroup cls
8 * @defgroup cls_basic Basic Classifier
9 *
10 * @par Introduction
11 * The basic classifier is the simplest form of a classifier. It does
12 * not have any special classification capabilities, instead it can be
13 * used to classify exclusively based on extended matches or to
14 * create a "catch-all" filter.
15 *
16 * @{
17 */
18
19#include "nl-default.h"
20
21#include <netlink/netlink.h>
22#include <netlink/route/classifier.h>
23#include <netlink/route/action.h>
24#include <netlink/route/cls/basic.h>
25#include <netlink/route/cls/ematch.h>
26
27#include "tc-api.h"
28
30{
31 uint32_t b_target;
32 struct rtnl_ematch_tree * b_ematch;
33 int b_mask;
34 struct rtnl_act * b_act;
35};
36
37/** @cond SKIP */
38#define BASIC_ATTR_TARGET 0x001
39#define BASIC_ATTR_EMATCH 0x002
40#define BASIC_ATTR_ACTION 0x004
41/** @endcond */
42
43static struct nla_policy basic_policy[TCA_BASIC_MAX+1] = {
44 [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
45 [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
46};
47
48static int basic_clone(void *_dst, void *_src)
49{
50 return -NLE_OPNOTSUPP;
51}
52
53static void basic_free_data(struct rtnl_tc *tc, void *data)
54{
55 struct rtnl_basic *b = data;
56
57 if (!b)
58 return;
59
60 if (b->b_act)
61 rtnl_act_put_all(&b->b_act);
62 rtnl_ematch_tree_free(b->b_ematch);
63}
64
65static int basic_msg_parser(struct rtnl_tc *tc, void *data)
66{
67 struct nlattr *tb[TCA_BASIC_MAX + 1];
68 struct rtnl_basic *b = data;
69 int err;
70
71 err = tca_parse(tb, TCA_BASIC_MAX, tc, basic_policy);
72 if (err < 0)
73 return err;
74
75 if (tb[TCA_BASIC_CLASSID]) {
76 b->b_target = nla_get_u32(tb[TCA_BASIC_CLASSID]);
77 b->b_mask |= BASIC_ATTR_TARGET;
78 }
79
80 if (tb[TCA_BASIC_EMATCHES]) {
81 if ((err = rtnl_ematch_parse_attr(tb[TCA_BASIC_EMATCHES],
82 &b->b_ematch)) < 0)
83 return err;
84
85 if (b->b_ematch)
86 b->b_mask |= BASIC_ATTR_EMATCH;
87 }
88 if (tb[TCA_BASIC_ACT]) {
89 b->b_mask |= BASIC_ATTR_ACTION;
90 err = rtnl_act_parse(&b->b_act, tb[TCA_BASIC_ACT]);
91 if (err < 0)
92 return err;
93 }
94
95 return 0;
96}
97
98static void basic_dump_line(struct rtnl_tc *tc, void *data,
99 struct nl_dump_params *p)
100{
101 struct rtnl_basic *b = data;
102 char buf[32];
103
104 if (!b)
105 return;
106
107 if (b->b_mask & BASIC_ATTR_EMATCH)
108 nl_dump(p, " ematch");
109 else
110 nl_dump(p, " match-all");
111
112 if (b->b_mask & BASIC_ATTR_TARGET)
113 nl_dump(p, " target %s",
114 rtnl_tc_handle2str(b->b_target, buf, sizeof(buf)));
115}
116
117static void basic_dump_details(struct rtnl_tc *tc, void *data,
118 struct nl_dump_params *p)
119{
120 struct rtnl_basic *b = data;
121
122 if (!b)
123 return;
124
125 if (b->b_mask & BASIC_ATTR_EMATCH) {
126 nl_dump_line(p, " ematch ");
127 rtnl_ematch_tree_dump(b->b_ematch, p);
128 } else
129 nl_dump(p, "no options.\n");
130}
131
132static int basic_msg_fill(struct rtnl_tc *tc, void *data,
133 struct nl_msg *msg)
134{
135 struct rtnl_basic *b = data;
136
137 if (!b)
138 return 0;
139
140 if (b->b_mask & BASIC_ATTR_TARGET)
141 NLA_PUT_U32(msg, TCA_BASIC_CLASSID, b->b_target);
142
143 if (b->b_mask & BASIC_ATTR_EMATCH &&
144 rtnl_ematch_fill_attr(msg, TCA_BASIC_EMATCHES, b->b_ematch) < 0)
145 goto nla_put_failure;
146
147 if (b->b_mask & BASIC_ATTR_ACTION) {
148 int err;
149
150 err = rtnl_act_fill(msg, TCA_BASIC_ACT, b->b_act);
151 if (err)
152 return err;
153 }
154
155 return 0;
156
157nla_put_failure:
158 return -NLE_NOMEM;
159}
160
161/**
162 * @name Attribute Modifications
163 * @{
164 */
165
166void rtnl_basic_set_target(struct rtnl_cls *cls, uint32_t target)
167{
168 struct rtnl_basic *b;
169
170 if (!(b = rtnl_tc_data(TC_CAST(cls))))
171 return;
172
173 b->b_target = target;
174 b->b_mask |= BASIC_ATTR_TARGET;
175}
176
177uint32_t rtnl_basic_get_target(struct rtnl_cls *cls)
178{
179 struct rtnl_basic *b;
180
181 if (!(b = rtnl_tc_data(TC_CAST(cls))))
182 return 0;
183
184 return b->b_target;
185}
186
187void rtnl_basic_set_ematch(struct rtnl_cls *cls, struct rtnl_ematch_tree *tree)
188{
189 struct rtnl_basic *b;
190
191 if (!(b = rtnl_tc_data(TC_CAST(cls))))
192 return;
193
194 if (b->b_ematch) {
195 rtnl_ematch_tree_free(b->b_ematch);
196 b->b_mask &= ~BASIC_ATTR_EMATCH;
197 }
198
199 b->b_ematch = tree;
200
201 if (tree)
202 b->b_mask |= BASIC_ATTR_EMATCH;
203}
204
205struct rtnl_ematch_tree *rtnl_basic_get_ematch(struct rtnl_cls *cls)
206{
207 struct rtnl_basic *b;
208
209 if (!(b = rtnl_tc_data(TC_CAST(cls))))
210 return NULL;
211
212 return b->b_ematch;
213}
214
215int rtnl_basic_add_action(struct rtnl_cls *cls, struct rtnl_act *act)
216{
217 struct rtnl_basic *b;
218 int err;
219
220 if (!act)
221 return 0;
222
223 if (!(b = rtnl_tc_data(TC_CAST(cls))))
224 return -NLE_NOMEM;
225
226 b->b_mask |= BASIC_ATTR_ACTION;
227 if ((err = rtnl_act_append(&b->b_act, act)))
228 return err;
229
230 /* In case user frees it */
231 rtnl_act_get(act);
232 return 0;
233}
234
235struct rtnl_act* rtnl_basic_get_action(struct rtnl_cls *cls)
236{
237 struct rtnl_basic *b;
238
239 if (!(b = rtnl_tc_data_peek(TC_CAST(cls))))
240 return NULL;
241
242 if (!(b->b_mask & BASIC_ATTR_ACTION))
243 return NULL;
244
245 return b->b_act;
246}
247
248int rtnl_basic_del_action(struct rtnl_cls *cls, struct rtnl_act *act)
249{
250 struct rtnl_basic *b;
251 int ret;
252
253 if (!act)
254 return 0;
255
256 if (!(b = rtnl_tc_data(TC_CAST(cls))))
257 return -NLE_NOMEM;
258
259 if (!(b->b_mask & BASIC_ATTR_ACTION))
260 return -NLE_INVAL;
261 ret = rtnl_act_remove(&b->b_act, act);
262 if (ret)
263 return ret;
264
265 if (!b->b_act)
266 b->b_mask &= ~BASIC_ATTR_ACTION;
267 rtnl_act_put(act);
268 return 0;
269}
270/** @} */
271
272static struct rtnl_tc_ops basic_ops = {
273 .to_kind = "basic",
274 .to_type = RTNL_TC_TYPE_CLS,
275 .to_size = sizeof(struct rtnl_basic),
276 .to_msg_parser = basic_msg_parser,
277 .to_clone = basic_clone,
278 .to_free_data = basic_free_data,
279 .to_msg_fill = basic_msg_fill,
280 .to_dump = {
281 [NL_DUMP_LINE] = basic_dump_line,
282 [NL_DUMP_DETAILS] = basic_dump_details,
283 },
284};
285
286static void _nl_init basic_init(void)
287{
288 rtnl_tc_register(&basic_ops);
289}
290
291static void _nl_exit basic_exit(void)
292{
293 rtnl_tc_unregister(&basic_ops);
294}
295
296/** @} */
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition: attr.c:710
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition: attr.h:230
@ NLA_NESTED
Nested attributes.
Definition: attr.h:42
@ NLA_U32
32 bit integer
Definition: attr.h:37
char * rtnl_tc_handle2str(uint32_t handle, char *buf, size_t len)
Convert a traffic control handle to a character string (Reentrant).
Definition: classid.c:109
void rtnl_ematch_tree_free(struct rtnl_ematch_tree *tree)
Free ematch tree object.
Definition: ematch.c:278
int rtnl_ematch_parse_attr(struct nlattr *attr, struct rtnl_ematch_tree **result)
Parse ematch netlink attributes.
Definition: ematch.c:396
void * rtnl_tc_data_peek(struct rtnl_tc *tc)
Returns the private data of the traffic control object.
Definition: tc.c:1065
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
Definition: tc.h:50
void * rtnl_tc_data(struct rtnl_tc *)
Return pointer to private data of traffic control object.
Definition: tc.c:1079
int rtnl_tc_register(struct rtnl_tc_ops *)
Register a traffic control module.
Definition: tc.c:1018
void rtnl_tc_unregister(struct rtnl_tc_ops *)
Unregister a traffic control module.
Definition: tc.c:1052
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:1017
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:20
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:21
Dumping parameters.
Definition: types.h:32
Attribute validation policy.
Definition: attr.h:63
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition: attr.h:65