src: add level option to the log statement
Pablo Neira Ayuso
maintainerPATCH v2 1/1 src: add level option to the log statement
This patch is required if you use upcoming Linux kernels >= 3.17 which come with a complete logging support for nf_tables.
If you use 'log' without options, the kernel logging buffer is used:
nft> add rule filter input log
You can also specify the logging prefix string:
nft> add rule filter input log prefix "input: "
You may want to specify the log level:
nft> add rule filter input log prefix "input: " level notice
By default, if not specified, the default level is 'warn' (just like in iptables).
If you specify the group, then nft uses the nfnetlink_log instead:
nft> add rule filter input log prefix "input: " group 10
You can also specify the snaplen and qthreshold for the nfnetlink_log. But you cannot mix level and group at the same time, they are mutually exclusive.
Default values for both snaplen and qthreshold are 0 (just like in iptables).
---
v2: use the scanner to identify the log level instead of strcmp.
Suggested by Patrick.
include/statement.h | 10 +++++++++ src/evaluate.c | 15 ++++++++++++- src/netlink_delinearize.c | 28 ++++++++++++++++++----- src/netlink_linearize.c | 18 +++++++-------- src/parser.y | 54 +++++++++++++++++++++++++++++++++++++++++++++ src/scanner.l | 9 ++++++++ src/statement.c | 31 ++++++++++++++++++++++---- 7 files changed, 146 insertions(+), 19 deletions(-)
include/statement.h +10 -0
@@ -28,11 +28,21 @@ extern struct stmt *meta_stmt_alloc(const struct location *loc,enum nft_meta_keys key, struct expr *expr); +enum { +STMT_LOG_PREFIX = (1 <<0 ), +STMT_LOG_SNAPLEN = (1 <<1 ), +STMT_LOG_GROUP = (1 <<2 ), +STMT_LOG_QTHRESHOLD = (1 <<3 ), +STMT_LOG_LEVEL = (1 <<4 ), +}; +struct log_stmt {const char *prefix;unsigned int snaplen;uint16_t group;uint16_t qthreshold; +uint32_t level; +uint32_t flags; };extern struct stmt *log_stmt_alloc (const struct location *loc);
src/evaluate.c +14 -1
@@ -1180,6 +1180,18 @@ static int stmt_evaluate_ct(struct eval_ctx *ctx, struct stmt *stmt)return 0 ; } +static int stmt_evaluate_log (struct eval_ctx *ctx,struct stmt *stmt) +{ +if (stmt->log.flags &STMT_LOG_LEVEL && + (stmt->log.flags &STMT_LOG_GROUP || + stmt->log.flags &STMT_LOG_SNAPLEN || + stmt->log.flags &STMT_LOG_QTHRESHOLD )) { +return stmt_error (ctx, stmt, +"level and group are mutually exclusive" ); + } +return 0 ; +} +static int stmt_evaluate (struct eval_ctx *ctx,struct stmt *stmt) {#ifdef DEBUG
@@ -1193,7 +1205,6 @@ static int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)switch (stmt->ops->type) {case STMT_COUNTER :case STMT_LIMIT : -case STMT_LOG :return 0 ;case STMT_EXPRESSION :return stmt_evaluate_expr (ctx, stmt);
@@ -1201,6 +1212,8 @@ static int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)return stmt_evaluate_verdict (ctx, stmt);case STMT_META :return stmt_evaluate_meta (ctx, stmt); +case STMT_LOG : +return stmt_evaluate_log (ctx, stmt);case STMT_REJECT :return stmt_evaluate_reject (ctx, stmt);case STMT_NAT :
src/netlink_delinearize.c +23 -5
@@ -428,12 +428,30 @@ static void netlink_parse_log(struct netlink_parse_ctx *ctx, stmt =log_stmt_alloc (loc); prefix =nft_rule_expr_get_str (nle,NFT_EXPR_LOG_PREFIX ); -if (prefix !=NULL ) +if (nft_rule_expr_is_set (nle,NFT_EXPR_LOG_PREFIX )) { stmt->log.prefix =xstrdup (prefix); - stmt->log.group =nft_rule_expr_get_u16 (nle,NFT_EXPR_LOG_GROUP ); - stmt->log.snaplen =nft_rule_expr_get_u32 (nle,NFT_EXPR_LOG_SNAPLEN ); - stmt->log.qthreshold = -nft_rule_expr_get_u16 (nle,NFT_EXPR_LOG_QTHRESHOLD ); + stmt->log.flags |=STMT_LOG_PREFIX ; + } +if (nft_rule_expr_is_set (nle,NFT_EXPR_LOG_GROUP )) { + stmt->log.group = +nft_rule_expr_get_u16 (nle,NFT_EXPR_LOG_GROUP ); + stmt->log.flags |=STMT_LOG_GROUP ; + } +if (nft_rule_expr_is_set (nle,NFT_EXPR_LOG_SNAPLEN )) { + stmt->log.snaplen = +nft_rule_expr_get_u32 (nle,NFT_EXPR_LOG_SNAPLEN ); + stmt->log.flags |=STMT_LOG_SNAPLEN ; + } +if (nft_rule_expr_is_set (nle,NFT_EXPR_LOG_QTHRESHOLD )) { + stmt->log.qthreshold = +nft_rule_expr_get_u16 (nle,NFT_EXPR_LOG_QTHRESHOLD ); + stmt->log.flags |=STMT_LOG_QTHRESHOLD ; + } +if (nft_rule_expr_is_set (nle,NFT_EXPR_LOG_LEVEL )) { + stmt->log.level = +nft_rule_expr_get_u32 (nle,NFT_EXPR_LOG_LEVEL ); + stmt->log.flags |=STMT_LOG_LEVEL ; + }list_add_tail (&stmt->list, &ctx->rule->stmts); }
src/netlink_linearize.c +9 -9
@@ -576,17 +576,17 @@ static void netlink_gen_log_stmt(struct netlink_linearize_ctx *ctx,nft_rule_expr_set_str (nle,NFT_EXPR_LOG_PREFIX , stmt->log.prefix); } -if (stmt->log.group) { +if (stmt->log.flags &STMT_LOG_GROUP ) {nft_rule_expr_set_u16 (nle,NFT_EXPR_LOG_GROUP , stmt->log.group); - } -if (stmt->log.snaplen) { -nft_rule_expr_set_u32 (nle,NFT_EXPR_LOG_SNAPLEN , - stmt->log.snaplen); - } -if (stmt->log.qthreshold) { -nft_rule_expr_set_u16 (nle,NFT_EXPR_LOG_QTHRESHOLD , - stmt->log.qthreshold); +if (stmt->log.flags &STMT_LOG_SNAPLEN ) +nft_rule_expr_set_u32 (nle,NFT_EXPR_LOG_SNAPLEN , + stmt->log.snaplen); +if (stmt->log.flags &STMT_LOG_QTHRESHOLD ) +nft_rule_expr_set_u16 (nle,NFT_EXPR_LOG_QTHRESHOLD , + stmt->log.qthreshold); + }else { +nft_rule_expr_set_u32 (nle,NFT_EXPR_LOG_LEVEL , stmt->log.level); }nft_rule_add_expr (ctx->nlr, nle); }
src/parser.y +54 -0
90 lines not on this page · raw message
src/scanner.l +9 -0
@@ -276,6 +276,15 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "group" { return GROUP; } "snaplen" { return SNAPLEN; } "queue-threshold" { return QUEUE_THRESHOLD; } +"level" { return LEVEL; } +"emerg" { return LEVEL_EMERG; } +"alert" { return LEVEL_ALERT; } +"crit" { return LEVEL_CRIT; } +"err" { return LEVEL_ERR; } +"warn" { return LEVEL_WARN; } +"notice" { return LEVEL_NOTICE; } +"info" { return LEVEL_INFO; } +"debug" { return LEVEL_DEBUG; } "queue" { return QUEUE;} "num" { return QUEUENUM;}
src/statement.c +27 -4
@@ -14,6 +14,7 @@#include <stdint.h> #include <inttypes.h> #include <string.h> +#include <syslog.h> #include <statement.h> #include <utils.h>
@@ -112,17 +113,39 @@ struct stmt *counter_stmt_alloc(const struct location *loc)return stmt_alloc (loc, &counter_stmt_ops); } +static const char *syslog_level[LOG_DEBUG +1 ] = { + [LOG_EMERG ] ="emerg" , + [LOG_ALERT ] ="alert" , + [LOG_CRIT ] ="crit" , + [LOG_ERR ] ="err" , + [LOG_WARNING ] ="warn" , + [LOG_NOTICE ] ="notice" , + [LOG_INFO ] ="info" , + [LOG_DEBUG ] ="debug" , +}; + +static const char *log_level (uint32_t level) +{ +if (level >LOG_DEBUG ) +return "unknown" ; + +return syslog_level[level]; +} +static void log_stmt_print (const struct stmt *stmt) {printf ("log" ); -if (stmt->log.prefix !=NULL ) +if (stmt->log.flags &STMT_LOG_PREFIX )printf (" prefix \"%s\"" , stmt->log.prefix); -if (stmt->log.group) +if (stmt->log.flags &STMT_LOG_GROUP )printf (" group %u" , stmt->log.group); -if (stmt->log.snaplen) +if (stmt->log.flags &STMT_LOG_SNAPLEN )printf (" snaplen %u" , stmt->log.snaplen); -if (stmt->log.qthreshold) +if (stmt->log.flags &STMT_LOG_QTHRESHOLD )printf (" queue-threshold %u" , stmt->log.qthreshold); +if ((stmt->log.flags &STMT_LOG_LEVEL ) && + stmt->log.level !=LOG_WARNING ) +printf (" level %s" ,log_level (stmt->log.level)); }static void log_stmt_destroy (struct stmt *stmt)
Signature · 1 line
1.7.10.4