]> git.ipfire.org Git - thirdparty/libnftnl.git/commitdiff
expr: add limit
authorPablo Neira Ayuso <pablo@netfilter.org>
Thu, 13 Jun 2013 11:33:08 +0000 (13:33 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Thu, 13 Jun 2013 11:33:08 +0000 (13:33 +0200)
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/libnftables/expr.h
src/Makefile.am
src/expr/limit.c [new file with mode: 0644]
src/expr_ops.c

index ba6ef6445d37653f9bc1f2231ffb2f2ce0f9cf58..bd3fd6efd3728a72e1eea91e46cd3d4552766ec9 100644 (file)
@@ -122,6 +122,11 @@ enum {
        NFT_EXPR_BYTEORDER_SIZE,
 };
 
+enum {
+       NFT_EXPR_LIMIT_RATE     = NFT_RULE_EXPR_ATTR_BASE,
+       NFT_EXPR_LIMIT_DEPTH,
+};
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
index 5872cce8a1a42e6c8fd86002d32e84d433729f51..401772069d196e4b7604255543e0c4efacaafed1 100644 (file)
@@ -18,6 +18,7 @@ libnftables_la_SOURCES = table.c              \
                         expr/ct.c              \
                         expr/data_reg.c        \
                         expr/exthdr.c          \
+                        expr/limit.c           \
                         expr/log.c             \
                         expr/lookup.c          \
                         expr/immediate.c       \
diff --git a/src/expr/limit.c b/src/expr/limit.c
new file mode 100644 (file)
index 0000000..64a5b70
--- /dev/null
@@ -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,
+};
index 3d0d65349dab46ed53694c57963e56e3320635f4..da4c1943556514ef6e7ec094553b7c8afb48e033 100644 (file)
@@ -9,6 +9,7 @@ extern struct expr_ops expr_ops_counter;
 extern struct expr_ops expr_ops_ct;
 extern struct expr_ops expr_ops_exthdr;
 extern struct expr_ops expr_ops_immediate;
+extern struct expr_ops expr_ops_limit;
 extern struct expr_ops expr_ops_log;
 extern struct expr_ops expr_ops_lookup;
 extern struct expr_ops expr_ops_match;
@@ -30,6 +31,7 @@ struct expr_ops *expr_ops[] = {
        &expr_ops_nat,
        &expr_ops_payload,
        &expr_ops_target,
+       &expr_ops_limit,
        &expr_ops_log,
        &expr_ops_lookup,
        NULL,