12#include <netlink-private/netlink.h>
13#include <netlink-private/utils.h>
14#include <netlink-private/tc.h>
15#include <netlink/netlink.h>
16#include <netlink/utils.h>
17#include <netlink-private/route/tc-api.h>
18#include <netlink/route/link.h>
19#include <netlink/route/action.h>
22static struct nl_object_ops act_obj_ops;
23static struct nl_cache_ops rtnl_act_ops;
25struct rtnl_act * rtnl_act_next(
struct rtnl_act *act)
34int rtnl_act_append(
struct rtnl_act **head,
struct rtnl_act *
new)
36 struct rtnl_act *p_act;
45 while (p_act->a_next) {
47 p_act = p_act->a_next;
50 if (count > TCA_ACT_MAX_PRIO)
57int rtnl_act_remove(
struct rtnl_act **head,
struct rtnl_act *act)
59 struct rtnl_act *a, **ap;
61 for (ap = head; (a = *ap) != NULL; ap = &a->a_next)
70 return -NLE_OBJ_NOTFOUND;
73static int rtnl_act_fill_one(
struct nl_msg *msg,
struct rtnl_act *act,
int order)
75 struct rtnl_tc *tc =
TC_CAST(act);
76 struct rtnl_tc_ops *ops;
84 if (tc->ce_mask & TCA_ATTR_KIND)
87 ops = rtnl_tc_get_ops(tc);
88 if (ops && (ops->to_msg_fill || ops->to_msg_fill_raw)) {
92 if (ops->to_msg_fill) {
96 if ((err = ops->to_msg_fill(tc, data, msg)) < 0)
100 }
else if ((err = ops->to_msg_fill_raw(tc, data, msg)) < 0)
101 goto nla_put_failure;
110int rtnl_act_fill(
struct nl_msg *msg,
int attrtype,
struct rtnl_act *act)
112 struct rtnl_act *p_act = act;
121 err = rtnl_act_fill_one(msg, p_act, ++order);
124 p_act = p_act->a_next;
131static int rtnl_act_msg_build(
struct rtnl_act *act,
int type,
int flags,
132 struct nl_msg **result)
135 struct tcamsg tcahdr = {
136 .tca_family = AF_UNSPEC,
138 int err = -NLE_MSGSIZE;
144 if (
nlmsg_append(msg, &tcahdr,
sizeof(tcahdr), NLMSG_ALIGNTO) < 0)
145 goto nla_put_failure;
147 err = rtnl_act_fill(msg, TCA_ACT_TAB, act);
149 goto nla_put_failure;
159static int act_build(
struct rtnl_act *act,
int type,
int flags,
160 struct nl_msg **result)
164 err = rtnl_act_msg_build(act, type, flags, result);
175struct rtnl_act *rtnl_act_alloc(
void)
181 tc->tc_type = RTNL_TC_TYPE_ACT;
183 return (
struct rtnl_act *) tc;
186void rtnl_act_get(
struct rtnl_act *act)
191void rtnl_act_put(
struct rtnl_act *act)
218 struct nl_msg **result)
220 return act_build(act, RTM_NEWACTION, flags, result);
281 struct nl_msg **result)
283 return act_build(act, RTM_NEWACTION, NLM_F_REPLACE | flags, result);
324 struct nl_msg **result)
326 return act_build(act, RTM_DELACTION, flags, result);
373static void act_dump_line(
struct rtnl_tc *tc,
struct nl_dump_params *p)
377void rtnl_act_put_all(
struct rtnl_act **head)
379 struct rtnl_act *curr, *next;
390int rtnl_act_parse(
struct rtnl_act **head,
struct nlattr *tb)
392 struct rtnl_act *act;
393 struct rtnl_tc_ops *ops;
394 struct nlattr *tb2[TCA_ACT_MAX + 1];
395 struct nlattr *nla[TCA_ACT_MAX_PRIO + 1];
396 char kind[TCKINDSIZ];
400 NLMSG_ALIGN(
nla_len(tb)), NULL);
404 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
410 act = rtnl_act_alloc();
421 if (tb2[TCA_ACT_KIND] == NULL) {
422 err = -NLE_MISSING_ATTR;
426 nla_strlcpy(kind, tb2[TCA_ACT_KIND],
sizeof(kind));
429 if (tb2[TCA_ACT_OPTIONS]) {
435 tc->ce_mask |= TCA_ATTR_OPTS;
438 ops = rtnl_tc_get_ops(tc);
439 if (ops && ops->to_msg_parser) {
447 err = ops->to_msg_parser(tc, data);
451 err = rtnl_act_append(head, act);
459 rtnl_act_put_all(head);
464static int rtnl_act_msg_parse(
struct nlmsghdr *n,
struct rtnl_act **act)
466 struct rtnl_tc *tc =
TC_CAST(*act);
467 struct nl_cache *link_cache;
468 struct nlattr *tb[TCAA_MAX + 1];
472 tc->ce_msgtype = n->nlmsg_type;
474 err =
nlmsg_parse(n,
sizeof(*tm), tb, TCAA_MAX, NULL);
479 tc->tc_family = tm->tca_family;
481 if (tb[TCA_ACT_TAB] == NULL)
482 return -NLE_MISSING_ATTR;
484 err = rtnl_act_parse(act, tb[TCA_ACT_TAB]);
488 if ((link_cache = __nl_cache_mngt_require(
"route/link"))) {
501static int act_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
502 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
504 struct rtnl_act *act, *p_act;
507 if (!(act = rtnl_act_alloc()))
510 if ((err = rtnl_act_msg_parse(nlh, &act)) < 0)
515 err = pp->pp_cb(OBJ_CAST(act), pp);
518 _nl_assert_not_reached();
523 p_act = p_act->a_next;
531static int act_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
533 struct tcamsg tcahdr = {
534 .tca_family = AF_UNSPEC,
541static struct rtnl_tc_type_ops act_ops = {
542 .tt_type = RTNL_TC_TYPE_ACT,
543 .tt_dump_prefix =
"act",
549static struct nl_cache_ops rtnl_act_ops = {
550 .co_name =
"route/act",
551 .co_hdrsize =
sizeof(
struct tcmsg),
553 { RTM_NEWACTION, NL_ACT_NEW,
"new" },
554 { RTM_DELACTION, NL_ACT_DEL,
"del" },
555 { RTM_GETACTION, NL_ACT_GET,
"get" },
556 END_OF_MSGTYPES_LIST,
558 .co_protocol = NETLINK_ROUTE,
559 .co_request_update = act_request_update,
560 .co_msg_parser = act_msg_parser,
561 .co_obj_ops = &act_obj_ops,
564static struct nl_object_ops act_obj_ops = {
565 .oo_name =
"route/act",
566 .oo_size =
sizeof(
struct rtnl_act),
567 .oo_free_data = rtnl_tc_free_data,
568 .oo_clone = rtnl_tc_clone,
574 .oo_compare = rtnl_tc_compare,
575 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
578static void __init act_init(
void)
580 rtnl_tc_type_register(&act_ops);
584static void __exit act_exit(
void)
587 rtnl_tc_type_unregister(&act_ops);
int rtnl_act_change(struct nl_sock *sk, struct rtnl_act *act, int flags)
Change an action.
int rtnl_act_build_delete_request(struct rtnl_act *act, int flags, struct nl_msg **result)
Build netlink message requesting the deletion of an action.
int rtnl_act_build_add_request(struct rtnl_act *act, int flags, struct nl_msg **result)
Build a netlink message requesting the addition of an action.
int rtnl_act_delete(struct nl_sock *sk, struct rtnl_act *act, int flags)
Delete action.
int rtnl_act_add(struct nl_sock *sk, struct rtnl_act *act, int flags)
Add/Update action.
int rtnl_act_build_change_request(struct rtnl_act *act, int flags, struct nl_msg **result)
Build a netlink message to change action attributes.
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, const struct nla_policy *policy)
Create attribute index based on a stream of attributes.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
Copy string attribute payload to a buffer.
int nla_len(const struct nlattr *nla)
Return length of the payload .
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
struct nl_data * nl_data_alloc_attr(const struct nlattr *nla)
Allocate abstract data object based on netlink attribute.
struct rtnl_link * rtnl_link_get(struct nl_cache *cache, int ifindex)
Lookup link in cache by interface index.
void rtnl_link_put(struct rtnl_link *link)
Return a link object reference.
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
int nl_send_sync(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message and wait for ACK or error message.
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.
int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
Define the type of traffic control object.
void rtnl_tc_set_link(struct rtnl_tc *tc, struct rtnl_link *link)
Set link of traffic control object.
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
void * rtnl_tc_data(struct rtnl_tc *tc)
Return pointer to private data of traffic control object.
@ NL_DUMP_STATS
Dump all attributes including statistics.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.