libnl 3.8.0
mall.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2017 Volodymyr Bendiuga <volodymyr.bendiuga@gmail.com>
4 */
5
6/**
7 * @ingroup cls
8 * @defgroup cls_mall Match-all Classifier
9 *
10 * @{
11 */
12
13#include "nl-default.h"
14
15#include <netlink/netlink.h>
16#include <netlink/attr.h>
17#include <netlink/utils.h>
18#include <netlink/route/classifier.h>
19#include <netlink/route/cls/matchall.h>
20#include <netlink/route/action.h>
21
22#include "tc-api.h"
23
24struct rtnl_mall {
25 uint32_t m_classid;
26 uint32_t m_flags;
27 struct rtnl_act *m_act;
28 int m_mask;
29};
30
31#define MALL_ATTR_CLASSID 0x01
32#define MALL_ATTR_FLAGS 0x02
33#define MALL_ATTR_ACTION 0x03
34
35
36static struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
37 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
38 [TCA_MATCHALL_FLAGS] = { .type = NLA_U32 },
39};
40
41/**
42 * @name Attribute Modifications
43 * @{
44 */
45
46int rtnl_mall_set_classid(struct rtnl_cls *cls, uint32_t classid)
47{
48 struct rtnl_mall *mall;
49 if (!(mall = rtnl_tc_data(TC_CAST(cls))))
50 return -NLE_NOMEM;
51
52 mall->m_classid = classid;
53 mall->m_mask |= MALL_ATTR_CLASSID;
54
55 return 0;
56}
57
58int rtnl_mall_get_classid(struct rtnl_cls *cls, uint32_t *classid)
59{
60 struct rtnl_mall *mall;
61
62 if (!(mall = rtnl_tc_data_peek(TC_CAST(cls))))
63 return -NLE_INVAL;
64
65 if (!(mall->m_mask & MALL_ATTR_CLASSID))
66 return -NLE_INVAL;
67
68 *classid = mall->m_classid;
69 return 0;
70}
71
72int rtnl_mall_set_flags(struct rtnl_cls *cls, uint32_t flags)
73{
74 struct rtnl_mall *mall;
75
76 if (!(mall = rtnl_tc_data(TC_CAST(cls))))
77 return -NLE_NOMEM;
78
79 mall->m_flags = flags;
80 mall->m_mask |= MALL_ATTR_FLAGS;
81
82 return 0;
83}
84
85int rtnl_mall_get_flags(struct rtnl_cls *cls, uint32_t *flags)
86{
87 struct rtnl_mall *mall;
88
89 if (!(mall = rtnl_tc_data_peek(TC_CAST(cls))))
90 return -NLE_INVAL;
91
92 if (!(mall->m_mask & MALL_ATTR_FLAGS))
93 return -NLE_INVAL;
94
95 *flags = mall->m_flags;
96 return 0;
97}
98
99int rtnl_mall_append_action(struct rtnl_cls *cls, struct rtnl_act *act)
100{
101 struct rtnl_mall *mall;
102 int err;
103
104 if (!act)
105 return 0;
106
107 if (!(mall = rtnl_tc_data(TC_CAST(cls))))
108 return -NLE_NOMEM;
109
110 mall->m_mask |= MALL_ATTR_ACTION;
111 err = rtnl_act_append(&mall->m_act, act);
112 if (err < 0)
113 return err;
114
115 rtnl_act_get(act);
116 return 0;
117}
118
119struct rtnl_act *rtnl_mall_get_first_action(struct rtnl_cls *cls)
120{
121 struct rtnl_mall *mall;
122 struct rtnl_act *act;
123
124 if (!(mall = rtnl_tc_data(TC_CAST(cls))))
125 return NULL;
126
127 if (!(mall->m_mask & MALL_ATTR_ACTION))
128 return NULL;
129
130 act = mall->m_act;
131 rtnl_act_get(act);
132
133 return act;
134}
135
136int rtnl_mall_del_action(struct rtnl_cls *cls, struct rtnl_act *act)
137{
138 struct rtnl_mall *mall;
139 int ret;
140
141 if (!act)
142 return 0;
143
144 if (!(mall = rtnl_tc_data(TC_CAST(cls))))
145 return -NLE_NOMEM;
146
147 if (!(mall->m_mask & MALL_ATTR_ACTION))
148 return -NLE_INVAL;
149
150 ret = rtnl_act_remove(&mall->m_act, act);
151 if (ret < 0)
152 return ret;
153
154 rtnl_act_put(act);
155
156 return 0;
157}
158
159/** @} */
160
161static void mall_free_data(struct rtnl_tc *tc, void *data)
162{
163 struct rtnl_mall *mall = data;
164
165 if (mall->m_act)
166 rtnl_act_put_all(&mall->m_act);
167}
168
169static int mall_msg_parser(struct rtnl_tc *tc, void *data)
170{
171 struct rtnl_mall *mall = data;
172 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
173 int err;
174
175 err = tca_parse(tb, TCA_MATCHALL_MAX, tc, mall_policy);
176 if (err < 0)
177 return err;
178
179 if (tb[TCA_MATCHALL_CLASSID]) {
180 mall->m_classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
181 mall->m_mask |= MALL_ATTR_CLASSID;
182 }
183
184 if (tb[TCA_MATCHALL_FLAGS]) {
185 mall->m_flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
186 mall->m_mask |= MALL_ATTR_FLAGS;
187 }
188
189 if (tb[TCA_MATCHALL_ACT]) {
190 mall->m_mask |= MALL_ATTR_ACTION;
191 err = rtnl_act_parse(&mall->m_act, tb[TCA_MATCHALL_ACT]);
192 if (err < 0)
193 return err;
194 }
195
196 return 0;
197}
198
199static int mall_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
200{
201 struct rtnl_mall *mall = data;
202
203 if (!mall)
204 return 0;
205
206 if (mall->m_mask & MALL_ATTR_CLASSID)
207 NLA_PUT_U32(msg, TCA_MATCHALL_CLASSID, mall->m_classid);
208
209 if (mall->m_mask & MALL_ATTR_FLAGS)
210 NLA_PUT_U32(msg, TCA_MATCHALL_FLAGS, mall->m_flags);
211
212 if (mall->m_mask & MALL_ATTR_ACTION) {
213 int err;
214
215 err = rtnl_act_fill(msg, TCA_MATCHALL_ACT, mall->m_act);
216 if (err < 0)
217 return err;
218 }
219
220 return 0;
221
222nla_put_failure:
223 return -NLE_NOMEM;
224}
225
226static int mall_clone(void *_dst, void *_src)
227{
228 struct rtnl_mall *dst = _dst, *src = _src;
229 struct rtnl_act *next, *new;
230 int err;
231
232 dst->m_act = NULL;
233
234 if (src->m_act) {
235 if (!(dst->m_act = rtnl_act_alloc()))
236 return -NLE_NOMEM;
237
238 /* action nl list next and prev pointers must be updated */
239 nl_init_list_head(&dst->m_act->ce_list);
240
241 memcpy(dst->m_act, src->m_act, sizeof(struct rtnl_act));
242 next = rtnl_act_next(src->m_act);
243 while (next) {
244 new = (struct rtnl_act *) nl_object_clone((struct nl_object *) next);
245 if (!new)
246 return -NLE_NOMEM;
247
248 err = rtnl_act_append(&dst->m_act, new);
249 if (err < 0)
250 return err;
251
252 next = rtnl_act_next(next);
253 }
254 }
255
256 return 0;
257}
258
259static void mall_dump_line(struct rtnl_tc *tc, void *data,
260 struct nl_dump_params *p)
261{
262 struct rtnl_mall *mall = data;
263 char buf[32];
264
265 if (!mall)
266 return;
267
268 if (mall->m_mask & MALL_ATTR_CLASSID)
269 nl_dump(p, " target %s",
270 rtnl_tc_handle2str(mall->m_classid, buf, sizeof(buf)));
271}
272
273static void mall_dump_details(struct rtnl_tc *tc, void *data,
274 struct nl_dump_params *p)
275{
276 struct rtnl_mall *mall = data;
277
278 if (!mall)
279 return;
280
281 nl_dump(p, "no details for match-all");
282}
283
284static struct rtnl_tc_ops mall_ops = {
285 .to_kind = "matchall",
286 .to_type = RTNL_TC_TYPE_CLS,
287 .to_size = sizeof(struct rtnl_mall),
288 .to_msg_parser = mall_msg_parser,
289 .to_free_data = mall_free_data,
290 .to_clone = mall_clone,
291 .to_msg_fill = mall_msg_fill,
292 .to_dump = {
293 [NL_DUMP_LINE] = mall_dump_line,
294 [NL_DUMP_DETAILS] = mall_dump_details,
295 },
296};
297
298static void _nl_init mall_init(void)
299{
300 rtnl_tc_register(&mall_ops);
301}
302
303static void _nl_exit mall_exit(void)
304{
305 rtnl_tc_unregister(&mall_ops);
306}
307
308/** @} */
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_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
struct nl_object * nl_object_clone(struct nl_object *obj)
Allocate a new object and copy all data from an existing object.
Definition: object.c:111
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
Definition: mall.c:24