libnl: /data/src/libnl3/lib/route/cls/basic.c Source File

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