123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580 |
- /*
- * lib/route/act.c Action
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation version 2.1
- * of the License.
- *
- * Copyright (c) 2013 Cong Wang <xiyou.wangcong@gmail.com>
- */
- /**
- * @ingroup tc
- * @defgroup act Action
- * @{
- */
- #include <netlink-private/netlink.h>
- #include <netlink-private/tc.h>
- #include <netlink/netlink.h>
- #include <netlink/utils.h>
- #include <netlink-private/route/tc-api.h>
- #include <netlink/route/link.h>
- static struct nl_object_ops act_obj_ops;
- static struct nl_cache_ops rtnl_act_ops;
- int rtnl_act_append(struct rtnl_act **head, struct rtnl_act *new)
- {
- struct rtnl_act *p_act;
- int count = 1;
- if (*head == NULL) {
- *head = new;
- return 0;
- }
- p_act = *head;
- while (p_act->a_next) {
- ++count;
- p_act = p_act->a_next;
- }
- if (count > TCA_ACT_MAX_PRIO)
- return -NLE_RANGE;
- p_act->a_next = new;
- return 0;
- }
- int rtnl_act_remove(struct rtnl_act **head, struct rtnl_act *act)
- {
- struct rtnl_act *a, **ap;
- for (ap = head; (a = *ap) != NULL; ap = &a->a_next)
- if (a == act)
- break;
- if (a) {
- *ap = a->a_next;
- a->a_next = NULL;
- return 0;
- }
- return -NLE_OBJ_NOTFOUND;
- }
- static int rtnl_act_fill_one(struct nl_msg *msg, struct rtnl_act *act, int order)
- {
- struct rtnl_tc *tc = TC_CAST(act);
- struct rtnl_tc_ops *ops;
- struct nlattr *nest;
- int err = -NLE_NOMEM;
- nest = nla_nest_start(msg, order);
- if (!nest)
- goto nla_put_failure;
- if (tc->ce_mask & TCA_ATTR_KIND)
- NLA_PUT_STRING(msg, TCA_ACT_KIND, tc->tc_kind);
- ops = rtnl_tc_get_ops(tc);
- if (ops && (ops->to_msg_fill || ops->to_msg_fill_raw)) {
- struct nlattr *opts;
- void *data = rtnl_tc_data(tc);
- if (ops->to_msg_fill) {
- if (!(opts = nla_nest_start(msg, TCA_ACT_OPTIONS)))
- goto nla_put_failure;
- if ((err = ops->to_msg_fill(tc, data, msg)) < 0)
- goto nla_put_failure;
- nla_nest_end(msg, opts);
- } else if ((err = ops->to_msg_fill_raw(tc, data, msg)) < 0)
- goto nla_put_failure;
- }
- nla_nest_end(msg, nest);
- return 0;
- nla_put_failure:
- return err;
- }
- int rtnl_act_fill(struct nl_msg *msg, int attrtype, struct rtnl_act *act)
- {
- struct rtnl_act *p_act = act;
- struct nlattr *nest;
- int err, order = 0;
- nest = nla_nest_start(msg, attrtype);
- if (!nest)
- return -NLE_MSGSIZE;
- while (p_act) {
- err = rtnl_act_fill_one(msg, p_act, ++order);
- if (err)
- return err;
- p_act = p_act->a_next;
- }
- nla_nest_end(msg, nest);
- return 0;
- }
- static int rtnl_act_msg_build(struct rtnl_act *act, int type, int flags,
- struct nl_msg **result)
- {
- struct nl_msg *msg;
- struct tcamsg tcahdr = {
- .tca_family = AF_UNSPEC,
- };
- int err = -NLE_MSGSIZE;
- msg = nlmsg_alloc_simple(type, flags);
- if (!msg)
- return -NLE_NOMEM;
- if (nlmsg_append(msg, &tcahdr, sizeof(tcahdr), NLMSG_ALIGNTO) < 0)
- goto nla_put_failure;
- err = rtnl_act_fill(msg, TCA_ACT_TAB, act);
- if (err < 0)
- goto nla_put_failure;
- *result = msg;
- return 0;
- nla_put_failure:
- nlmsg_free(msg);
- return err;
- }
- static int act_build(struct rtnl_act *act, int type, int flags,
- struct nl_msg **result)
- {
- int err;
- err = rtnl_act_msg_build(act, type, flags, result);
- if (err < 0)
- return err;
- return 0;
- }
- /**
- * @name Allocation/Freeing
- * @{
- */
- struct rtnl_act *rtnl_act_alloc(void)
- {
- struct rtnl_tc *tc;
- tc = TC_CAST(nl_object_alloc(&act_obj_ops));
- if (tc)
- tc->tc_type = RTNL_TC_TYPE_ACT;
- return (struct rtnl_act *) tc;
- }
- void rtnl_act_get(struct rtnl_act *act)
- {
- nl_object_get(OBJ_CAST(act));
- }
- void rtnl_act_put(struct rtnl_act *act)
- {
- nl_object_put((struct nl_object *) act);
- }
- /** @} */
- /**
- * @name Addition/Modification/Deletion
- * @{
- */
- /**
- * Build a netlink message requesting the addition of an action
- * @arg act Action to add
- * @arg flags Additional netlink message flags
- * @arg result Pointer to store resulting netlink message
- *
- * The behaviour of this function is identical to rtnl_act_add() with
- * the exception that it will not send the message but return it int the
- * provided return pointer instead.
- *
- * @see rtnl_act_add()
- *
- * @return 0 on success or a negative error code.
- */
- int rtnl_act_build_add_request(struct rtnl_act *act, int flags,
- struct nl_msg **result)
- {
- return act_build(act, RTM_NEWACTION, flags, result);
- }
- /**
- * Add/Update action
- * @arg sk Netlink socket
- * @arg act Action to add/update
- * @arg flags Additional netlink message flags
- *
- * Builds a \c RTM_NEWACTION netlink message requesting the addition
- * of a new action and sends the message to the kernel. The
- * configuration of the action is derived from the attributes of
- * the specified traffic class.
- *
- * The following flags may be specified:
- * - \c NLM_F_CREATE: Create action if it does not exist,
- * otherwise -NLE_OBJ_NOTFOUND is returned.
- * - \c NLM_F_EXCL: Return -NLE_EXISTS if an action with
- * matching handle exists already.
- *
- * Existing actions with matching handles will be updated, unless
- * the flag \c NLM_F_EXCL is specified. If no matching action
- * exists, it will be created if the flag \c NLM_F_CREATE is set,
- * otherwise the error -NLE_OBJ_NOTFOUND is returned.
- *
- * After sending, the function will wait for the ACK or an eventual
- * error message to be received and will therefore block until the
- * operation has been completed.
- *
- * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
- * this function to return immediately after sending. In this case,
- * it is the responsibility of the caller to handle any error
- * messages returned.
- *
- * @return 0 on success or a negative error code.
- */
- int rtnl_act_add(struct nl_sock *sk, struct rtnl_act *act, int flags)
- {
- struct nl_msg *msg;
- int err;
- if ((err = rtnl_act_build_add_request(act, flags, &msg)) < 0)
- return err;
- return nl_send_sync(sk, msg);
- }
- /**
- * Build a netlink message to change action attributes
- * @arg act Action to change
- * @arg flags additional netlink message flags
- * @arg result Pointer to store resulting message.
- *
- * Builds a new netlink message requesting a change of a neigh
- * attributes. The netlink message header isn't fully equipped with
- * all relevant fields and must thus be sent out via nl_send_auto_complete()
- * or supplemented as needed.
- *
- * @return 0 on success or a negative error code.
- */
- int rtnl_act_build_change_request(struct rtnl_act *act, int flags,
- struct nl_msg **result)
- {
- return act_build(act, RTM_NEWACTION, NLM_F_REPLACE | flags, result);
- }
- /**
- * Change an action
- * @arg sk Netlink socket.
- * @arg act action to change
- * @arg flags additional netlink message flags
- *
- * Builds a netlink message by calling rtnl_act_build_change_request(),
- * sends the request to the kernel and waits for the next ACK to be
- * received and thus blocks until the request has been processed.
- *
- * @return 0 on sucess or a negative error if an error occured.
- */
- int rtnl_act_change(struct nl_sock *sk, struct rtnl_act *act, int flags)
- {
- struct nl_msg *msg;
- int err;
- if ((err = rtnl_act_build_change_request(act, flags, &msg)) < 0)
- return err;
- return nl_send_sync(sk, msg);
- }
- /**
- * Build netlink message requesting the deletion of an action
- * @arg act Action to delete
- * @arg flags Additional netlink message flags
- * @arg result Pointer to store resulting netlink message
- *
- * The behaviour of this function is identical to rtnl_act_delete() with
- * the exception that it will not send the message but return it in the
- * provided return pointer instead.
- *
- * @see rtnl_act_delete()
- *
- * @return 0 on success or a negative error code.
- */
- int rtnl_act_build_delete_request(struct rtnl_act *act, int flags,
- struct nl_msg **result)
- {
- return act_build(act, RTM_DELACTION, flags, result);
- }
- /**
- * Delete action
- * @arg sk Netlink socket
- * @arg act Action to delete
- * @arg flags Additional netlink message flags
- *
- * Builds a \c RTM_DELACTION netlink message requesting the deletion
- * of an action and sends the message to the kernel.
- *
- * The message is constructed out of the following attributes:
- * - \c ifindex (required)
- * - \c prio (required)
- * - \c protocol (required)
- * - \c handle (required)
- * - \c parent (optional, if not specified parent equals root-qdisc)
- * - \c kind (optional, must match if provided)
- *
- * All other action attributes including all class type specific
- * attributes are ignored.
- *
- * After sending, the function will wait for the ACK or an eventual
- * error message to be received and will therefore block until the
- * operation has been completed.
- *
- * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
- * this function to return immediately after sending. In this case,
- * it is the responsibility of the caller to handle any error
- * messages returned.
- *
- * @return 0 on success or a negative error code.
- */
- int rtnl_act_delete(struct nl_sock *sk, struct rtnl_act *act, int flags)
- {
- struct nl_msg *msg;
- int err;
- if ((err = rtnl_act_build_delete_request(act, flags, &msg)) < 0)
- return err;
- return nl_send_sync(sk, msg);
- }
- /** @} */
- static void act_dump_line(struct rtnl_tc *tc, struct nl_dump_params *p)
- {
- }
- void rtnl_act_put_all(struct rtnl_act **head)
- {
- struct rtnl_act *curr, *next;
- curr = *head;
- while (curr) {
- next = curr->a_next;
- rtnl_act_put(curr);
- curr = next;
- }
- *head = NULL;
- }
- int rtnl_act_parse(struct rtnl_act **head, struct nlattr *tb)
- {
- struct rtnl_act *act;
- struct rtnl_tc_ops *ops;
- struct nlattr *tb2[TCA_ACT_MAX + 1];
- struct nlattr *nla[TCA_ACT_MAX_PRIO + 1];
- char kind[TCKINDSIZ];
- int err, i;
- err = nla_parse(nla, TCA_ACT_MAX_PRIO, nla_data(tb),
- NLMSG_ALIGN(nla_len(tb)), NULL);
- if (err < 0)
- return err;
- for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
- struct rtnl_tc *tc;
- if (nla[i] == NULL)
- continue;
- act = rtnl_act_alloc();
- if (!act) {
- err = -NLE_NOMEM;
- goto err_free;
- }
- tc = TC_CAST(act);
- err = nla_parse(tb2, TCA_ACT_MAX, nla_data(nla[i]),
- nla_len(nla[i]), NULL);
- if (err < 0)
- goto err_free;
- if (tb2[TCA_ACT_KIND] == NULL) {
- err = -NLE_MISSING_ATTR;
- goto err_free;
- }
- nla_strlcpy(kind, tb2[TCA_ACT_KIND], sizeof(kind));
- rtnl_tc_set_kind(tc, kind);
- if (tb2[TCA_ACT_OPTIONS]) {
- tc->tc_opts = nl_data_alloc_attr(tb2[TCA_ACT_OPTIONS]);
- if (!tc->tc_opts) {
- err = -NLE_NOMEM;
- goto err_free;
- }
- tc->ce_mask |= TCA_ATTR_OPTS;
- }
- ops = rtnl_tc_get_ops(tc);
- if (ops && ops->to_msg_parser) {
- void *data = rtnl_tc_data(tc);
- if (!data) {
- err = -NLE_NOMEM;
- goto err_free;
- }
- err = ops->to_msg_parser(tc, data);
- if (err < 0)
- goto err_free;
- }
- err = rtnl_act_append(head, act);
- if (err < 0)
- goto err_free;
- }
- return 0;
- err_free:
- rtnl_act_put (act);
- rtnl_act_put_all(head);
- return err;
- }
- static int rtnl_act_msg_parse(struct nlmsghdr *n, struct rtnl_act **act)
- {
- struct rtnl_tc *tc = TC_CAST(*act);
- struct nl_cache *link_cache;
- struct nlattr *tb[TCAA_MAX + 1];
- struct tcamsg *tm;
- int err;
- tc->ce_msgtype = n->nlmsg_type;
- err = nlmsg_parse(n, sizeof(*tm), tb, TCAA_MAX, NULL);
- if (err < 0)
- return err;
- tm = nlmsg_data(n);
- tc->tc_family = tm->tca_family;
- if (tb[TCA_ACT_TAB] == NULL)
- return -NLE_MISSING_ATTR;
- err = rtnl_act_parse(act, tb[TCA_ACT_TAB]);
- if (err < 0)
- return err;
- if ((link_cache = __nl_cache_mngt_require("route/link"))) {
- struct rtnl_link *link;
- if ((link = rtnl_link_get(link_cache, tc->tc_ifindex))) {
- rtnl_tc_set_link(tc, link);
- /* rtnl_tc_set_link incs refcnt */
- rtnl_link_put(link);
- }
- }
- return 0;
- }
- static int act_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
- struct nlmsghdr *nlh, struct nl_parser_param *pp)
- {
- struct rtnl_act *act, *p_act;
- int err;
- if (!(act = rtnl_act_alloc()))
- return -NLE_NOMEM;
- if ((err = rtnl_act_msg_parse(nlh, &act)) < 0)
- goto errout;
- p_act = act;
- while(p_act) {
- err = pp->pp_cb(OBJ_CAST(act), pp);
- if (err)
- break;
- p_act = p_act->a_next;
- }
- errout:
- rtnl_act_put(act);
- return err;
- }
- static int act_request_update(struct nl_cache *cache, struct nl_sock *sk)
- {
- struct tcamsg tcahdr = {
- .tca_family = AF_UNSPEC,
- };
- return nl_send_simple(sk, RTM_GETACTION, NLM_F_DUMP, &tcahdr,
- sizeof(tcahdr));
- }
- static struct rtnl_tc_type_ops act_ops = {
- .tt_type = RTNL_TC_TYPE_ACT,
- .tt_dump_prefix = "act",
- .tt_dump = {
- [NL_DUMP_LINE] = act_dump_line,
- },
- };
- static struct nl_cache_ops rtnl_act_ops = {
- .co_name = "route/act",
- .co_hdrsize = sizeof(struct tcmsg),
- .co_msgtypes = {
- { RTM_NEWACTION, NL_ACT_NEW, "new" },
- { RTM_DELACTION, NL_ACT_DEL, "del" },
- { RTM_GETACTION, NL_ACT_GET, "get" },
- END_OF_MSGTYPES_LIST,
- },
- .co_protocol = NETLINK_ROUTE,
- .co_request_update = act_request_update,
- .co_msg_parser = act_msg_parser,
- .co_obj_ops = &act_obj_ops,
- };
- static struct nl_object_ops act_obj_ops = {
- .oo_name = "route/act",
- .oo_size = sizeof(struct rtnl_act),
- .oo_free_data = rtnl_tc_free_data,
- .oo_clone = rtnl_tc_clone,
- .oo_dump = {
- [NL_DUMP_LINE] = rtnl_tc_dump_line,
- [NL_DUMP_DETAILS] = rtnl_tc_dump_details,
- [NL_DUMP_STATS] = rtnl_tc_dump_stats,
- },
- .oo_compare = rtnl_tc_compare,
- .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
- };
- static void __init act_init(void)
- {
- rtnl_tc_type_register(&act_ops);
- nl_cache_mngt_register(&rtnl_act_ops);
- }
- static void __exit act_exit(void)
- {
- nl_cache_mngt_unregister(&rtnl_act_ops);
- rtnl_tc_type_unregister(&act_ops);
- }
- /** @} */
|