From faa855f4deaded25781a41af19ebfe82fa77bfc0 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Thu, 13 Jun 2013 13:33:08 +0200 Subject: expr: add limit Signed-off-by: Pablo Neira Ayuso --- include/libnftables/expr.h | 5 ++ src/Makefile.am | 1 + src/expr/limit.c | 213 +++++++++++++++++++++++++++++++++++++++++++++ src/expr_ops.c | 2 + 4 files changed, 221 insertions(+) create mode 100644 src/expr/limit.c diff --git a/include/libnftables/expr.h b/include/libnftables/expr.h index ba6ef64..bd3fd6e 100644 --- a/include/libnftables/expr.h +++ b/include/libnftables/expr.h @@ -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 diff --git a/src/Makefile.am b/src/Makefile.am index 5872cce..4017720 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 0000000..64a5b70 --- /dev/null +++ b/src/expr/limit.c @@ -0,0 +1,213 @@ +/* + * (C) 2012-2013 by Pablo Neira Ayuso + * + * 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 + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "internal.h" +#include +#include +#include +#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, "%"PRIu64"" + "%"PRIu64"", + 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, +}; diff --git a/src/expr_ops.c b/src/expr_ops.c index 3d0d653..da4c194 100644 --- a/src/expr_ops.c +++ b/src/expr_ops.c @@ -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, -- cgit v1.2.3