19 #include <netlink-private/netlink.h> 20 #include <netlink-private/tc.h> 21 #include <netlink/netlink.h> 22 #include <netlink/utils.h> 23 #include <netlink-private/route/tc-api.h> 24 #include <netlink/route/link.h> 25 #include <netlink/route/action.h> 28 static struct nl_object_ops act_obj_ops;
29 static struct nl_cache_ops rtnl_act_ops;
31 struct rtnl_act * rtnl_act_next(
struct rtnl_act *act)
40 int rtnl_act_append(
struct rtnl_act **head,
struct rtnl_act *
new)
42 struct rtnl_act *p_act;
51 while (p_act->a_next) {
53 p_act = p_act->a_next;
56 if (count > TCA_ACT_MAX_PRIO)
63 int rtnl_act_remove(
struct rtnl_act **head,
struct rtnl_act *act)
65 struct rtnl_act *a, **ap;
67 for (ap = head; (a = *ap) != NULL; ap = &a->a_next)
76 return -NLE_OBJ_NOTFOUND;
79 static int rtnl_act_fill_one(
struct nl_msg *msg,
struct rtnl_act *act,
int order)
81 struct rtnl_tc *tc =
TC_CAST(act);
82 struct rtnl_tc_ops *ops;
90 if (tc->ce_mask & TCA_ATTR_KIND)
93 ops = rtnl_tc_get_ops(tc);
94 if (ops && (ops->to_msg_fill || ops->to_msg_fill_raw)) {
98 if (ops->to_msg_fill) {
100 goto nla_put_failure;
102 if ((err = ops->to_msg_fill(tc, data, msg)) < 0)
103 goto nla_put_failure;
106 }
else if ((err = ops->to_msg_fill_raw(tc, data, msg)) < 0)
107 goto nla_put_failure;
116 int rtnl_act_fill(
struct nl_msg *msg,
int attrtype,
struct rtnl_act *act)
118 struct rtnl_act *p_act = act;
127 err = rtnl_act_fill_one(msg, p_act, ++order);
130 p_act = p_act->a_next;
137 static int rtnl_act_msg_build(
struct rtnl_act *act,
int type,
int flags,
138 struct nl_msg **result)
141 struct tcamsg tcahdr = {
142 .tca_family = AF_UNSPEC,
144 int err = -NLE_MSGSIZE;
150 if (
nlmsg_append(msg, &tcahdr,
sizeof(tcahdr), NLMSG_ALIGNTO) < 0)
151 goto nla_put_failure;
153 err = rtnl_act_fill(msg, TCA_ACT_TAB, act);
155 goto nla_put_failure;
165 static int act_build(
struct rtnl_act *act,
int type,
int flags,
166 struct nl_msg **result)
170 err = rtnl_act_msg_build(act, type, flags, result);
181 struct rtnl_act *rtnl_act_alloc(
void)
187 tc->tc_type = RTNL_TC_TYPE_ACT;
189 return (
struct rtnl_act *) tc;
192 void rtnl_act_get(
struct rtnl_act *act)
197 void rtnl_act_put(
struct rtnl_act *act)
224 struct nl_msg **result)
226 return act_build(act, RTM_NEWACTION, flags, result);
287 struct nl_msg **result)
289 return act_build(act, RTM_NEWACTION, NLM_F_REPLACE | flags, result);
330 struct nl_msg **result)
332 return act_build(act, RTM_DELACTION, flags, result);
379 static void act_dump_line(
struct rtnl_tc *tc,
struct nl_dump_params *p)
383 void rtnl_act_put_all(
struct rtnl_act **head)
385 struct rtnl_act *curr, *next;
396 int rtnl_act_parse(
struct rtnl_act **head,
struct nlattr *tb)
398 struct rtnl_act *act;
399 struct rtnl_tc_ops *ops;
400 struct nlattr *tb2[TCA_ACT_MAX + 1];
401 struct nlattr *nla[TCA_ACT_MAX_PRIO + 1];
402 char kind[TCKINDSIZ];
406 NLMSG_ALIGN(
nla_len(tb)), NULL);
410 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
416 act = rtnl_act_alloc();
427 if (tb2[TCA_ACT_KIND] == NULL) {
428 err = -NLE_MISSING_ATTR;
432 nla_strlcpy(kind, tb2[TCA_ACT_KIND],
sizeof(kind));
435 if (tb2[TCA_ACT_OPTIONS]) {
441 tc->ce_mask |= TCA_ATTR_OPTS;
444 ops = rtnl_tc_get_ops(tc);
445 if (ops && ops->to_msg_parser) {
453 err = ops->to_msg_parser(tc, data);
457 err = rtnl_act_append(head, act);
465 rtnl_act_put_all(head);
470 static int rtnl_act_msg_parse(
struct nlmsghdr *n,
struct rtnl_act **act)
472 struct rtnl_tc *tc =
TC_CAST(*act);
473 struct nl_cache *link_cache;
474 struct nlattr *tb[TCAA_MAX + 1];
478 tc->ce_msgtype = n->nlmsg_type;
480 err =
nlmsg_parse(n,
sizeof(*tm), tb, TCAA_MAX, NULL);
485 tc->tc_family = tm->tca_family;
487 if (tb[TCA_ACT_TAB] == NULL)
488 return -NLE_MISSING_ATTR;
490 err = rtnl_act_parse(act, tb[TCA_ACT_TAB]);
494 if ((link_cache = __nl_cache_mngt_require(
"route/link"))) {
507 static int act_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
508 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
510 struct rtnl_act *act, *p_act;
513 if (!(act = rtnl_act_alloc()))
516 if ((err = rtnl_act_msg_parse(nlh, &act)) < 0)
521 err = pp->pp_cb(OBJ_CAST(act), pp);
524 p_act = p_act->a_next;
532 static int act_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
534 struct tcamsg tcahdr = {
535 .tca_family = AF_UNSPEC,
542 static struct rtnl_tc_type_ops act_ops = {
543 .tt_type = RTNL_TC_TYPE_ACT,
544 .tt_dump_prefix =
"act",
550 static struct nl_cache_ops rtnl_act_ops = {
551 .co_name =
"route/act",
552 .co_hdrsize =
sizeof(
struct tcmsg),
554 { RTM_NEWACTION, NL_ACT_NEW,
"new" },
555 { RTM_DELACTION, NL_ACT_DEL,
"del" },
556 { RTM_GETACTION, NL_ACT_GET,
"get" },
557 END_OF_MSGTYPES_LIST,
559 .co_protocol = NETLINK_ROUTE,
560 .co_request_update = act_request_update,
561 .co_msg_parser = act_msg_parser,
562 .co_obj_ops = &act_obj_ops,
565 static struct nl_object_ops act_obj_ops = {
566 .oo_name =
"route/act",
567 .oo_size =
sizeof(
struct rtnl_act),
568 .oo_free_data = rtnl_tc_free_data,
569 .oo_clone = rtnl_tc_clone,
575 .oo_compare = rtnl_tc_compare,
576 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
579 static void __init act_init(
void)
581 rtnl_tc_type_register(&act_ops);
585 static void __exit act_exit(
void)
588 rtnl_tc_type_unregister(&act_ops);
Dump object briefly on one line.
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 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 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.
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 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.