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> 24 #include <netlink/route/action.h> 27 static struct nl_object_ops act_obj_ops;
28 static struct nl_cache_ops rtnl_act_ops;
30 int rtnl_act_append(
struct rtnl_act **head,
struct rtnl_act *
new)
32 struct rtnl_act *p_act;
41 while (p_act->a_next) {
43 p_act = p_act->a_next;
46 if (count > TCA_ACT_MAX_PRIO)
53 int rtnl_act_remove(
struct rtnl_act **head,
struct rtnl_act *act)
55 struct rtnl_act *a, **ap;
57 for (ap = head; (a = *ap) != NULL; ap = &a->a_next)
66 return -NLE_OBJ_NOTFOUND;
69 static int rtnl_act_fill_one(
struct nl_msg *msg,
struct rtnl_act *act,
int order)
71 struct rtnl_tc *tc =
TC_CAST(act);
72 struct rtnl_tc_ops *ops;
80 if (tc->ce_mask & TCA_ATTR_KIND)
83 ops = rtnl_tc_get_ops(tc);
84 if (ops && (ops->to_msg_fill || ops->to_msg_fill_raw)) {
88 if (ops->to_msg_fill) {
92 if ((err = ops->to_msg_fill(tc, data, msg)) < 0)
96 }
else if ((err = ops->to_msg_fill_raw(tc, data, msg)) < 0)
106 int rtnl_act_fill(
struct nl_msg *msg,
int attrtype,
struct rtnl_act *act)
108 struct rtnl_act *p_act = act;
117 err = rtnl_act_fill_one(msg, p_act, ++order);
120 p_act = p_act->a_next;
127 static int rtnl_act_msg_build(
struct rtnl_act *act,
int type,
int flags,
128 struct nl_msg **result)
131 struct tcamsg tcahdr = {
132 .tca_family = AF_UNSPEC,
134 int err = -NLE_MSGSIZE;
140 if (
nlmsg_append(msg, &tcahdr,
sizeof(tcahdr), NLMSG_ALIGNTO) < 0)
141 goto nla_put_failure;
143 err = rtnl_act_fill(msg, TCA_ACT_TAB, act);
145 goto nla_put_failure;
155 static int act_build(
struct rtnl_act *act,
int type,
int flags,
156 struct nl_msg **result)
160 err = rtnl_act_msg_build(act, type, flags, result);
171 struct rtnl_act *rtnl_act_alloc(
void)
177 tc->tc_type = RTNL_TC_TYPE_ACT;
179 return (
struct rtnl_act *) tc;
182 void rtnl_act_get(
struct rtnl_act *act)
187 void rtnl_act_put(
struct rtnl_act *act)
214 struct nl_msg **result)
216 return act_build(act, RTM_NEWACTION, flags, result);
277 struct nl_msg **result)
279 return act_build(act, RTM_NEWACTION, NLM_F_REPLACE | flags, result);
320 struct nl_msg **result)
322 return act_build(act, RTM_DELACTION, flags, result);
369 static void act_dump_line(
struct rtnl_tc *tc,
struct nl_dump_params *p)
373 void rtnl_act_put_all(
struct rtnl_act **head)
375 struct rtnl_act *curr, *next;
386 int rtnl_act_parse(
struct rtnl_act **head,
struct nlattr *tb)
388 struct rtnl_act *act;
389 struct rtnl_tc_ops *ops;
390 struct nlattr *tb2[TCA_ACT_MAX + 1];
391 struct nlattr *nla[TCA_ACT_MAX_PRIO + 1];
392 char kind[TCKINDSIZ];
396 NLMSG_ALIGN(
nla_len(tb)), NULL);
400 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
406 act = rtnl_act_alloc();
417 if (tb2[TCA_ACT_KIND] == NULL) {
418 err = -NLE_MISSING_ATTR;
422 nla_strlcpy(kind, tb2[TCA_ACT_KIND],
sizeof(kind));
425 if (tb2[TCA_ACT_OPTIONS]) {
431 tc->ce_mask |= TCA_ATTR_OPTS;
434 ops = rtnl_tc_get_ops(tc);
435 if (ops && ops->to_msg_parser) {
443 err = ops->to_msg_parser(tc, data);
447 err = rtnl_act_append(head, act);
455 rtnl_act_put_all(head);
460 static int rtnl_act_msg_parse(
struct nlmsghdr *n,
struct rtnl_act **act)
462 struct rtnl_tc *tc =
TC_CAST(*act);
463 struct nl_cache *link_cache;
464 struct nlattr *tb[TCAA_MAX + 1];
468 tc->ce_msgtype = n->nlmsg_type;
470 err =
nlmsg_parse(n,
sizeof(*tm), tb, TCAA_MAX, NULL);
475 tc->tc_family = tm->tca_family;
477 if (tb[TCA_ACT_TAB] == NULL)
478 return -NLE_MISSING_ATTR;
480 err = rtnl_act_parse(act, tb[TCA_ACT_TAB]);
484 if ((link_cache = __nl_cache_mngt_require(
"route/link"))) {
497 static int act_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
498 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
500 struct rtnl_act *act, *p_act;
503 if (!(act = rtnl_act_alloc()))
506 if ((err = rtnl_act_msg_parse(nlh, &act)) < 0)
511 err = pp->pp_cb(OBJ_CAST(act), pp);
514 p_act = p_act->a_next;
522 static int act_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
524 struct tcamsg tcahdr = {
525 .tca_family = AF_UNSPEC,
532 static struct rtnl_tc_type_ops act_ops = {
533 .tt_type = RTNL_TC_TYPE_ACT,
534 .tt_dump_prefix =
"act",
540 static struct nl_cache_ops rtnl_act_ops = {
541 .co_name =
"route/act",
542 .co_hdrsize =
sizeof(
struct tcmsg),
544 { RTM_NEWACTION, NL_ACT_NEW,
"new" },
545 { RTM_DELACTION, NL_ACT_DEL,
"del" },
546 { RTM_GETACTION, NL_ACT_GET,
"get" },
547 END_OF_MSGTYPES_LIST,
549 .co_protocol = NETLINK_ROUTE,
550 .co_request_update = act_request_update,
551 .co_msg_parser = act_msg_parser,
552 .co_obj_ops = &act_obj_ops,
555 static struct nl_object_ops act_obj_ops = {
556 .oo_name =
"route/act",
557 .oo_size =
sizeof(
struct rtnl_act),
558 .oo_free_data = rtnl_tc_free_data,
559 .oo_clone = rtnl_tc_clone,
565 .oo_compare = rtnl_tc_compare,
566 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
569 static void __init act_init(
void)
571 rtnl_tc_type_register(&act_ops);
575 static void __exit act_exit(
void)
578 rtnl_tc_type_unregister(&act_ops);
Dump object briefly on one line.
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
struct nl_data * nl_data_alloc_attr(const struct nlattr *nla)
Allocate abstract data object based on netlink attribute.
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.