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 struct rtnl_act * rtnl_act_next(
struct rtnl_act *act)
39 int rtnl_act_append(
struct rtnl_act **head,
struct rtnl_act *
new)
41 struct rtnl_act *p_act;
50 while (p_act->a_next) {
52 p_act = p_act->a_next;
55 if (count > TCA_ACT_MAX_PRIO)
62 int rtnl_act_remove(
struct rtnl_act **head,
struct rtnl_act *act)
64 struct rtnl_act *a, **ap;
66 for (ap = head; (a = *ap) != NULL; ap = &a->a_next)
75 return -NLE_OBJ_NOTFOUND;
78 static int rtnl_act_fill_one(
struct nl_msg *msg,
struct rtnl_act *act,
int order)
80 struct rtnl_tc *tc =
TC_CAST(act);
81 struct rtnl_tc_ops *ops;
89 if (tc->ce_mask & TCA_ATTR_KIND)
92 ops = rtnl_tc_get_ops(tc);
93 if (ops && (ops->to_msg_fill || ops->to_msg_fill_raw)) {
97 if (ops->to_msg_fill) {
101 if ((err = ops->to_msg_fill(tc, data, msg)) < 0)
102 goto nla_put_failure;
105 }
else if ((err = ops->to_msg_fill_raw(tc, data, msg)) < 0)
106 goto nla_put_failure;
115 int rtnl_act_fill(
struct nl_msg *msg,
int attrtype,
struct rtnl_act *act)
117 struct rtnl_act *p_act = act;
126 err = rtnl_act_fill_one(msg, p_act, ++order);
129 p_act = p_act->a_next;
136 static int rtnl_act_msg_build(
struct rtnl_act *act,
int type,
int flags,
137 struct nl_msg **result)
140 struct tcamsg tcahdr = {
141 .tca_family = AF_UNSPEC,
143 int err = -NLE_MSGSIZE;
149 if (
nlmsg_append(msg, &tcahdr,
sizeof(tcahdr), NLMSG_ALIGNTO) < 0)
150 goto nla_put_failure;
152 err = rtnl_act_fill(msg, TCA_ACT_TAB, act);
154 goto nla_put_failure;
164 static int act_build(
struct rtnl_act *act,
int type,
int flags,
165 struct nl_msg **result)
169 err = rtnl_act_msg_build(act, type, flags, result);
180 struct rtnl_act *rtnl_act_alloc(
void)
186 tc->tc_type = RTNL_TC_TYPE_ACT;
188 return (
struct rtnl_act *) tc;
191 void rtnl_act_get(
struct rtnl_act *act)
196 void rtnl_act_put(
struct rtnl_act *act)
223 struct nl_msg **result)
225 return act_build(act, RTM_NEWACTION, flags, result);
286 struct nl_msg **result)
288 return act_build(act, RTM_NEWACTION, NLM_F_REPLACE | flags, result);
329 struct nl_msg **result)
331 return act_build(act, RTM_DELACTION, flags, result);
378 static void act_dump_line(
struct rtnl_tc *tc,
struct nl_dump_params *p)
382 void rtnl_act_put_all(
struct rtnl_act **head)
384 struct rtnl_act *curr, *next;
395 int rtnl_act_parse(
struct rtnl_act **head,
struct nlattr *tb)
397 struct rtnl_act *act;
398 struct rtnl_tc_ops *ops;
399 struct nlattr *tb2[TCA_ACT_MAX + 1];
400 struct nlattr *nla[TCA_ACT_MAX_PRIO + 1];
401 char kind[TCKINDSIZ];
405 NLMSG_ALIGN(
nla_len(tb)), NULL);
409 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
415 act = rtnl_act_alloc();
426 if (tb2[TCA_ACT_KIND] == NULL) {
427 err = -NLE_MISSING_ATTR;
431 nla_strlcpy(kind, tb2[TCA_ACT_KIND],
sizeof(kind));
434 if (tb2[TCA_ACT_OPTIONS]) {
440 tc->ce_mask |= TCA_ATTR_OPTS;
443 ops = rtnl_tc_get_ops(tc);
444 if (ops && ops->to_msg_parser) {
452 err = ops->to_msg_parser(tc, data);
456 err = rtnl_act_append(head, act);
464 rtnl_act_put_all(head);
469 static int rtnl_act_msg_parse(
struct nlmsghdr *n,
struct rtnl_act **act)
471 struct rtnl_tc *tc =
TC_CAST(*act);
472 struct nl_cache *link_cache;
473 struct nlattr *tb[TCAA_MAX + 1];
477 tc->ce_msgtype = n->nlmsg_type;
479 err =
nlmsg_parse(n,
sizeof(*tm), tb, TCAA_MAX, NULL);
484 tc->tc_family = tm->tca_family;
486 if (tb[TCA_ACT_TAB] == NULL)
487 return -NLE_MISSING_ATTR;
489 err = rtnl_act_parse(act, tb[TCA_ACT_TAB]);
493 if ((link_cache = __nl_cache_mngt_require(
"route/link"))) {
506 static int act_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
507 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
509 struct rtnl_act *act, *p_act;
512 if (!(act = rtnl_act_alloc()))
515 if ((err = rtnl_act_msg_parse(nlh, &act)) < 0)
520 err = pp->pp_cb(OBJ_CAST(act), pp);
523 p_act = p_act->a_next;
531 static int act_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
533 struct tcamsg tcahdr = {
534 .tca_family = AF_UNSPEC,
541 static struct rtnl_tc_type_ops act_ops = {
542 .tt_type = RTNL_TC_TYPE_ACT,
543 .tt_dump_prefix =
"act",
549 static 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,
564 static 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),
578 static void __init act_init(
void)
580 rtnl_tc_type_register(&act_ops);
584 static void __exit act_exit(
void)
587 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.