summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/msg.c2
-rw-r--r--lib/object.c1
-rw-r--r--lib/route/addr.c3
-rw-r--r--lib/route/class.c78
-rw-r--r--lib/route/cls.c (renamed from lib/route/classifier.c)33
-rw-r--r--lib/route/cls/basic.c211
-rw-r--r--lib/route/cls/cgroup.c141
-rw-r--r--lib/route/cls/ematch.c410
-rw-r--r--lib/route/cls/ematch/cmp.c116
-rw-r--r--lib/route/cls/ematch/container.c39
-rw-r--r--lib/route/cls/fw.c96
-rw-r--r--lib/route/cls/u32.c127
-rw-r--r--lib/route/cls_obj.c59
13 files changed, 1126 insertions, 190 deletions
diff --git a/lib/msg.c b/lib/msg.c
index 22761a09..9fe9d541 100644
--- a/lib/msg.c
+++ b/lib/msg.c
@@ -284,7 +284,7 @@ int nlmsg_valid_hdr(const struct nlmsghdr *nlh, int hdrlen)
*/
int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
{
- return (remaining >= sizeof(struct nlmsghdr) &&
+ return (remaining >= (int)sizeof(struct nlmsghdr) &&
nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
nlh->nlmsg_len <= remaining);
}
diff --git a/lib/object.c b/lib/object.c
index fb44247b..46d81414 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -108,6 +108,7 @@ struct nl_object *nl_object_clone(struct nl_object *obj)
new->ce_ops = obj->ce_ops;
new->ce_msgtype = obj->ce_msgtype;
+ new->ce_mask = obj->ce_mask;
if (size)
memcpy((void *)new + doff, (void *)obj + doff, size);
diff --git a/lib/route/addr.c b/lib/route/addr.c
index fa9d0f41..d14c4811 100644
--- a/lib/route/addr.c
+++ b/lib/route/addr.c
@@ -907,8 +907,7 @@ static struct nl_object_ops addr_obj_ops = {
.oo_compare = addr_compare,
.oo_attrs2str = addr_attrs2str,
.oo_id_attrs = (ADDR_ATTR_FAMILY | ADDR_ATTR_IFINDEX |
- ADDR_ATTR_LOCAL | ADDR_ATTR_PREFIXLEN |
- ADDR_ATTR_PEER),
+ ADDR_ATTR_LOCAL | ADDR_ATTR_PREFIXLEN),
};
static struct nl_af_group addr_groups[] = {
diff --git a/lib/route/class.c b/lib/route/class.c
index 2bfd7dd8..ddf2d2e8 100644
--- a/lib/route/class.c
+++ b/lib/route/class.c
@@ -157,6 +157,60 @@ int rtnl_class_add(struct nl_sock *sk, struct rtnl_class *class, int flags)
return wait_for_ack(sk);
}
+int rtnl_class_build_delete_request(struct rtnl_class *class,
+ struct nl_msg **result)
+{
+ struct nl_msg *msg;
+ struct tcmsg tchdr;
+ int required = TCA_ATTR_IFINDEX | TCA_ATTR_PARENT;
+
+ if ((class->ce_mask & required) != required)
+ BUG();
+
+ msg = nlmsg_alloc_simple(RTM_DELTCLASS, 0);
+ if (!msg)
+ return -NLE_NOMEM;
+
+ tchdr.tcm_family = AF_UNSPEC;
+ tchdr.tcm_handle = class->c_handle;
+ tchdr.tcm_parent = class->c_parent;
+ tchdr.tcm_ifindex = class->c_ifindex;
+ if (nlmsg_append(msg, &tchdr, sizeof(tchdr), NLMSG_ALIGNTO) < 0) {
+ nlmsg_free(msg);
+ return -NLE_MSGSIZE;
+ }
+
+ *result = msg;
+ return 0;
+}
+
+/**
+ * Delete a class
+ * @arg sk Netlink socket.
+ * @arg class class to delete
+ *
+ * Builds a netlink message by calling rtnl_class_build_delete_request(),
+ * sends the request to the kernel and waits for the ACK to be
+ * received and thus blocks until the request has been processed.
+ *
+ * @return 0 on success or a negative error code
+ */
+int rtnl_class_delete(struct nl_sock *sk, struct rtnl_class *class)
+{
+ struct nl_msg *msg;
+ int err;
+
+ if ((err = rtnl_class_build_delete_request(class, &msg)) < 0)
+ return err;
+
+ err = nl_send_auto_complete(sk, msg);
+ nlmsg_free(msg);
+ if (err < 0)
+ return err;
+
+ return wait_for_ack(sk);
+}
+
/** @} */
/**
@@ -196,6 +250,30 @@ int rtnl_class_alloc_cache(struct nl_sock *sk, int ifindex,
return 0;
}
+/**
+ * Look up class by its handle in the provided cache
+ * @arg cache class cache
+ * @arg ifindex interface the class is attached to
+ * @arg handle class handle
+ * @return pointer to class inside the cache or NULL if no match was found.
+ */
+struct rtnl_class *rtnl_class_get(struct nl_cache *cache, int ifindex,
+ uint32_t handle)
+{
+ struct rtnl_class *class;
+
+ if (cache->c_ops != &rtnl_class_ops)
+ return NULL;
+
+ nl_list_for_each_entry(class, &cache->c_items, ce_list) {
+ if (class->c_handle == handle && class->c_ifindex == ifindex) {
+ nl_object_get((struct nl_object *) class);
+ return class;
+ }
+ }
+ return NULL;
+}
+
/** @} */
static struct nl_cache_ops rtnl_class_ops = {
diff --git a/lib/route/classifier.c b/lib/route/cls.c
index 4b5ce9d2..cbf03456 100644
--- a/lib/route/classifier.c
+++ b/lib/route/cls.c
@@ -6,7 +6,7 @@
* License as published by the Free Software Foundation version 2.1
* of the License.
*
- * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
+ * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
*/
/**
@@ -38,9 +38,9 @@ static struct nl_cache_ops rtnl_cls_ops;
static int cls_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
struct nlmsghdr *nlh, struct nl_parser_param *pp)
{
- int err;
- struct rtnl_cls *cls;
struct rtnl_cls_ops *cops;
+ struct rtnl_cls *cls;
+ int err;
cls = rtnl_cls_alloc();
if (!cls) {
@@ -57,11 +57,8 @@ static int cls_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
cls->c_protocol = ntohs(TC_H_MIN(cls->c_info));
cops = rtnl_cls_lookup_ops(cls);
- if (cops && cops->co_msg_parser) {
- err = cops->co_msg_parser(cls);
- if (err < 0)
- goto errout_free;
- }
+ if (cops && cops->co_msg_parser && (err = cops->co_msg_parser(cls)) < 0)
+ goto errout_free;
err = pp->pp_cb((struct nl_object *) cls, pp);
errout_free:
@@ -97,19 +94,23 @@ static int cls_build(struct rtnl_cls *cls, int type, int flags,
tchdr = nlmsg_data(nlmsg_hdr(*result));
prio = rtnl_cls_get_prio(cls);
proto = rtnl_cls_get_protocol(cls);
- tchdr->tcm_info = TC_H_MAKE(prio << 16, htons(proto)),
+ tchdr->tcm_info = TC_H_MAKE(prio << 16, htons(proto));
cops = rtnl_cls_lookup_ops(cls);
if (cops && cops->co_get_opts) {
struct nl_msg *opts;
-
- opts = cops->co_get_opts(cls);
- if (opts) {
- err = nla_put_nested(*result, TCA_OPTIONS, opts);
- nlmsg_free(opts);
- if (err < 0)
- goto errout;
+
+ if (!(opts = nlmsg_alloc())) {
+ err = -NLE_NOMEM;
+ goto errout;
}
+
+ if (!(err = cops->co_get_opts(cls, opts)))
+ err = nla_put_nested(*result, TCA_OPTIONS, opts);
+
+ nlmsg_free(opts);
+ if (err < 0)
+ goto errout;
}
return 0;
diff --git a/lib/route/cls/basic.c b/lib/route/cls/basic.c
new file mode 100644
index 00000000..1460b72c
--- /dev/null
+++ b/lib/route/cls/basic.c
@@ -0,0 +1,211 @@
+/*
+ * lib/route/cls/basic.c Basic Classifier
+ *
+ * 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) 2008-2009 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup cls
+ * @defgroup basic Basic Classifier
+ *
+ * @par Introduction
+ * The basic classifier is the simplest form of a classifier. It does
+ * not have any special classification capabilities, instead it can be
+ * used to classify exclusively based on extended matches or to
+ * create a "catch-all" filter.
+ *
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/route/classifier.h>
+#include <netlink/route/classifier-modules.h>
+#include <netlink/route/cls/basic.h>
+#include <netlink/route/cls/ematch.h>
+
+struct rtnl_basic
+{
+ uint32_t b_classid;
+ struct rtnl_ematch_tree * b_ematch;
+ int b_mask;
+};
+
+/** @cond SKIP */
+#define BASIC_ATTR_CLASSID 0x001
+#define BASIC_ATTR_EMATCH 0x002
+/** @endcond */
+
+static struct nla_policy basic_policy[TCA_FW_MAX+1] = {
+ [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
+ [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
+ [TCA_BASIC_ACT] = { .type = NLA_NESTED },
+ [TCA_BASIC_POLICE] = { .type = NLA_NESTED },
+};
+
+static int basic_clone(struct rtnl_cls *_dst, struct rtnl_cls *_src)
+{
+ return -NLE_OPNOTSUPP;
+}
+
+static void basic_free_data(struct rtnl_cls *cls)
+{
+ struct rtnl_basic *basic = rtnl_cls_data(cls);
+
+ rtnl_ematch_tree_free(basic->b_ematch);
+}
+
+static int basic_msg_parser(struct rtnl_cls *cls)
+{
+ struct nlattr *tb[TCA_BASIC_MAX + 1];
+ struct rtnl_basic *basic = rtnl_cls_data(cls);
+ int err;
+
+ err = tca_parse(tb, TCA_BASIC_MAX, (struct rtnl_tca *) cls, basic_policy);
+ if (err < 0)
+ return err;
+
+ if (tb[TCA_BASIC_CLASSID]) {
+ basic->b_classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
+ basic->b_mask |= BASIC_ATTR_CLASSID;
+ }
+
+ if (tb[TCA_BASIC_EMATCHES]) {
+ if ((err = rtnl_ematch_parse(tb[TCA_BASIC_EMATCHES],
+ &basic->b_ematch)) < 0)
+ return err;
+
+ if (basic->b_ematch)
+ basic->b_mask |= BASIC_ATTR_EMATCH;
+ }
+
+ if (tb[TCA_BASIC_ACT]) {
+ /* XXX */
+ }
+
+ if (tb[TCA_BASIC_POLICE]) {
+ /* XXX */
+ }
+
+ return 0;
+}
+
+static void basic_dump_line(struct rtnl_cls *cls, struct nl_dump_params *p)
+{
+ struct rtnl_basic *b = rtnl_cls_data(cls);
+ char buf[32];
+
+ if (b->b_mask & BASIC_ATTR_EMATCH)
+ nl_dump(p, " ematch");
+ else
+ nl_dump(p, " match-all");
+
+ if (b->b_mask & BASIC_ATTR_CLASSID)
+ nl_dump(p, " classify-to %s",
+ rtnl_tc_handle2str(b->b_classid, buf, sizeof(buf)));
+}
+
+static void basic_dump_details(struct rtnl_cls *cls, struct nl_dump_params *p)
+{
+ struct rtnl_basic *b = rtnl_cls_data(cls);
+
+ if (b->b_mask & BASIC_ATTR_EMATCH) {
+ nl_dump(p, "\n");
+ nl_dump_line(p, " ematch ");
+ rtnl_ematch_tree_dump(b->b_ematch, p);
+ } else
+ nl_dump(p, "no options.\n");
+}
+
+static int basic_get_opts(struct rtnl_cls *cls, struct nl_msg *msg)
+{
+ struct rtnl_basic *b = rtnl_cls_data(cls);
+
+ if (!(b->b_mask & BASIC_ATTR_CLASSID))
+ return -NLE_MISSING_ATTR;
+
+ NLA_PUT_U32(msg, TCA_BASIC_CLASSID, b->b_classid);
+
+ return 0;
+
+nla_put_failure:
+ return -NLE_NOMEM;
+}
+
+/**
+ * @name Attribute Modifications
+ * @{
+ */
+
+int rtnl_basic_set_classid(struct rtnl_cls *cls, uint32_t classid)
+{
+ struct rtnl_basic *b = rtnl_cls_data(cls);
+
+ b->b_classid = classid;
+ b->b_mask |= BASIC_ATTR_CLASSID;
+
+ return 0;
+}
+
+uint32_t rtnl_basic_get_classid(struct rtnl_cls *cls)
+{
+ struct rtnl_basic *b = rtnl_cls_data(cls);
+
+ return b->b_classid;
+}
+
+int rtnl_basic_set_ematch(struct rtnl_cls *cls, struct rtnl_ematch_tree *tree)
+{
+ struct rtnl_basic *b = rtnl_cls_data(cls);
+
+ if (b->b_ematch) {
+ rtnl_ematch_tree_free(b->b_ematch);
+ b->b_mask &= ~BASIC_ATTR_EMATCH;
+ }
+
+ b->b_ematch = tree;
+
+ if (tree)
+ b->b_mask |= BASIC_ATTR_EMATCH;
+
+ return 0;
+}
+
+struct rtnl_ematch_tree *rtnl_basic_get_ematch(struct rtnl_cls *cls)
+{
+ struct rtnl_basic *b = rtnl_cls_data(cls);
+ return b->b_ematch;
+}
+
+/** @} */
+
+static struct rtnl_cls_ops basic_ops = {
+ .co_kind = "basic",
+ .co_size = sizeof(struct rtnl_basic),
+ .co_msg_parser = basic_msg_parser,
+ .co_clone = basic_clone,
+ .co_free_data = basic_free_data,
+ .co_get_opts = basic_get_opts,
+ .co_dump = {
+ [NL_DUMP_LINE] = basic_dump_line,
+ [NL_DUMP_DETAILS] = basic_dump_details,
+ },
+};
+
+static void __init basic_init(void)
+{
+ rtnl_cls_register(&basic_ops);
+}
+
+static void __exit basic_exit(void)
+{
+ rtnl_cls_unregister(&basic_ops);
+}
+
+/** @} */
diff --git a/lib/route/cls/cgroup.c b/lib/route/cls/cgroup.c
new file mode 100644
index 00000000..e5f38b82
--- /dev/null
+++ b/lib/route/cls/cgroup.c
@@ -0,0 +1,141 @@
+/*
+ * lib/route/cls/cgroup.c Control Groups Classifier
+ *
+ * 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) 2009 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup cls_api
+ * @defgroup cgroup Control Groups Classifier
+ *
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/attr.h>
+#include <netlink/utils.h>
+#include <netlink/route/classifier.h>
+#include <netlink/route/classifier-modules.h>
+#include <netlink/route/cls/cgroup.h>
+#include <netlink/route/cls/ematch.h>
+
+/** @cond SKIP */
+#define CGROUP_ATTR_EMATCH 0x001
+/** @endcond */
+
+static struct nla_policy cgroup_policy[TCA_CGROUP_MAX+1] = {
+ [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
+};
+
+static void cgroup_free_data(struct rtnl_cls *cls)
+{
+ struct rtnl_cgroup *cg = rtnl_cls_data(cls);
+
+ rtnl_ematch_tree_free(cg->cg_ematch);
+}
+
+static int cgroup_msg_parser(struct rtnl_cls *cls)
+{
+ struct rtnl_cgroup *cg = rtnl_cls_data(cls);
+ struct nlattr *tb[TCA_CGROUP_MAX + 1];
+ int err;
+
+ err = tca_parse(tb, TCA_CGROUP_MAX, (struct rtnl_tca *) cls,
+ cgroup_policy);
+ if (err < 0)
+ return err;
+
+ if (tb[TCA_CGROUP_EMATCHES]) {
+ if ((err = rtnl_ematch_parse(tb[TCA_CGROUP_EMATCHES],
+ &cg->cg_ematch)) < 0)
+ return err;
+ cg->cg_mask |= CGROUP_ATTR_EMATCH;
+ }
+
+#if 0
+ TODO:
+ TCA_CGROUP_ACT,
+ TCA_CGROUP_POLICE,
+#endif
+
+ return 0;
+}
+
+static void cgroup_dump_line(struct rtnl_cls *cls, struct nl_dump_params *p)
+{
+ struct rtnl_cgroup *cg = rtnl_cls_data(cls);
+
+ if (cg->cg_mask & CGROUP_ATTR_EMATCH)
+ nl_dump(p, " ematch");
+ else
+ nl_dump(p, " match-all");
+}
+
+static void cgroup_dump_details(struct rtnl_cls *cls, struct nl_dump_params *p)
+{
+ struct rtnl_cgroup *cg = rtnl_cls_data(cls);
+
+ if (cg->cg_mask & CGROUP_ATTR_EMATCH) {
+ nl_dump(p, "\n");
+ nl_dump_line(p, " ematch ");
+ rtnl_ematch_tree_dump(cg->cg_ematch, p);
+ }
+}
+
+/**
+ * @name Attribute Modifications
+ * @{
+ */
+
+int rtnl_cgroup_set_ematch(struct rtnl_cls *cls, struct rtnl_ematch_tree *tree)
+{
+ struct rtnl_cgroup *cg = rtnl_cls_data(cls);
+
+ if (cg->cg_ematch) {
+ rtnl_ematch_tree_free(cg->cg_ematch);
+ cg->cg_mask &= ~CGROUP_ATTR_EMATCH;
+ }
+
+ cg->cg_ematch = tree;
+
+ if (tree)
+ cg->cg_mask |= CGROUP_ATTR_EMATCH;
+
+ return 0;
+}
+
+struct rtnl_ematch_tree *rtnl_cgroup_get_ematch(struct rtnl_cls *cls)
+{
+ struct rtnl_cgroup *cg = rtnl_cls_data(cls);
+ return cg->cg_ematch;
+}
+
+static struct rtnl_cls_ops cgroup_ops = {
+ .co_kind = "cgroup",
+ .co_size = sizeof(struct rtnl_cgroup),
+ .co_msg_parser = cgroup_msg_parser,
+ .co_free_data = cgroup_free_data,
+ .co_dump = {
+ [NL_DUMP_LINE] = cgroup_dump_line,
+ [NL_DUMP_DETAILS] = cgroup_dump_details,
+ },
+};
+
+static void __init cgroup_init(void)
+{
+ rtnl_cls_register(&cgroup_ops);
+}
+
+static void __exit cgroup_exit(void)
+{
+ rtnl_cls_unregister(&cgroup_ops);
+}
+
+/** @} */
diff --git a/lib/route/cls/ematch.c b/lib/route/cls/ematch.c
new file mode 100644
index 00000000..cb77b16d
--- /dev/null
+++ b/lib/route/cls/ematch.c
@@ -0,0 +1,410 @@
+/*
+ * lib/route/cls/ematch.c Extended Matches
+ *
+ * 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) 2008-2009 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup cls
+ * @defgroup ematch Extended Match
+ *
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/route/classifier.h>
+#include <netlink/route/classifier-modules.h>
+#include <netlink/route/cls/ematch.h>
+
+/**
+ * @name Module Registration
+ * @{
+ */
+
+static NL_LIST_HEAD(ematch_ops_list);
+
+/**
+ * Register ematch module
+ * @arg ops Module operations.
+ *
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_ematch_register(struct rtnl_ematch_ops *ops)
+{
+ if (rtnl_ematch_lookup_ops(ops->eo_kind))
+ return -NLE_EXIST;
+
+ nl_list_add_tail(&ops->eo_list, &ematch_ops_list);
+
+ return 0;
+}
+
+/**
+ * Unregister ematch module
+ * @arg ops Module operations.
+ *
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_ematch_unregister(struct rtnl_ematch_ops *ops)
+{
+ struct rtnl_ematch_ops *o;
+
+ nl_list_for_each_entry(o, &ematch_ops_list, eo_list) {
+ if (ops->eo_kind == o->eo_kind) {
+ nl_list_del(&o->eo_list);
+ return 0;
+ }
+ }
+
+ return -NLE_OBJ_NOTFOUND;
+}
+
+/**
+ * Lookup ematch module by kind
+ * @arg kind Module kind.
+ *
+ * @return Module operations or NULL if not found.
+ */
+struct rtnl_ematch_ops *rtnl_ematch_lookup_ops(int kind)
+{
+ struct rtnl_ematch_ops *ops;
+
+ nl_list_for_each_entry(ops, &ematch_ops_list, eo_list)
+ if (ops->eo_kind == kind)
+ return ops;
+
+ return NULL;
+}
+
+/**
+ * Lookup ematch module by name
+ * @arg name Name of ematch module.
+ *
+ * @return Module operations or NULL if not fuond.
+ */
+struct rtnl_ematch_ops *rtnl_ematch_lookup_ops_name(const char *name)
+{
+ struct rtnl_ematch_ops *ops;
+
+ nl_list_for_each_entry(ops, &ematch_ops_list, eo_list)
+ if (!strcasecmp(ops->eo_name, name))
+ return ops;
+
+ return NULL;
+}
+
+/** @} */
+
+/**
+ * @name Match
+ */
+
+struct rtnl_ematch *rtnl_ematch_alloc(struct rtnl_ematch_ops *ops)
+{
+ struct rtnl_ematch *e;
+ size_t len = sizeof(*e) + (ops ? ops->eo_datalen : 0);
+
+ if (!(e = calloc(1, len)))
+ return NULL;
+
+ NL_INIT_LIST_HEAD(&e->e_list);
+ NL_INIT_LIST_HEAD(&e->e_childs);
+
+ if (ops) {
+ e->e_ops = ops;
+ e->e_kind = ops->eo_kind;
+ }
+
+ return e;
+}
+
+/**
+ * Add ematch to the end of the parent's list of children.
+ * @arg parent Parent ematch.
+ * @arg child Ematch to be added as new child of parent.
+ */
+void rtnl_ematch_add_child(struct rtnl_ematch *parent,
+ struct rtnl_ematch *child)
+{
+ nl_list_add_tail(&child->e_list, &parent->e_childs);
+}
+
+/**
+ * Remove ematch from the list it is linked to.
+ * @arg ematch Ematch to be unlinked.
+ */
+void rtnl_ematch_unlink(struct rtnl_ematch *ematch)
+{
+ nl_list_del(&ematch->e_list);
+}
+
+void rtnl_ematch_free(struct rtnl_ematch *ematch)
+{
+ if (!ematch)
+ return;
+
+ free(ematch);
+}
+
+void rtnl_ematch_set_flags(struct rtnl_ematch *ematch, uint16_t flags)
+{
+ ematch->e_flags |= flags;
+}
+
+void rtnl_ematch_unset_flags(struct rtnl_ematch *ematch, uint16_t flags)
+{
+ ematch->e_flags &= ~flags;
+}
+
+uint16_t rtnl_ematch_get_flags(struct rtnl_ematch *ematch)
+{
+ return ematch->e_flags;
+}
+
+void *rtnl_ematch_data(struct rtnl_ematch *ematch)
+{
+ return ematch->e_data;
+}
+
+/** @} */
+
+/**
+ * @name Tree
+ */
+
+struct rtnl_ematch_tree *rtnl_ematch_tree_alloc(uint16_t progid)
+{
+ struct rtnl_ematch_tree *tree;
+
+ if (!(tree = calloc(1, sizeof(*tree))))
+ return NULL;
+
+ NL_INIT_LIST_HEAD(&tree->et_list);
+ tree->et_progid = progid;
+
+ return tree;
+}
+
+static void free_ematch_list(struct nl_list_head *head)
+{
+ struct rtnl_ematch *pos, *next;
+
+ nl_list_for_each_entry_safe(pos, next, head, e_list) {
+ if (!nl_list_empty(&pos->e_childs))
+ free_ematch_list(&pos->e_childs);
+ rtnl_ematch_free(pos);
+ }
+}
+
+void rtnl_ematch_tree_free(struct rtnl_ematch_tree *tree)
+{
+ if (!tree)
+ return;
+
+ free_ematch_list(&tree->et_list);
+ free(tree);
+}
+
+void rtnl_ematch_tree_add_tail(struct rtnl_ematch_tree *tree,
+ struct rtnl_ematch *ematch)
+{
+ nl_list_add_tail(&ematch->e_list, &tree->et_list);
+}
+
+static inline uint32_t container_ref(struct rtnl_ematch *ematch)
+{
+ return *((uint32_t *) rtnl_ematch_data(ematch));
+}
+
+static int link_tree(struct rtnl_ematch *index[], int nmatches, int pos,
+ struct nl_list_head *root)
+{
+ struct rtnl_ematch *ematch;
+ int i;
+
+ for (i = pos; i < nmatches; i++) {
+ ematch = index[i];
+
+ nl_list_add_tail(&ematch->e_list, root);
+
+ if (ematch->e_kind == TCF_EM_CONTAINER)
+ link_tree(index, nmatches, container_ref(ematch),
+ &ematch->e_childs);
+
+ if (!(ematch->e_flags & TCF_EM_REL_MASK))
+ return 0;
+ }
+
+ /* Last entry in chain can't possibly have no relation */
+ return -NLE_INVAL;
+}
+
+static struct nla_policy tree_policy[TCA_EMATCH_TREE_MAX+1] = {
+ [TCA_EMATCH_TREE_HDR] = { .minlen=sizeof(struct tcf_ematch_tree_hdr) },
+ [TCA_EMATCH_TREE_LIST] = { .type = NLA_NESTED },
+};
+
+/**
+ * Parse ematch netlink attributes
+ *
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_ematch_parse(struct nlattr *attr, struct rtnl_ematch_tree **result)
+{
+ struct nlattr *a, *tb[TCA_EMATCH_TREE_MAX+1];
+ struct tcf_ematch_tree_hdr *thdr;
+ struct rtnl_ematch_tree *tree;
+ struct rtnl_ematch **index;
+ int nmatches = 0, err, remaining;
+
+ err = nla_parse_nested(tb, TCA_EMATCH_TREE_MAX, attr, tree_policy);
+ if (err < 0)
+ return err;
+
+ if (!tb[TCA_EMATCH_TREE_HDR])
+ return -NLE_MISSING_ATTR;
+
+ thdr = nla_data(tb[TCA_EMATCH_TREE_HDR]);
+
+ /* Ignore empty trees */
+ if (thdr->nmatches == 0)
+ return 0;
+
+ if (!tb[TCA_EMATCH_TREE_LIST])
+ return -NLE_MISSING_ATTR;
+
+ if (thdr->nmatches > (nla_len(tb[TCA_EMATCH_TREE_LIST]) /
+ nla_total_size(sizeof(struct tcf_ematch_hdr))))
+ return -NLE_INVAL;
+
+ if (!(index = calloc(thdr->nmatches, sizeof(struct rtnl_ematch *))))
+ return -NLE_NOMEM;
+
+ if (!(tree = rtnl_ematch_tree_alloc(thdr->progid))) {
+ err = -NLE_NOMEM;
+ goto errout;
+ }
+
+ nla_for_each_nested(a, tb[TCA_EMATCH_TREE_LIST], remaining) {
+ struct rtnl_ematch_ops *ops;
+ struct tcf_ematch_hdr *hdr;
+ struct rtnl_ematch *ematch;
+ void *data;
+ size_t len;
+
+ if (nla_len(a) < sizeof(*hdr)) {
+ err = -NLE_INVAL;
+ goto errout;
+ }
+
+ if (nmatches >= thdr->nmatches) {
+ err = -NLE_RANGE;
+ goto errout;
+ }
+
+ hdr = nla_data(a);
+ data = nla_data(a) + NLA_ALIGN(sizeof(*hdr));
+ len = nla_len(a) - NLA_ALIGN(sizeof(*hdr));
+
+ ops = rtnl_ematch_lookup_ops(hdr->kind);
+ if (ops && ops->eo_datalen && len < ops->eo_datalen) {
+ err = -NLE_INVAL;
+ goto errout;
+ }
+
+ if (!(ematch = rtnl_ematch_alloc(ops))) {
+ err = -NLE_NOMEM;
+ goto errout;
+ }
+
+ ematch->e_id = hdr->matchid;
+ ematch->e_kind = hdr->kind;
+ ematch->e_flags = hdr->flags;
+
+ if (ops && (err = ops->eo_parse(ematch, data, len)) < 0)
+ goto errout;
+
+ if (hdr->kind == TCF_EM_CONTAINER &&
+ container_ref(ematch) >= thdr->nmatches) {
+ err = -NLE_INVAL;
+ goto errout;
+ }
+
+ index[nmatches++] = ematch;
+ }
+
+ if (nmatches != thdr->nmatches) {
+ err = -NLE_INVAL;
+ goto errout;
+ }
+
+ err = link_tree(index, nmatches, 0, &tree->et_list);
+ if (err < 0)
+ goto errout;
+
+ free(index);
+ *result = tree;
+
+ return 0;
+
+errout:
+ rtnl_ematch_tree_free(tree);
+ free(index);
+ return err;
+}
+
+static void dump_ematch_sequence(struct nl_list_head *head,
+ struct nl_dump_params *p)
+{
+ struct rtnl_ematch *match;
+
+ nl_list_for_each_entry(match, head, e_list) {
+ if (match->e_flags & TCF_EM_INVERT)
+ nl_dump(p, "NOT ");
+
+ if (match->e_kind == TCF_EM_CONTAINER) {
+ nl_dump(p, "(");
+ dump_ematch_sequence(&match->e_childs, p);
+ nl_dump(p, ")");
+ } else if (!match->e_ops) {
+ nl_dump(p, "[unknown ematch %d]", match->e_kind);
+ } else {
+ nl_dump(p, "%s(", match->e_ops->eo_name);
+
+ if (match->e_ops->eo_dump)
+ match->e_ops->eo_dump(match, p);
+
+ nl_dump(p, ")");
+ }
+
+ switch (match->e_flags & TCF_EM_REL_MASK) {
+ case TCF_EM_REL_AND:
+ nl_dump(p, " AND ");
+ break;
+ case TCF_EM_REL_OR:
+ nl_dump(p, " OR ");
+ break;
+ default:
+ /* end of first level ematch sequence */
+ return;
+ }
+ }
+}
+
+void rtnl_ematch_tree_dump(struct rtnl_ematch_tree *tree,
+ struct nl_dump_params *p)
+{
+ dump_ematch_sequence(&tree->et_list, p);
+ nl_dump(p, "\n");
+}
+
+/** @} */
+
+/** @} */
diff --git a/lib/route/cls/ematch/cmp.c b/lib/route/cls/ematch/cmp.c
new file mode 100644
index 00000000..ec25320f
--- /dev/null
+++ b/lib/route/cls/ematch/cmp.c
@@ -0,0 +1,116 @@
+/*
+ * lib/route/cls/ematch/cmp.c Simple packet data comparison ematch
+ *
+ * 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) 2008-2009 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup ematch
+ * @defgroup em_cmp Simple packet data comparison
+ *
+ * @{
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/route/cls/ematch.h>
+#include <linux/tc_ematch/tc_em_cmp.h>
+
+void rtnl_ematch_cmp_set(struct rtnl_ematch *ematch,
+ struct tcf_em_cmp *cfg)
+{
+ memcpy(rtnl_ematch_data(ematch), cfg, sizeof(*cfg));
+}
+
+struct tcf_em_cmp *rtnl_ematch_cmp_get(struct rtnl_ematch *ematch)
+{
+ return rtnl_ematch_data(ematch);
+}
+
+static const char *align_txt(struct tcf_em_cmp *cmp)
+{
+ switch (cmp->align) {
+ case TCF_EM_ALIGN_U8:
+ return "u8";
+ case TCF_EM_ALIGN_U16:
+ return (cmp->flags & TCF_EM_CMP_TRANS) ? "h16" : "u16";
+ case TCF_EM_ALIGN_U32:
+ return (cmp->flags & TCF_EM_CMP_TRANS) ? "h32" : "u32";
+ default:
+ return (cmp->flags & TCF_EM_CMP_TRANS) ? "h?" : "u?";
+ }
+}
+
+static const char *layer_txt(struct tcf_em_cmp *cmp)
+{
+ switch (cmp->layer) {
+ case TCF_LAYER_LINK:
+ return "link";
+ case TCF_LAYER_NETWORK:
+ return "network";
+ case TCF_LAYER_TRANSPORT:
+ return "transport";
+ default:
+ return "?";
+ }
+}
+
+static const char *relation_txt(struct tcf_em_cmp *cmp)
+{
+ switch (cmp->opnd) {
+ case TCF_EM_OPND_EQ:
+ return "eq";
+ case TCF_EM_OPND_LT:
+ return "lt";
+ case TCF_EM_OPND_GT:
+ return "gt";
+ default:
+ return "?";
+ }
+}
+
+static int cmp_parse(struct rtnl_ematch *m, void *data, size_t len)
+{
+ memcpy(rtnl_ematch_data(m), data, len);
+
+ return 0;
+}
+
+static void cmp_dump(struct rtnl_ematch *m, struct nl_dump_params *p)
+{
+ struct tcf_em_cmp *cmp = rtnl_ematch_data(m);
+
+ nl_dump(p, "%s at %s+%u ",
+ align_txt(cmp), layer_txt(cmp), cmp->off);
+
+ if (cmp->mask)
+ nl_dump(p, "& 0x%x ", cmp->mask);
+
+ nl_dump(p, "%s %u", relation_txt(cmp), cmp->val);
+}
+
+static struct rtnl_ematch_ops cmp_ops = {
+ .eo_kind = TCF_EM_CMP,
+ .eo_name = "cmp",
+ .eo_datalen = sizeof(struct tcf_em_cmp),
+ .eo_parse = cmp_parse,
+ .eo_dump = cmp_dump,
+};
+
+static void __init cmp_init(void)
+{
+ rtnl_ematch_register(&cmp_ops);
+}
+
+static void __exit cmp_exit(void)
+{
+ rtnl_ematch_unregister(&cmp_ops);
+}
+
+/** @} */
diff --git a/lib/route/cls/ematch/container.c b/lib/route/cls/ematch/container.c
new file mode 100644
index 00000000..54d836fe
--- /dev/null
+++ b/lib/route/cls/ematch/container.c
@@ -0,0 +1,39 @@
+/*
+ * lib/route/cls/ematch/container.c Container Ematch
+ *
+ * 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) 2008-2009 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include <netlink-local.h>
+#include <netlink-tc.h>
+#include <netlink/netlink.h>
+#include <netlink/route/cls/ematch.h>
+
+static int container_parse(struct rtnl_ematch *m, void *data, size_t len)
+{
+ memcpy(m->e_data, data, sizeof(uint32_t));
+
+ return 0;
+}
+
+static struct rtnl_ematch_ops container_ops = {
+ .eo_kind = TCF_EM_CONTAINER,
+ .eo_name = "container",
+ .eo_datalen = sizeof(uint32_t),
+ .eo_parse = container_parse,
+};
+
+static void __init container_init(void)
+{
+ rtnl_ematch_register(&container_ops);
+}
+
+static void __exit container_exit(void)
+{
+ rtnl_ematch_unregister(&container_ops);
+}
diff --git a/lib/route/cls/fw.c b/lib/route/cls/fw.c
index d18d3f8f..8cf25b97 100644
--- a/lib/route/cls/fw.c
+++ b/lib/route/cls/fw.c
@@ -6,7 +6,7 @@
* License as published by the Free Software Foundation version 2.1
* of the License.
*
- * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
+ * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
* Copyright (c) 2006 Petr Gotthard <petr.gotthard@siemens.com>
* Copyright (c) 2006 Siemens AG Oesterreich
*/
@@ -32,19 +32,6 @@
#define FW_ATTR_INDEV 0x008
/** @endcond */
-static inline struct rtnl_fw *fw_cls(struct rtnl_cls *cls)
-{
- return (struct rtnl_fw *) cls->c_subdata;
-}
-
-static inline struct rtnl_fw *fw_alloc(struct rtnl_cls *cls)
-{
- if (!cls->c_subdata)
- cls->c_subdata = calloc(1, sizeof(struct rtnl_fw));
-
- return fw_cls(cls);
-}
-
static struct nla_policy fw_policy[TCA_FW_MAX+1] = {
[TCA_FW_CLASSID] = { .type = NLA_U32 },
[TCA_FW_INDEV] = { .type = NLA_STRING,
@@ -53,18 +40,14 @@ static struct nla_policy fw_policy[TCA_FW_MAX+1] = {
static int fw_msg_parser(struct rtnl_cls *cls)
{
- int err;
+ struct rtnl_fw *f = rtnl_cls_data(cls);
struct nlattr *tb[TCA_FW_MAX + 1];
- struct rtnl_fw *f;
+ int err;
err = tca_parse(tb, TCA_FW_MAX, (struct rtnl_tca *) cls, fw_policy);
if (err < 0)
return err;
- f = fw_alloc(cls);
- if (!f)
- return -NLE_NOMEM;
-
if (tb[TCA_FW_CLASSID]) {
f->cf_classid = nla_get_u32(tb[TCA_FW_CLASSID]);
f->cf_mask |= FW_ATTR_CLASSID;
@@ -94,47 +77,31 @@ static int fw_msg_parser(struct rtnl_cls *cls)
static void fw_free_data(struct rtnl_cls *cls)
{
- struct rtnl_fw *f = fw_cls(cls);
-
- if (!f)
- return;
+ struct rtnl_fw *f = rtnl_cls_data(cls);
nl_data_free(f->cf_act);
nl_data_free(f->cf_police);
-
- free(cls->c_subdata);
}
static int fw_clone(struct rtnl_cls *_dst, struct rtnl_cls *_src)
{
- struct rtnl_fw *dst, *src = fw_cls(_src);
-
- if (!src)
- return 0;
+ struct rtnl_fw *dst = rtnl_cls_data(_dst);
+ struct rtnl_fw *src = rtnl_cls_data(_src);
- dst = fw_alloc(_dst);
- if (!dst)
+ if (src->cf_act && !(dst->cf_act = nl_data_clone(src->cf_act)))
return -NLE_NOMEM;
-
- if (src->cf_act)
- if (!(dst->cf_act = nl_data_clone(src->cf_act)))
- return -NLE_NOMEM;
- if (src->cf_police)
- if (!(dst->cf_police = nl_data_clone(src->cf_police)))
- return -NLE_NOMEM;
+ if (src->cf_police && !(dst->cf_police = nl_data_clone(src->cf_police)))
+ return -NLE_NOMEM;
return 0;
}
static void fw_dump_line(struct rtnl_cls *cls, struct nl_dump_params *p)
{
- struct rtnl_fw *f = fw_cls(cls);
+ struct rtnl_fw *f = rtnl_cls_data(cls);
char buf[32];
- if (!f)
- return;
-
if (f->cf_mask & FW_ATTR_CLASSID)
nl_dump(p, " target %s",
rtnl_tc_handle2str(f->cf_classid, buf, sizeof(buf)));
@@ -142,45 +109,32 @@ static void fw_dump_line(struct rtnl_cls *cls, struct nl_dump_params *p)
static void fw_dump_details(struct rtnl_cls *cls, struct nl_dump_params *p)
{
- struct rtnl_fw *f = fw_cls(cls);
-
- if (!f)
- return;
+ struct rtnl_fw *f = rtnl_cls_data(cls);
if (f->cf_mask & FW_ATTR_INDEV)
nl_dump(p, "indev %s ", f->cf_indev);
}
-static void fw_dump_stats(struct rtnl_cls *cls, struct nl_dump_params *p)
-{
-}
-
-static struct nl_msg *fw_get_opts(struct rtnl_cls *cls)
+static int fw_get_opts(struct rtnl_cls *cls, struct nl_msg *msg)
{
- struct rtnl_fw *f;
- struct nl_msg *msg;
+ struct rtnl_fw *f = rtnl_cls_data(cls);
- f = fw_cls(cls);
- if (!f)
- return NULL;
-
- msg = nlmsg_alloc();
- if (!msg)
- return NULL;
-
if (f->cf_mask & FW_ATTR_CLASSID)
- nla_put_u32(msg, TCA_FW_CLASSID, f->cf_classid);
+ NLA_PUT_U32(msg, TCA_FW_CLASSID, f->cf_classid);
if (f->cf_mask & FW_ATTR_ACTION)
- nla_put_data(msg, TCA_FW_ACT, f->cf_act);
+ NLA_PUT_DATA(msg, TCA_FW_ACT, f->cf_act);
if (f->cf_mask & FW_ATTR_POLICE)
- nla_put_data(msg, TCA_FW_POLICE, f->cf_police);
+ NLA_PUT_DATA(msg, TCA_FW_POLICE, f->cf_police);
if (f->cf_mask & FW_ATTR_INDEV)
- nla_put_string(msg, TCA_FW_INDEV, f->cf_indev);
+ NLA_PUT_STRING(msg, TCA_FW_INDEV, f->cf_indev);
- return msg;
+ return 0;
+
+nla_put_failure:
+ return -NLE_NOMEM;
}
/**
@@ -190,12 +144,8 @@ static struct nl_msg *fw_get_opts(struct rtnl_cls *cls)
int rtnl_fw_set_classid(struct rtnl_cls *cls, uint32_t classid)
{
- struct rtnl_fw *f;
+ struct rtnl_fw *f = rtnl_cls_data(cls);
- f = fw_alloc(cls);
- if (!f)
- return -NLE_NOMEM;
-
f->cf_classid = classid;
f->cf_mask |= FW_ATTR_CLASSID;
@@ -206,6 +156,7 @@ int rtnl_fw_set_classid(struct rtnl_cls *cls, uint32_t classid)
static struct rtnl_cls_ops fw_ops = {
.co_kind = "fw",
+ .co_size = sizeof(struct rtnl_fw),
.co_msg_parser = fw_msg_parser,
.co_free_data = fw_free_data,
.co_clone = fw_clone,
@@ -213,7 +164,6 @@ static struct rtnl_cls_ops fw_ops = {
.co_dump = {
[NL_DUMP_LINE] = fw_dump_line,
[NL_DUMP_DETAILS] = fw_dump_details,
- [NL_DUMP_STATS] = fw_dump_stats,
},
};
diff --git a/lib/route/cls/u32.c b/lib/route/cls/u32.c
index cf02cdfe..46d502bc 100644
--- a/lib/route/cls/u32.c
+++ b/lib/route/cls/u32.c
@@ -6,7 +6,7 @@
* License as published by the Free Software Foundation version 2.1
* of the License.
*
- * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
+ * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
* Copyright (c) 2005-2006 Petr Gotthard <petr.gotthard@siemens.com>
* Copyright (c) 2005-2006 Siemens AG Oesterreich
*/
@@ -40,19 +40,6 @@
#define U32_ATTR_INDEV 0x100
/** @endcond */
-static inline struct rtnl_u32 *u32_cls(struct rtnl_cls *cls)
-{
- return (struct rtnl_u32 *) cls->c_subdata;
-}
-
-static inline struct rtnl_u32 *u32_alloc(struct rtnl_cls *cls)
-{
- if (!cls->c_subdata)
- cls->c_subdata = calloc(1, sizeof(struct rtnl_u32));
-
- return u32_cls(cls);
-}
-
static inline struct tc_u32_sel *u32_selector(struct rtnl_u32 *u)
{
return (struct tc_u32_sel *) u->cu_selector->d_data;
@@ -79,18 +66,14 @@ static struct nla_policy u32_policy[TCA_U32_MAX+1] = {
static int u32_msg_parser(struct rtnl_cls *cls)
{
- int err;
+ struct rtnl_u32 *u = rtnl_cls_data(cls);
struct nlattr *tb[TCA_U32_MAX + 1];
- struct rtnl_u32 *u;
+ int err;
err = tca_parse(tb, TCA_U32_MAX, (struct rtnl_tca *) cls, u32_policy);
if (err < 0)
return err;
- u = u32_alloc(cls);
- if (!u)
- goto errout_nomem;
-
if (tb[TCA_U32_DIVISOR]) {
u->cu_divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
u->cu_mask |= U32_ATTR_DIVISOR;
@@ -170,57 +153,40 @@ errout:
static void u32_free_data(struct rtnl_cls *cls)
{
- struct rtnl_u32 *u = u32_cls(cls);
-
- if (!u)
- return;
+ struct rtnl_u32 *u = rtnl_cls_data(cls);
nl_data_free(u->cu_selector);
nl_data_free(u->cu_act);
nl_data_free(u->cu_police);
nl_data_free(u->cu_pcnt);
-
- free(cls->c_subdata);
}
static int u32_clone(struct rtnl_cls *_dst, struct rtnl_cls *_src)
{
- struct rtnl_u32 *dst, *src = u32_cls(_src);
+ struct rtnl_u32 *dst = rtnl_cls_data(_dst);
+ struct rtnl_u32 *src = rtnl_cls_data(_src);
- if (!src)
- return 0;
-
- dst = u32_alloc(_dst);
- if (!dst)
+ if (src->cu_selector &&
+ !(dst->cu_selector = nl_data_clone(src->cu_selector)))
return -NLE_NOMEM;
- if (src->cu_selector)
- if (!(dst->cu_selector = nl_data_clone(src->cu_selector)))
- return -NLE_NOMEM;
-
- if (src->cu_act)
- if (!(dst->cu_act = nl_data_clone(src->cu_act)))
- return -NLE_NOMEM;
+ if (src->cu_act && !(dst->cu_act = nl_data_clone(src->cu_act)))
+ return -NLE_NOMEM;
- if (src->cu_police)
- if (!(dst->cu_police = nl_data_clone(src->cu_police)))
- return -NLE_NOMEM;
+ if (src->cu_police && !(dst->cu_police = nl_data_clone(src->cu_police)))
+ return -NLE_NOMEM;
- if (src->cu_pcnt)
- if (!(dst->cu_pcnt = nl_data_clone(src->cu_pcnt)))
- return -NLE_NOMEM;
+ if (src->cu_pcnt && !(dst->cu_pcnt = nl_data_clone(src->cu_pcnt)))
+ return -NLE_NOMEM;
return 0;
}
static void u32_dump_line(struct rtnl_cls *cls, struct nl_dump_params *p)
{
- struct rtnl_u32 *u = u32_cls(cls);
+ struct rtnl_u32 *u = rtnl_cls_data(cls);
char buf[32];
- if (!u)
- return;
-
if (u->cu_mask & U32_ATTR_DIVISOR)
nl_dump(p, " divisor %u", u->cu_divisor);
else if (u->cu_mask & U32_ATTR_CLASSID)
@@ -289,12 +255,9 @@ static void print_selector(struct nl_dump_params *p, struct tc_u32_sel *sel,
static void u32_dump_details(struct rtnl_cls *cls, struct nl_dump_params *p)
{
- struct rtnl_u32 *u = u32_cls(cls);
+ struct rtnl_u32 *u = rtnl_cls_data(cls);
struct tc_u32_sel *s;
- if (!u)
- return;
-
if (!(u->cu_mask & U32_ATTR_SELECTOR)) {
nl_dump(p, "no-selector\n");
return;
@@ -328,10 +291,7 @@ static void u32_dump_details(struct rtnl_cls *cls, struct nl_dump_params *p)
static void u32_dump_stats(struct rtnl_cls *cls, struct nl_dump_params *p)
{
- struct rtnl_u32 *u = u32_cls(cls);
-
- if (!u)
- return;
+ struct rtnl_u32 *u = rtnl_cls_data(cls);
if (u->cu_mask & U32_ATTR_PCNT) {
struct tc_u32_pcnt *pc = u->cu_pcnt->d_data;
@@ -342,44 +302,38 @@ static void u32_dump_stats(struct rtnl_cls *cls, struct nl_dump_params *p)
}
}
-static struct nl_msg *u32_get_opts(struct rtnl_cls *cls)
+static int u32_get_opts(struct rtnl_cls *cls, struct nl_msg *msg)
{
- struct rtnl_u32 *u;
- struct nl_msg *msg;
+ struct rtnl_u32 *u = rtnl_cls_data(cls);
- u = u32_cls(cls);
- if (!u)
- return NULL;
-
- msg = nlmsg_alloc();
- if (!msg)
- return NULL;
-
if (u->cu_mask & U32_ATTR_DIVISOR)
- nla_put_u32(msg, TCA_U32_DIVISOR, u->cu_divisor);
+ NLA_PUT_U32(msg, TCA_U32_DIVISOR, u->cu_divisor);
if (u->cu_mask & U32_ATTR_HASH)
- nla_put_u32(msg, TCA_U32_HASH, u->cu_hash);
+ NLA_PUT_U32(msg, TCA_U32_HASH, u->cu_hash);
if (u->cu_mask & U32_ATTR_CLASSID)
- nla_put_u32(msg, TCA_U32_CLASSID, u->cu_classid);
+ NLA_PUT_U32(msg, TCA_U32_CLASSID, u->cu_classid);
if (u->cu_mask & U32_ATTR_LINK)
- nla_put_u32(msg, TCA_U32_LINK, u->cu_link);
+ NLA_PUT_U32(msg, TCA_U32_LINK, u->cu_link);
if (u->cu_mask & U32_ATTR_SELECTOR)
- nla_put_data(msg, TCA_U32_SEL, u->cu_selector);
+ NLA_PUT_DATA(msg, TCA_U32_SEL, u->cu_selector);
if (u->cu_mask & U32_ATTR_ACTION)
- nla_put_data(msg, TCA_U32_ACT, u->cu_act);
+ NLA_PUT_DATA(msg, TCA_U32_ACT, u->cu_act);
if (u->cu_mask & U32_ATTR_POLICE)
- nla_put_data(msg, TCA_U32_POLICE, u->cu_police);
+ NLA_PUT_DATA(msg, TCA_U32_POLICE, u->cu_police);
if (u->cu_mask & U32_ATTR_INDEV)
- nla_put_string(msg, TCA_U32_INDEV, u->cu_indev);
+ NLA_PUT_STRING(msg, TCA_U32_INDEV, u->cu_indev);
- return msg;
+ return 0;
+
+nla_put_failure:
+ return -NLE_NOMEM;
}
/**
@@ -397,12 +351,8 @@ void rtnl_u32_set_handle(struct rtnl_cls *cls, int htid, int hash,
int rtnl_u32_set_classid(struct rtnl_cls *cls, uint32_t classid)
{
- struct rtnl_u32 *u;
+ struct rtnl_u32 *u = rtnl_cls_data(cls);
- u = u32_alloc(cls);
- if (!u)
- return -NLE_NOMEM;
-
u->cu_classid = classid;
u->cu_mask |= U32_ATTR_CLASSID;
@@ -419,11 +369,7 @@ int rtnl_u32_set_classid(struct rtnl_cls *cls, uint32_t classid)
int rtnl_u32_set_flags(struct rtnl_cls *cls, int flags)
{
struct tc_u32_sel *sel;
- struct rtnl_u32 *u;
-
- u = u32_alloc(cls);
- if (!u)
- return -NLE_NOMEM;
+ struct rtnl_u32 *u = rtnl_cls_data(cls);
sel = u32_selector_alloc(u);
if (!sel)
@@ -453,13 +399,9 @@ int rtnl_u32_add_key(struct rtnl_cls *cls, uint32_t val, uint32_t mask,
int off, int offmask)
{
struct tc_u32_sel *sel;
- struct rtnl_u32 *u;
+ struct rtnl_u32 *u = rtnl_cls_data(cls);
int err;
- u = u32_alloc(cls);
- if (!u)
- return -NLE_NOMEM;
-
sel = u32_selector_alloc(u);
if (!sel)
return -NLE_NOMEM;
@@ -562,6 +504,7 @@ int rtnl_u32_add_key_in6_addr(struct rtnl_cls *cls, struct in6_addr *addr,
static struct rtnl_cls_ops u32_ops = {
.co_kind = "u32",
+ .co_size = sizeof(struct rtnl_u32),
.co_msg_parser = u32_msg_parser,
.co_free_data = u32_free_data,
.co_clone = u32_clone,
diff --git a/lib/route/cls_obj.c b/lib/route/cls_obj.c
index 217b6d08..c8218c07 100644
--- a/lib/route/cls_obj.c
+++ b/lib/route/cls_obj.c
@@ -39,6 +39,8 @@ static void cls_free_data(struct nl_object *obj)
cops = rtnl_cls_lookup_ops(cls);
if (cops && cops->co_free_data)
cops->co_free_data(cls);
+
+ nl_data_free(cls->c_subdata);
}
static int cls_clone(struct nl_object *_dst, struct nl_object *_src)
@@ -52,6 +54,13 @@ static int cls_clone(struct nl_object *_dst, struct nl_object *_src)
if (err < 0)
goto errout;
+ if (src->c_subdata) {
+ if (!(dst->c_subdata = nl_data_clone(src->c_subdata))) {
+ err = -NLE_NOMEM;
+ goto errout;
+ }
+ }
+
cops = rtnl_cls_lookup_ops(src);
if (cops && cops->co_clone)
err = cops->co_clone(dst, src);
@@ -133,6 +142,11 @@ void rtnl_cls_set_ifindex(struct rtnl_cls *f, int ifindex)
tca_set_ifindex((struct rtnl_tca *) f, ifindex);
}
+int rtnl_cls_get_ifindex(struct rtnl_cls *cls)
+{
+ return cls->c_ifindex;
+}
+
void rtnl_cls_set_handle(struct rtnl_cls *f, uint32_t handle)
{
tca_set_handle((struct rtnl_tca *) f, handle);
@@ -143,14 +157,21 @@ void rtnl_cls_set_parent(struct rtnl_cls *f, uint32_t parent)
tca_set_parent((struct rtnl_tca *) f, parent);
}
-int rtnl_cls_set_kind(struct rtnl_cls *f, const char *kind)
+uint32_t rtnl_cls_get_parent(struct rtnl_cls *cls)
{
- tca_set_kind((struct rtnl_tca *) f, kind);
+ return cls->c_parent;
+}
+
+int rtnl_cls_set_kind(struct rtnl_cls *cls, const char *kind)
+{
+ if (cls->ce_mask & TCA_ATTR_KIND)
+ return -NLE_EXIST;
+
+ tca_set_kind((struct rtnl_tca *) cls, kind);
+
+ /* Force allocation of data */
+ rtnl_cls_data(cls);
- f->c_ops = __rtnl_cls_lookup_ops(kind);
- if (f->c_ops == NULL)
- return -NLE_OBJ_NOTFOUND;
-
return 0;
}
@@ -187,6 +208,32 @@ uint16_t rtnl_cls_get_protocol(struct rtnl_cls *cls)
return ETH_P_ALL;
}
+void *rtnl_cls_data(struct rtnl_cls *cls)
+{
+ if (!cls->c_subdata) {
+ struct rtnl_cls_ops *ops = cls->c_ops;
+
+ if (!ops) {
+ if (!cls->c_kind[0])
+ BUG();
+
+ ops = __rtnl_cls_lookup_ops(cls->c_kind);
+ if (ops == NULL)
+ return NULL;
+
+ cls->c_ops = ops;
+ }
+
+ if (!ops->co_size)
+ BUG();
+
+ if (!(cls->c_subdata = nl_data_alloc(NULL, ops->co_size)))
+ return NULL;
+ }
+
+ return nl_data_get(cls->c_subdata);
+}
+
/** @} */
struct nl_object_ops cls_obj_ops = {