1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2013 Cong Wang <[email protected]>
4 */
5
6 /**
7 * @ingroup tc
8 * @defgroup act Action
9 * @{
10 */
11
12 #include "nl-default.h"
13
14 #include <linux/gen_stats.h>
15
16 #include <netlink/netlink.h>
17 #include <netlink/utils.h>
18 #include <netlink/route/link.h>
19 #include <netlink/route/action.h>
20
21 #include "nl-route.h"
22 #include "tc-api.h"
23 #include "nl-priv-dynamic-core/object-api.h"
24 #include "nl-priv-dynamic-core/cache-api.h"
25 #include "nl-aux-route/nl-route.h"
26
27 static struct nl_object_ops act_obj_ops;
28 static struct nl_cache_ops rtnl_act_ops;
29
rtnl_act_next(struct rtnl_act * act)30 struct rtnl_act * rtnl_act_next(struct rtnl_act *act)
31 {
32 if (act == NULL) {
33 return NULL;
34 }
35
36 return act->a_next;
37 }
38
rtnl_act_append(struct rtnl_act ** head,struct rtnl_act * new)39 int rtnl_act_append(struct rtnl_act **head, struct rtnl_act *new)
40 {
41 struct rtnl_act *p_act;
42 int count = 1;
43
44 if (*head == NULL) {
45 *head = new;
46 return 0;
47 }
48
49 p_act = *head;
50 while (p_act->a_next) {
51 ++count;
52 p_act = p_act->a_next;
53 }
54
55 if (count > TCA_ACT_MAX_PRIO)
56 return -NLE_RANGE;
57
58 p_act->a_next = new;
59 return 0;
60 }
61
rtnl_act_remove(struct rtnl_act ** head,struct rtnl_act * act)62 int rtnl_act_remove(struct rtnl_act **head, struct rtnl_act *act)
63 {
64 struct rtnl_act *a, **ap;
65
66 for (ap = head; (a = *ap) != NULL; ap = &a->a_next)
67 if (a == act)
68 break;
69 if (a) {
70 *ap = a->a_next;
71 a->a_next = NULL;
72 return 0;
73 }
74
75 return -NLE_OBJ_NOTFOUND;
76 }
77
rtnl_act_fill_one(struct nl_msg * msg,struct rtnl_act * act,int order)78 static int rtnl_act_fill_one(struct nl_msg *msg, struct rtnl_act *act, int order)
79 {
80 struct rtnl_tc *tc = TC_CAST(act);
81 struct rtnl_tc_ops *ops;
82 struct nlattr *nest;
83 int err = -NLE_NOMEM;
84
85 nest = nla_nest_start(msg, order);
86 if (!nest)
87 goto nla_put_failure;
88
89 if (tc->ce_mask & TCA_ATTR_KIND)
90 NLA_PUT_STRING(msg, TCA_ACT_KIND, tc->tc_kind);
91
92 ops = rtnl_tc_get_ops(tc);
93 if (ops && (ops->to_msg_fill || ops->to_msg_fill_raw)) {
94 struct nlattr *opts;
95 void *data = rtnl_tc_data(tc);
96
97 if (ops->to_msg_fill) {
98 if (!(opts = nla_nest_start(msg, TCA_ACT_OPTIONS)))
99 goto nla_put_failure;
100
101 if ((err = ops->to_msg_fill(tc, data, msg)) < 0)
102 goto nla_put_failure;
103
104 nla_nest_end(msg, opts);
105 } else if ((err = ops->to_msg_fill_raw(tc, data, msg)) < 0)
106 goto nla_put_failure;
107 }
108 nla_nest_end(msg, nest);
109 return 0;
110
111 nla_put_failure:
112 return err;
113 }
114
rtnl_act_fill(struct nl_msg * msg,int attrtype,struct rtnl_act * act)115 int rtnl_act_fill(struct nl_msg *msg, int attrtype, struct rtnl_act *act)
116 {
117 struct rtnl_act *p_act = act;
118 struct nlattr *nest;
119 int err, order = 0;
120
121 nest = nla_nest_start(msg, attrtype);
122 if (!nest)
123 return -NLE_MSGSIZE;
124
125 while (p_act) {
126 err = rtnl_act_fill_one(msg, p_act, ++order);
127 if (err < 0)
128 return err;
129 p_act = p_act->a_next;
130 }
131
132 nla_nest_end(msg, nest);
133 return 0;
134 }
135
rtnl_act_msg_build(struct rtnl_act * act,int type,int flags,struct nl_msg ** result)136 static int rtnl_act_msg_build(struct rtnl_act *act, int type, int flags,
137 struct nl_msg **result)
138 {
139 struct nl_msg *msg;
140 struct tcamsg tcahdr = {
141 .tca_family = AF_UNSPEC,
142 };
143 int err = -NLE_MSGSIZE;
144
145 msg = nlmsg_alloc_simple(type, flags);
146 if (!msg)
147 return -NLE_NOMEM;
148
149 if (nlmsg_append(msg, &tcahdr, sizeof(tcahdr), NLMSG_ALIGNTO) < 0)
150 goto nla_put_failure;
151
152 err = rtnl_act_fill(msg, TCA_ACT_TAB, act);
153 if (err < 0)
154 goto nla_put_failure;
155
156 *result = msg;
157 return 0;
158
159 nla_put_failure:
160 nlmsg_free(msg);
161 return err;
162 }
163
act_build(struct rtnl_act * act,int type,int flags,struct nl_msg ** result)164 static int act_build(struct rtnl_act *act, int type, int flags,
165 struct nl_msg **result)
166 {
167 int err;
168
169 err = rtnl_act_msg_build(act, type, flags, result);
170 if (err < 0)
171 return err;
172 return 0;
173 }
174
175 /**
176 * @name Allocation/Freeing
177 * @{
178 */
179
rtnl_act_alloc(void)180 struct rtnl_act *rtnl_act_alloc(void)
181 {
182 struct rtnl_tc *tc;
183
184 tc = TC_CAST(nl_object_alloc(&act_obj_ops));
185 if (tc)
186 tc->tc_type = RTNL_TC_TYPE_ACT;
187
188 return (struct rtnl_act *) tc;
189 }
190
rtnl_act_get(struct rtnl_act * act)191 void rtnl_act_get(struct rtnl_act *act)
192 {
193 nl_object_get(OBJ_CAST(act));
194 }
195
rtnl_act_put(struct rtnl_act * act)196 void rtnl_act_put(struct rtnl_act *act)
197 {
198 nl_object_put((struct nl_object *) act);
199 }
200
201 /** @} */
202
203 /**
204 * @name Addition/Modification/Deletion
205 * @{
206 */
207
208 /**
209 * Build a netlink message requesting the addition of an action
210 * @arg act Action to add
211 * @arg flags Additional netlink message flags
212 * @arg result Pointer to store resulting netlink message
213 *
214 * The behaviour of this function is identical to rtnl_act_add() with
215 * the exception that it will not send the message but return it int the
216 * provided return pointer instead.
217 *
218 * @see rtnl_act_add()
219 *
220 * @return 0 on success or a negative error code.
221 */
rtnl_act_build_add_request(struct rtnl_act * act,int flags,struct nl_msg ** result)222 int rtnl_act_build_add_request(struct rtnl_act *act, int flags,
223 struct nl_msg **result)
224 {
225 return act_build(act, RTM_NEWACTION, flags, result);
226 }
227
228 /**
229 * Add/Update action
230 * @arg sk Netlink socket
231 * @arg act Action to add/update
232 * @arg flags Additional netlink message flags
233 *
234 * Builds a \c RTM_NEWACTION netlink message requesting the addition
235 * of a new action and sends the message to the kernel. The
236 * configuration of the action is derived from the attributes of
237 * the specified traffic class.
238 *
239 * The following flags may be specified:
240 * - \c NLM_F_CREATE: Create action if it does not exist,
241 * otherwise -NLE_OBJ_NOTFOUND is returned.
242 * - \c NLM_F_EXCL: Return -NLE_EXISTS if an action with
243 * matching handle exists already.
244 *
245 * Existing actions with matching handles will be updated, unless
246 * the flag \c NLM_F_EXCL is specified. If no matching action
247 * exists, it will be created if the flag \c NLM_F_CREATE is set,
248 * otherwise the error -NLE_OBJ_NOTFOUND is returned.
249 *
250 * After sending, the function will wait for the ACK or an eventual
251 * error message to be received and will therefore block until the
252 * operation has been completed.
253 *
254 * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
255 * this function to return immediately after sending. In this case,
256 * it is the responsibility of the caller to handle any error
257 * messages returned.
258 *
259 * @return 0 on success or a negative error code.
260 */
rtnl_act_add(struct nl_sock * sk,struct rtnl_act * act,int flags)261 int rtnl_act_add(struct nl_sock *sk, struct rtnl_act *act, int flags)
262 {
263 struct nl_msg *msg;
264 int err;
265
266 if ((err = rtnl_act_build_add_request(act, flags, &msg)) < 0)
267 return err;
268
269 return nl_send_sync(sk, msg);
270 }
271
272 /**
273 * Build a netlink message to change action attributes
274 * @arg act Action to change
275 * @arg flags additional netlink message flags
276 * @arg result Pointer to store resulting message.
277 *
278 * Builds a new netlink message requesting a change of a neigh
279 * attributes. The netlink message header isn't fully equipped with
280 * all relevant fields and must thus be sent out via nl_send_auto_complete()
281 * or supplemented as needed.
282 *
283 * @return 0 on success or a negative error code.
284 */
rtnl_act_build_change_request(struct rtnl_act * act,int flags,struct nl_msg ** result)285 int rtnl_act_build_change_request(struct rtnl_act *act, int flags,
286 struct nl_msg **result)
287 {
288 return act_build(act, RTM_NEWACTION, NLM_F_REPLACE | flags, result);
289 }
290
291 /**
292 * Change an action
293 * @arg sk Netlink socket.
294 * @arg act action to change
295 * @arg flags additional netlink message flags
296 *
297 * Builds a netlink message by calling rtnl_act_build_change_request(),
298 * sends the request to the kernel and waits for the next ACK to be
299 * received and thus blocks until the request has been processed.
300 *
301 * @return 0 on success or a negative error if an error occured.
302 */
rtnl_act_change(struct nl_sock * sk,struct rtnl_act * act,int flags)303 int rtnl_act_change(struct nl_sock *sk, struct rtnl_act *act, int flags)
304 {
305 struct nl_msg *msg;
306 int err;
307
308 if ((err = rtnl_act_build_change_request(act, flags, &msg)) < 0)
309 return err;
310
311 return nl_send_sync(sk, msg);
312 }
313
314 /**
315 * Build netlink message requesting the deletion of an action
316 * @arg act Action to delete
317 * @arg flags Additional netlink message flags
318 * @arg result Pointer to store resulting netlink message
319 *
320 * The behaviour of this function is identical to rtnl_act_delete() with
321 * the exception that it will not send the message but return it in the
322 * provided return pointer instead.
323 *
324 * @see rtnl_act_delete()
325 *
326 * @return 0 on success or a negative error code.
327 */
rtnl_act_build_delete_request(struct rtnl_act * act,int flags,struct nl_msg ** result)328 int rtnl_act_build_delete_request(struct rtnl_act *act, int flags,
329 struct nl_msg **result)
330 {
331 return act_build(act, RTM_DELACTION, flags, result);
332 }
333
334 /**
335 * Delete action
336 * @arg sk Netlink socket
337 * @arg act Action to delete
338 * @arg flags Additional netlink message flags
339 *
340 * Builds a \c RTM_DELACTION netlink message requesting the deletion
341 * of an action and sends the message to the kernel.
342 *
343 * The message is constructed out of the following attributes:
344 * - \c ifindex (required)
345 * - \c prio (required)
346 * - \c protocol (required)
347 * - \c handle (required)
348 * - \c parent (optional, if not specified parent equals root-qdisc)
349 * - \c kind (optional, must match if provided)
350 *
351 * All other action attributes including all class type specific
352 * attributes are ignored.
353 *
354 * After sending, the function will wait for the ACK or an eventual
355 * error message to be received and will therefore block until the
356 * operation has been completed.
357 *
358 * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
359 * this function to return immediately after sending. In this case,
360 * it is the responsibility of the caller to handle any error
361 * messages returned.
362 *
363 * @return 0 on success or a negative error code.
364 */
rtnl_act_delete(struct nl_sock * sk,struct rtnl_act * act,int flags)365 int rtnl_act_delete(struct nl_sock *sk, struct rtnl_act *act, int flags)
366 {
367 struct nl_msg *msg;
368 int err;
369
370 if ((err = rtnl_act_build_delete_request(act, flags, &msg)) < 0)
371 return err;
372
373 return nl_send_sync(sk, msg);
374 }
375
376 /** @} */
377
act_dump_line(struct rtnl_tc * tc,struct nl_dump_params * p)378 static void act_dump_line(struct rtnl_tc *tc, struct nl_dump_params *p)
379 {
380 }
381
rtnl_act_put_all(struct rtnl_act ** head)382 void rtnl_act_put_all(struct rtnl_act **head)
383 {
384 struct rtnl_act *curr, *next;
385
386 curr = *head;
387 while (curr) {
388 next = curr->a_next;
389 rtnl_act_put(curr);
390 curr = next;
391 }
392 *head = NULL;
393 }
394
395 static struct nla_policy tc_act_stats_policy[TCA_STATS_MAX+1] = {
396 [TCA_STATS_BASIC] = { .minlen = sizeof(struct gnet_stats_basic) },
397 [TCA_STATS_QUEUE] = { .minlen = sizeof(struct gnet_stats_queue) },
398 [TCA_STATS_RATE_EST] = { .minlen = sizeof(struct gnet_stats_rate_est) },
399 [TCA_STATS_RATE_EST64] = { .minlen = sizeof(struct gnet_stats_rate_est64) },
400 };
401
rtnl_act_parse(struct rtnl_act ** head,struct nlattr * tb)402 int rtnl_act_parse(struct rtnl_act **head, struct nlattr *tb)
403 {
404 _nl_auto_rtnl_act_all struct rtnl_act *tmp_head = NULL;
405 struct rtnl_tc_ops *ops;
406 struct nlattr *tb2[TCA_ACT_MAX + 1];
407 struct nlattr *nla[TCA_ACT_MAX_PRIO + 1];
408 char kind[TCKINDSIZ];
409 int err, i;
410
411 err = nla_parse(nla, TCA_ACT_MAX_PRIO, nla_data(tb),
412 NLMSG_ALIGN(nla_len(tb)), NULL);
413 if (err < 0)
414 return err;
415
416 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
417 _nl_auto_rtnl_act struct rtnl_act *act = NULL;
418 struct rtnl_tc *tc;
419
420 if (nla[i] == NULL)
421 continue;
422
423 act = rtnl_act_alloc();
424 if (!act)
425 return -NLE_NOMEM;
426
427 tc = TC_CAST(act);
428 err = nla_parse(tb2, TCA_ACT_MAX, nla_data(nla[i]),
429 nla_len(nla[i]), NULL);
430 if (err < 0)
431 return err;
432
433 if (tb2[TCA_ACT_KIND] == NULL)
434 return -NLE_MISSING_ATTR;
435
436 nla_strlcpy(kind, tb2[TCA_ACT_KIND], sizeof(kind));
437 rtnl_tc_set_kind(tc, kind);
438
439 if (tb2[TCA_ACT_OPTIONS]) {
440 tc->tc_opts = nl_data_alloc_attr(tb2[TCA_ACT_OPTIONS]);
441 if (!tc->tc_opts)
442 return -NLE_NOMEM;
443 tc->ce_mask |= TCA_ATTR_OPTS;
444 }
445
446 if (tb2[TCA_ACT_STATS]) {
447 struct nlattr *tb3[TCA_STATS_MAX + 1];
448
449 err = nla_parse_nested(tb3, TCA_STATS_MAX, tb2[TCA_ACT_STATS],
450 tc_act_stats_policy);
451 if (err < 0)
452 return err;
453
454 if (tb3[TCA_STATS_BASIC]) {
455 struct gnet_stats_basic bs;
456
457 memcpy(&bs, nla_data(tb3[TCA_STATS_BASIC]),
458 sizeof(bs));
459 tc->tc_stats[RTNL_TC_BYTES] = bs.bytes;
460 tc->tc_stats[RTNL_TC_PACKETS] = bs.packets;
461 }
462 if (tb3[TCA_STATS_RATE_EST64]) {
463 struct gnet_stats_rate_est64 re;
464
465 memcpy(&re, nla_data(tb3[TCA_STATS_RATE_EST64]),
466 sizeof(re));
467 tc->tc_stats[RTNL_TC_RATE_BPS] = re.bps;
468 tc->tc_stats[RTNL_TC_RATE_PPS] = re.pps;
469 } else if (tb3[TCA_STATS_RATE_EST]) {
470 struct gnet_stats_rate_est *re;
471
472 re = nla_data(tb3[TCA_STATS_RATE_EST]);
473 tc->tc_stats[RTNL_TC_RATE_BPS] = re->bps;
474 tc->tc_stats[RTNL_TC_RATE_PPS] = re->pps;
475 }
476 if (tb3[TCA_STATS_QUEUE]) {
477 struct gnet_stats_queue *q;
478
479 q = nla_data(tb3[TCA_STATS_QUEUE]);
480 tc->tc_stats[RTNL_TC_DROPS] = q->drops;
481 tc->tc_stats[RTNL_TC_OVERLIMITS] = q->overlimits;
482 }
483 }
484
485 ops = rtnl_tc_get_ops(tc);
486 if (ops && ops->to_msg_parser) {
487 void *data = rtnl_tc_data(tc);
488
489 if (!data)
490 return -NLE_NOMEM;
491
492 err = ops->to_msg_parser(tc, data);
493 if (err < 0)
494 return err;
495 }
496 err = _rtnl_act_append_take(&tmp_head, _nl_steal_pointer(&act));
497 if (err < 0)
498 return err;
499 }
500
501 *head = _nl_steal_pointer(&tmp_head);
502 return 0;
503 }
504
rtnl_act_msg_parse(struct nlmsghdr * n,struct rtnl_act ** act)505 static int rtnl_act_msg_parse(struct nlmsghdr *n, struct rtnl_act **act)
506 {
507 struct rtnl_tc *tc = TC_CAST(*act);
508 struct nl_cache *link_cache;
509 struct nlattr *tb[TCAA_MAX + 1];
510 struct tcamsg *tm;
511 int err;
512
513 tc->ce_msgtype = n->nlmsg_type;
514
515 err = nlmsg_parse(n, sizeof(*tm), tb, TCAA_MAX, NULL);
516 if (err < 0)
517 return err;
518
519 tm = nlmsg_data(n);
520 tc->tc_family = tm->tca_family;
521
522 if (tb[TCA_ACT_TAB] == NULL)
523 return -NLE_MISSING_ATTR;
524
525 err = rtnl_act_parse(act, tb[TCA_ACT_TAB]);
526 if (err < 0)
527 return err;
528
529 if ((link_cache = __nl_cache_mngt_require("route/link"))) {
530 struct rtnl_link *link;
531
532 if ((link = rtnl_link_get(link_cache, tc->tc_ifindex))) {
533 rtnl_tc_set_link(tc, link);
534
535 /* rtnl_tc_set_link incs refcnt */
536 rtnl_link_put(link);
537 }
538 }
539
540 return 0;
541 }
act_msg_parser(struct nl_cache_ops * ops,struct sockaddr_nl * who,struct nlmsghdr * nlh,struct nl_parser_param * pp)542 static int act_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
543 struct nlmsghdr *nlh, struct nl_parser_param *pp)
544 {
545 struct rtnl_act *act, *p_act;
546 int err;
547
548 if (!(act = rtnl_act_alloc()))
549 return -NLE_NOMEM;
550
551 if ((err = rtnl_act_msg_parse(nlh, &act)) < 0)
552 goto errout;
553
554 p_act = act;
555 while(p_act) {
556 err = pp->pp_cb(OBJ_CAST(act), pp);
557 if (err) {
558 if (err > 0) {
559 _nl_assert_not_reached();
560 err = -NLE_FAILURE;
561 }
562 break;
563 }
564 p_act = p_act->a_next;
565 }
566 errout:
567 rtnl_act_put(act);
568
569 return err;
570 }
571
act_request_update(struct nl_cache * cache,struct nl_sock * sk)572 static int act_request_update(struct nl_cache *cache, struct nl_sock *sk)
573 {
574 struct tcamsg tcahdr = {
575 .tca_family = AF_UNSPEC,
576 };
577
578 return nl_send_simple(sk, RTM_GETACTION, NLM_F_DUMP, &tcahdr,
579 sizeof(tcahdr));
580 }
581
582 static struct rtnl_tc_type_ops act_ops = {
583 .tt_type = RTNL_TC_TYPE_ACT,
584 .tt_dump_prefix = "act",
585 .tt_dump = {
586 [NL_DUMP_LINE] = act_dump_line,
587 },
588 };
589
590 static struct nl_cache_ops rtnl_act_ops = {
591 .co_name = "route/act",
592 .co_hdrsize = sizeof(struct tcmsg),
593 .co_msgtypes = {
594 { RTM_NEWACTION, NL_ACT_NEW, "new" },
595 { RTM_DELACTION, NL_ACT_DEL, "del" },
596 { RTM_GETACTION, NL_ACT_GET, "get" },
597 END_OF_MSGTYPES_LIST,
598 },
599 .co_protocol = NETLINK_ROUTE,
600 .co_request_update = act_request_update,
601 .co_msg_parser = act_msg_parser,
602 .co_obj_ops = &act_obj_ops,
603 };
604
605 static struct nl_object_ops act_obj_ops = {
606 .oo_name = "route/act",
607 .oo_size = sizeof(struct rtnl_act),
608 .oo_free_data = rtnl_tc_free_data,
609 .oo_clone = rtnl_tc_clone,
610 .oo_dump = {
611 [NL_DUMP_LINE] = rtnl_tc_dump_line,
612 [NL_DUMP_DETAILS] = rtnl_tc_dump_details,
613 [NL_DUMP_STATS] = rtnl_tc_dump_stats,
614 },
615 .oo_compare = rtnl_tc_compare,
616 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
617 };
618
act_init(void)619 static void _nl_init act_init(void)
620 {
621 rtnl_tc_type_register(&act_ops);
622 nl_cache_mngt_register(&rtnl_act_ops);
623 }
624
act_exit(void)625 static void _nl_exit act_exit(void)
626 {
627 nl_cache_mngt_unregister(&rtnl_act_ops);
628 rtnl_tc_type_unregister(&act_ops);
629 }
630
631 /** @} */
632