summaryrefslogtreecommitdiffstats
path: root/src/expr
diff options
context:
space:
mode:
authorPablo Neira Ayuso <pablo@netfilter.org>2013-06-13 13:33:08 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2013-06-13 13:33:08 +0200
commitfaa855f4deaded25781a41af19ebfe82fa77bfc0 (patch)
tree274bd0d68db1704693c17320c94544786a1738ac /src/expr
parentd173bc61c4c64952b548cd14a6e0bcfdbc677d9f (diff)
expr: add limit
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/expr')
-rw-r--r--src/expr/limit.c213
1 files changed, 213 insertions, 0 deletions
diff --git a/src/expr/limit.c b/src/expr/limit.c
new file mode 100644
index 0000000..64a5b70
--- /dev/null
+++ b/src/expr/limit.c
@@ -0,0 +1,213 @@
+/*
+ * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <string.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <linux/netfilter/nf_tables.h>
+
+#include "internal.h"
+#include <libmnl/libmnl.h>
+#include <libnftables/expr.h>
+#include <libnftables/rule.h>
+#include "expr_ops.h"
+
+struct nft_expr_limit {
+ uint64_t rate;
+ uint64_t depth;
+};
+
+static int
+nft_rule_expr_limit_set(struct nft_rule_expr *e, uint16_t type,
+ const void *data, size_t data_len)
+{
+ struct nft_expr_limit *limit = (struct nft_expr_limit *)e->data;
+
+ switch(type) {
+ case NFT_EXPR_LIMIT_RATE:
+ limit->rate = *((uint64_t *)data);
+ break;
+ case NFT_EXPR_LIMIT_DEPTH:
+ limit->depth = *((uint64_t *)data);
+ break;
+ default:
+ return -1;
+ }
+ return 0;
+}
+
+static const void *
+nft_rule_expr_limit_get(struct nft_rule_expr *e, uint16_t type, size_t *data_len)
+{
+ struct nft_expr_limit *limit = (struct nft_expr_limit *)e->data;
+
+ switch(type) {
+ case NFT_EXPR_LIMIT_RATE:
+ if (e->flags & (1 << NFT_EXPR_LIMIT_RATE))
+ return &limit->rate;
+ else
+ return NULL;
+ break;
+ case NFT_EXPR_LIMIT_DEPTH:
+ if (e->flags & (1 << NFT_EXPR_LIMIT_DEPTH))
+ return &limit->depth;
+ else
+ return NULL;
+ break;
+ default:
+ break;
+ }
+ return NULL;
+}
+
+static int nft_rule_expr_limit_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ if (mnl_attr_type_valid(attr, NFTA_LIMIT_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch(type) {
+ case NFTA_LIMIT_RATE:
+ case NFTA_LIMIT_DEPTH:
+ if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) {
+ perror("mnl_attr_validate");
+ return MNL_CB_ERROR;
+ }
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static void
+nft_rule_expr_limit_build(struct nlmsghdr *nlh, struct nft_rule_expr *e)
+{
+ struct nft_expr_limit *limit = (struct nft_expr_limit *)e->data;
+
+ if (e->flags & (1 << NFT_EXPR_LIMIT_RATE))
+ mnl_attr_put_u64(nlh, NFTA_LIMIT_RATE, htobe64(limit->rate));
+ if (e->flags & (1 << NFT_EXPR_LIMIT_DEPTH))
+ mnl_attr_put_u64(nlh, NFTA_LIMIT_DEPTH, htobe64(limit->depth));
+}
+
+static int
+nft_rule_expr_limit_parse(struct nft_rule_expr *e, struct nlattr *attr)
+{
+ struct nft_expr_limit *limit = (struct nft_expr_limit *)e->data;
+ struct nlattr *tb[NFTA_LIMIT_MAX+1] = {};
+
+ if (mnl_attr_parse_nested(attr, nft_rule_expr_limit_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_LIMIT_RATE]) {
+ limit->rate = be64toh(mnl_attr_get_u64(tb[NFTA_LIMIT_RATE]));
+ e->flags |= (1 << NFT_EXPR_LIMIT_RATE);
+ }
+ if (tb[NFTA_LIMIT_DEPTH]) {
+ limit->depth = be64toh(mnl_attr_get_u64(tb[NFTA_LIMIT_DEPTH]));
+ e->flags |= (1 << NFT_EXPR_LIMIT_DEPTH);
+ }
+
+ return 0;
+}
+
+static int nft_rule_expr_limit_xml_parse(struct nft_rule_expr *e, char *xml)
+{
+#ifdef XML_PARSING
+ struct nft_expr_limit *limit = (struct nft_expr_limit *)e->data;
+ mxml_node_t *tree = NULL;
+ mxml_node_t *node = NULL;
+ uint64_t tmp;
+ char *endptr;
+
+ tree = mxmlLoadString(NULL, xml, MXML_OPAQUE_CALLBACK);
+ if (tree == NULL)
+ return -1;
+
+ if (mxmlElementGetAttr(tree, "type") == NULL)
+ goto err;
+
+ if (strcmp("limit", mxmlElementGetAttr(tree, "type")) != 0)
+ goto err;
+
+ node = mxmlFindElement(tree, tree, "rate", NULL, NULL,
+ MXML_DESCEND_FIRST);
+ if (node == NULL)
+ goto err;
+
+ tmp = strtoull(node->child->value.opaque, &endptr, 10);
+ if (tmp > UINT64_MAX || tmp < 0 || *endptr)
+ goto err;
+
+ limit->rate = tmp;
+ e->flags |= (1 << NFT_EXPR_LIMIT_RATE);
+
+ node = mxmlFindElement(tree, tree, "depth", NULL, NULL,
+ MXML_DESCEND);
+ if (node == NULL)
+ goto err;
+
+ tmp = strtoull(node->child->value.opaque, &endptr, 10);
+ if (tmp > UINT64_MAX || tmp < 0 || *endptr)
+ goto err;
+
+ limit->depth = tmp;
+ e->flags |= (1 << NFT_EXPR_LIMIT_DEPTH);
+
+ mxmlDelete(tree);
+ return 0;
+err:
+ errno = EINVAL;
+ mxmlDelete(tree);
+ return -1;
+#else
+ errno = EOPNOTSUPP;
+ return -1;
+#endif
+}
+
+static int
+nft_rule_expr_limit_snprintf(char *buf, size_t len, uint32_t type,
+ uint32_t flags, struct nft_rule_expr *e)
+{
+ struct nft_expr_limit *limit = (struct nft_expr_limit *)e->data;
+
+ switch(type) {
+ case NFT_RULE_O_DEFAULT:
+ return snprintf(buf, len, "rate=%"PRIu64" depth=%"PRIu64" ",
+ limit->rate, limit->depth);
+ case NFT_RULE_O_XML:
+ return snprintf(buf, len, "<rate>%"PRIu64"</rate>"
+ "<depth>%"PRIu64"</depth>",
+ limit->rate, limit->depth);
+ default:
+ break;
+ }
+ return -1;
+}
+
+struct expr_ops expr_ops_limit = {
+ .name = "limit",
+ .alloc_len = sizeof(struct nft_expr_limit),
+ .max_attr = NFTA_LIMIT_MAX,
+ .set = nft_rule_expr_limit_set,
+ .get = nft_rule_expr_limit_get,
+ .parse = nft_rule_expr_limit_parse,
+ .build = nft_rule_expr_limit_build,
+ .snprintf = nft_rule_expr_limit_snprintf,
+ .xml_parse = nft_rule_expr_limit_xml_parse,
+};