18 #include <netlink-private/netlink.h>
19 #include <netlink-private/tc.h>
20 #include <netlink/netlink.h>
21 #include <netlink/utils.h>
22 #include <netlink-private/route/tc-api.h>
23 #include <netlink/route/link.h>
26 static struct nl_object_ops act_obj_ops;
27 static struct nl_cache_ops rtnl_act_ops;
29 int rtnl_act_append(
struct rtnl_act **head,
struct rtnl_act *
new)
31 struct rtnl_act *p_act;
40 while (p_act->a_next) {
42 p_act = p_act->a_next;
45 if (count > TCA_ACT_MAX_PRIO)
52 int rtnl_act_remove(
struct rtnl_act **head,
struct rtnl_act *act)
54 struct rtnl_act *a, **ap;
56 for (ap = head; (a = *ap) != NULL; ap = &a->a_next)
65 return -NLE_OBJ_NOTFOUND;
68 static int rtnl_act_fill_one(
struct nl_msg *msg,
struct rtnl_act *act,
int order)
70 struct rtnl_tc *tc =
TC_CAST(act);
71 struct rtnl_tc_ops *ops;
79 if (tc->ce_mask & TCA_ATTR_KIND)
82 ops = rtnl_tc_get_ops(tc);
83 if (ops && (ops->to_msg_fill || ops->to_msg_fill_raw)) {
87 if (ops->to_msg_fill) {
91 if ((err = ops->to_msg_fill(tc, data, msg)) < 0)
95 }
else if ((err = ops->to_msg_fill_raw(tc, data, msg)) < 0)
105 int rtnl_act_fill(
struct nl_msg *msg,
int attrtype,
struct rtnl_act *act)
107 struct rtnl_act *p_act = act;
116 err = rtnl_act_fill_one(msg, p_act, ++order);
119 p_act = p_act->a_next;
126 static int rtnl_act_msg_build(
struct rtnl_act *act,
int type,
int flags,
127 struct nl_msg **result)
130 struct tcamsg tcahdr = {
131 .tca_family = AF_UNSPEC,
133 int err = -NLE_MSGSIZE;
139 if (
nlmsg_append(msg, &tcahdr,
sizeof(tcahdr), NLMSG_ALIGNTO) < 0)
140 goto nla_put_failure;
142 err = rtnl_act_fill(msg, TCA_ACT_TAB, act);
144 goto nla_put_failure;
154 static int act_build(
struct rtnl_act *act,
int type,
int flags,
155 struct nl_msg **result)
159 err = rtnl_act_msg_build(act, type, flags, result);
170 struct rtnl_act *rtnl_act_alloc(
void)
176 tc->tc_type = RTNL_TC_TYPE_ACT;
178 return (
struct rtnl_act *) tc;
181 void rtnl_act_get(
struct rtnl_act *act)
186 void rtnl_act_put(
struct rtnl_act *act)
213 struct nl_msg **result)
215 return act_build(act, RTM_NEWACTION, flags, result);
276 struct nl_msg **result)
278 return act_build(act, RTM_NEWACTION, NLM_F_REPLACE | flags, result);
319 struct nl_msg **result)
321 return act_build(act, RTM_DELACTION, flags, result);
368 static void act_dump_line(
struct rtnl_tc *tc,
struct nl_dump_params *p)
372 void rtnl_act_put_all(
struct rtnl_act **head)
374 struct rtnl_act *curr, *next;
385 int rtnl_act_parse(
struct rtnl_act **head,
struct nlattr *tb)
387 struct rtnl_act *act;
388 struct rtnl_tc_ops *ops;
389 struct nlattr *tb2[TCA_ACT_MAX + 1];
390 struct nlattr *nla[TCA_ACT_MAX_PRIO + 1];
391 char kind[TCKINDSIZ];
395 NLMSG_ALIGN(
nla_len(tb)), NULL);
399 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
405 act = rtnl_act_alloc();
416 if (tb2[TCA_ACT_KIND] == NULL) {
417 err = -NLE_MISSING_ATTR;
421 nla_strlcpy(kind, tb2[TCA_ACT_KIND],
sizeof(kind));
424 if (tb2[TCA_ACT_OPTIONS]) {
430 tc->ce_mask |= TCA_ATTR_OPTS;
433 ops = rtnl_tc_get_ops(tc);
434 if (ops && ops->to_msg_parser) {
442 err = ops->to_msg_parser(tc, data);
446 err = rtnl_act_append(head, act);
454 rtnl_act_put_all(head);
459 static int rtnl_act_msg_parse(
struct nlmsghdr *n,
struct rtnl_act **act)
461 struct rtnl_tc *tc =
TC_CAST(*act);
462 struct nl_cache *link_cache;
463 struct nlattr *tb[TCAA_MAX + 1];
467 tc->ce_msgtype = n->nlmsg_type;
469 err =
nlmsg_parse(n,
sizeof(*tm), tb, TCAA_MAX, NULL);
474 tc->tc_family = tm->tca_family;
476 if (tb[TCA_ACT_TAB] == NULL)
477 return -NLE_MISSING_ATTR;
479 err = rtnl_act_parse(act, tb[TCA_ACT_TAB]);
483 if ((link_cache = __nl_cache_mngt_require(
"route/link"))) {
496 static int act_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
497 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
499 struct rtnl_act *act, *p_act;
502 if (!(act = rtnl_act_alloc()))
505 if ((err = rtnl_act_msg_parse(nlh, &act)) < 0)
510 err = pp->pp_cb(OBJ_CAST(act), pp);
513 p_act = p_act->a_next;
521 static int act_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
523 struct tcamsg tcahdr = {
524 .tca_family = AF_UNSPEC,
531 static struct rtnl_tc_type_ops act_ops = {
532 .tt_type = RTNL_TC_TYPE_ACT,
533 .tt_dump_prefix =
"act",
539 static struct nl_cache_ops rtnl_act_ops = {
540 .co_name =
"route/act",
541 .co_hdrsize =
sizeof(
struct tcmsg),
543 { RTM_NEWACTION, NL_ACT_NEW,
"new" },
544 { RTM_DELACTION, NL_ACT_DEL,
"del" },
545 { RTM_GETACTION, NL_ACT_GET,
"get" },
546 END_OF_MSGTYPES_LIST,
548 .co_protocol = NETLINK_ROUTE,
549 .co_request_update = act_request_update,
550 .co_msg_parser = act_msg_parser,
551 .co_obj_ops = &act_obj_ops,
554 static struct nl_object_ops act_obj_ops = {
555 .oo_name =
"route/act",
556 .oo_size =
sizeof(
struct rtnl_act),
557 .oo_free_data = rtnl_tc_free_data,
558 .oo_clone = rtnl_tc_clone,
564 .oo_compare = rtnl_tc_compare,
565 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
568 static void __init act_init(
void)
570 rtnl_tc_type_register(&act_ops);
574 static void __exit act_exit(
void)
577 rtnl_tc_type_unregister(&act_ops);
Dump object briefly on one line.
struct nl_data * nl_data_alloc_attr(struct nlattr *nla)
Allocate abstract data object based on netlink attribute.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
Define the type of traffic control object.
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
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.
struct rtnl_link * rtnl_link_get(struct nl_cache *cache, int ifindex)
Lookup link in cache by interface index.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
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 nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
parse attributes of a netlink message
Dump all attributes but no statistics.
void rtnl_tc_set_link(struct rtnl_tc *tc, struct rtnl_link *link)
Set link of traffic control object.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
int rtnl_act_build_change_request(struct rtnl_act *act, int flags, struct nl_msg **result)
Build a netlink message to change action attributes.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
int nla_len(const struct nlattr *nla)
Return length of the payload .
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, struct nla_policy *policy)
Create attribute index based on a stream of attributes.
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 nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.
void * rtnl_tc_data(struct rtnl_tc *tc)
Return pointer to private data of traffic control object.
int rtnl_act_change(struct nl_sock *sk, struct rtnl_act *act, int flags)
Change an action.
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.
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
int rtnl_act_add(struct nl_sock *sk, struct rtnl_act *act, int flags)
Add/Update action.
void rtnl_link_put(struct rtnl_link *link)
Return a link object reference.
int rtnl_act_delete(struct nl_sock *sk, struct rtnl_act *act, int flags)
Delete action.
Dump all attributes including statistics.
size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
Copy string attribute payload to a buffer.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.