summaryrefslogtreecommitdiffstats
path: root/src/expr/limit.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/expr/limit.c')
-rw-r--r--src/expr/limit.c101
1 files changed, 59 insertions, 42 deletions
diff --git a/src/expr/limit.c b/src/expr/limit.c
index 5872e27..3644497 100644
--- a/src/expr/limit.c
+++ b/src/expr/limit.c
@@ -1,11 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* (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>
*/
@@ -32,28 +28,26 @@ struct nftnl_expr_limit {
static int
nftnl_expr_limit_set(struct nftnl_expr *e, uint16_t type,
- const void *data, uint32_t data_len)
+ const void *data, uint32_t data_len, uint32_t byteorder)
{
struct nftnl_expr_limit *limit = nftnl_expr_data(e);
switch(type) {
case NFTNL_EXPR_LIMIT_RATE:
- memcpy(&limit->rate, data, sizeof(limit->rate));
+ memcpy(&limit->rate, data, data_len);
break;
case NFTNL_EXPR_LIMIT_UNIT:
- memcpy(&limit->unit, data, sizeof(limit->unit));
+ memcpy(&limit->unit, data, data_len);
break;
case NFTNL_EXPR_LIMIT_BURST:
- memcpy(&limit->burst, data, sizeof(limit->burst));
+ memcpy(&limit->burst, data, data_len);
break;
case NFTNL_EXPR_LIMIT_TYPE:
- memcpy(&limit->type, data, sizeof(limit->type));
+ memcpy(&limit->type, data, data_len);
break;
case NFTNL_EXPR_LIMIT_FLAGS:
- memcpy(&limit->flags, data, sizeof(limit->flags));
+ memcpy(&limit->flags, data, data_len);
break;
- default:
- return -1;
}
return 0;
}
@@ -160,16 +154,28 @@ nftnl_expr_limit_parse(struct nftnl_expr *e, struct nlattr *attr)
return 0;
}
-static const char *get_unit(uint64_t u)
+static const char *get_time(uint64_t seconds, uint64_t *val)
{
- switch (u) {
- case 1: return "second";
- case 60: return "minute";
- case 60 * 60: return "hour";
- case 60 * 60 * 24: return "day";
- case 60 * 60 * 24 * 7: return "week";
+ static const struct {
+ unsigned int size;
+ const char *name;
+ } units[] = {
+ { 0, "second" },
+ { 60, "minute" },
+ { 60, "hour" },
+ { 24, "day" },
+ { 7, "week" }
+ };
+ int i;
+
+ for (i = 1; i < array_size(units); i++) {
+ if (seconds % units[i].size)
+ break;
+ seconds /= units[i].size;
}
- return "error";
+ if (val)
+ *val = seconds;
+ return units[i - 1].name;
}
static const char *limit_to_type(enum nft_limit_type type)
@@ -183,38 +189,49 @@ static const char *limit_to_type(enum nft_limit_type type)
}
}
-static int nftnl_expr_limit_snprintf_default(char *buf, size_t len,
- const struct nftnl_expr *e)
-{
- struct nftnl_expr_limit *limit = nftnl_expr_data(e);
-
- return snprintf(buf, len, "rate %"PRIu64"/%s burst %u type %s flags 0x%x ",
- limit->rate, get_unit(limit->unit), limit->burst,
- limit_to_type(limit->type), limit->flags);
-}
-
static int
-nftnl_expr_limit_snprintf(char *buf, size_t len, uint32_t type,
+nftnl_expr_limit_snprintf(char *buf, size_t len,
uint32_t flags, const struct nftnl_expr *e)
{
- switch(type) {
- case NFTNL_OUTPUT_DEFAULT:
- return nftnl_expr_limit_snprintf_default(buf, len, e);
- case NFTNL_OUTPUT_XML:
- case NFTNL_OUTPUT_JSON:
- default:
- break;
+ struct nftnl_expr_limit *limit = nftnl_expr_data(e);
+ unsigned int offset = 0;
+ const char *time_unit;
+ uint64_t time_val;
+ int ret;
+
+ ret = snprintf(buf, len, "rate %"PRIu64"/", limit->rate);
+ SNPRINTF_BUFFER_SIZE(ret, len, offset);
+
+ time_unit = get_time(limit->unit, &time_val);
+ if (time_val > 1) {
+ ret = snprintf(buf + offset, len, "%"PRIu64" ", time_val);
+ SNPRINTF_BUFFER_SIZE(ret, len, offset);
}
- return -1;
+
+ ret = snprintf(buf + offset, len, "%s burst %u type %s flags 0x%x ",
+ time_unit, limit->burst, limit_to_type(limit->type),
+ limit->flags);
+ SNPRINTF_BUFFER_SIZE(ret, len, offset);
+
+ return offset;
}
+static struct attr_policy limit_attr_policy[__NFTNL_EXPR_LIMIT_MAX] = {
+ [NFTNL_EXPR_LIMIT_RATE] = { .maxlen = sizeof(uint64_t) },
+ [NFTNL_EXPR_LIMIT_UNIT] = { .maxlen = sizeof(uint64_t) },
+ [NFTNL_EXPR_LIMIT_BURST] = { .maxlen = sizeof(uint32_t) },
+ [NFTNL_EXPR_LIMIT_TYPE] = { .maxlen = sizeof(uint32_t) },
+ [NFTNL_EXPR_LIMIT_FLAGS] = { .maxlen = sizeof(uint32_t) },
+};
+
struct expr_ops expr_ops_limit = {
.name = "limit",
.alloc_len = sizeof(struct nftnl_expr_limit),
- .max_attr = NFTA_LIMIT_MAX,
+ .nftnl_max_attr = __NFTNL_EXPR_LIMIT_MAX - 1,
+ .attr_policy = limit_attr_policy,
.set = nftnl_expr_limit_set,
.get = nftnl_expr_limit_get,
.parse = nftnl_expr_limit_parse,
.build = nftnl_expr_limit_build,
- .snprintf = nftnl_expr_limit_snprintf,
+ .output = nftnl_expr_limit_snprintf,
};