nft: add reject support
PATCH 1/1 nft: add reject support
This patch allows to use reject in rules. Example:
nft add rule filter input udp dport 22 reject
In this rule, we assume that the reason is network unreachable. Also we can specify the reason with the option "with" and the reason. Example:
nft add rule filter input tcp dport 22 reject with host-unreach
In the bridge tables and inet tables, we can use this action too. Example:
nft add rule inet filter input reject with icmp-host-unreach
In this two tables, we generate a meta or payload dependency for enclosing which kind of traffic we must matching. We add this dependency only in the case that we must to add more context to this rule.
---
[Tested with the rules]
[Error ways]
Reject not supported for arp
* nft add rule arp filter input reject (reject not supported for arp)
Use icmp code field in a ipv6 table or icmpv6 code field in ipv4 table
* nft add rule ip filter input reject with icmpv6-no-route
* nft add rule ip6 filter input reject with icmp-host-unreach
Insufficient context for using reject
* nft add rule inet filter input reject
* nft add rule bridge filter input reject
Use tcp reset with a udp rule
* nft add rule ip filter input udp dport 9999 reject with tcp-reset X
[Hits ways]
IPV4
* nft add rule ip filter input udp dport 9999 reject with icmp-host-unreach
* nft add rule ip filter input tcp dport 9999 reject
* nft add rule ip filter input reject
IPV6
* nft add rule ip6 filter input udp dport 9999 reject
* nft add rule ip6 filter input tcp dport 9999 /
reject with icmpv6-admin-prohibited
* nft add rule ip6 filter input reject
INET
* nft add rule inet filter input reject with icmp-host-unreach
* nft add rule inet filter input reject with icmpv6-no-route
* nft add rule inet filter input udp dport 9999 counter / reject with icmpv6-no-route
BRIDGE
* nft add rule bridge filter input reject with icmp-host-unreach
* nft add rule bridge filter input reject with icmpv6-no-route
include/statement.h | 2 + src/evaluate.c | 116 +++++++++++++++++++++++++++++++++++++++++++-- src/netlink_delinearize.c | 37 +++++++++++++++ src/netlink_linearize.c | 2 +- src/parser.y | 73 ++++++++++++++++++++++++++-- src/payload.c | 31 ++++++++++-- src/scanner.l | 1 + src/statement.c | 29 ++++++++++++ 8 files changed, 279 insertions(+), 12 deletions(-)
include/statement.h +2 -0
@@ -47,6 +47,8 @@ extern struct stmt *limit_stmt_alloc(const struct location *loc);struct reject_stmt {enum nft_reject_types type; +int8_t icmp_code; +unsigned int family; };extern struct stmt *reject_stmt_alloc (const struct location *loc);
src/evaluate.c +113 -3
142 lines not on this page · raw message
src/netlink_delinearize.c +37 -0
@@ -14,6 +14,9 @@#include <string.h> #include <limits.h> #include <linux/netfilter/nf_tables.h> +#include <arpa/inet.h> +#include <linux/netfilter.h> +#include <net/ethernet.h> #include <netlink.h> #include <rule.h> #include <statement.h>
@@ -456,6 +459,9 @@ static void netlink_parse_reject(struct netlink_parse_ctx *ctx,struct stmt *stmt; stmt =reject_stmt_alloc (loc); + stmt->reject.type =nft_rule_expr_get_u32 (expr,NFT_EXPR_REJECT_TYPE ); + stmt->reject.icmp_code =nft_rule_expr_get_u8 (expr, +NFT_EXPR_REJECT_CODE );list_add_tail (&stmt->list, &ctx->rule->stmts); }
@@ -912,6 +918,34 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, } } +static void stmt_reject_postprocess (struct rule_pp_ctx rctx,struct stmt *stmt) +{ +const struct proto_desc *desc, *base; +int protocol; + +switch (rctx.pctx.family) { +case NFPROTO_IPV4 : +case NFPROTO_IPV6 : + stmt->reject.family = rctx.pctx.family; +break ; +case NFPROTO_INET : +case NFPROTO_BRIDGE : +if (rctx.pbase !=PROTO_BASE_INVALID ) { + base = rctx.pctx.protocol[PROTO_BASE_LL_HDR ].desc; + desc = rctx.pctx.protocol[PROTO_BASE_NETWORK_HDR ].desc; + protocol =proto_find_num (base, desc); +if (protocol ==ETH_P_IP ) + stmt->reject.family =NFPROTO_IPV4 ; +else if (protocol ==ETH_P_IPV6 ) + stmt->reject.family =NFPROTO_IPV6 ; +else + stmt->reject.family = protocol; + } +break ; +default : +break ; + } +}static void rule_parse_postprocess (struct netlink_parse_ctx *ctx,struct rule *rule) {struct rule_pp_ctx rctx;
@@ -939,6 +973,9 @@ static void rule_parse_postprocess(struct netlink_parse_ctx *ctx, struct rule *rif (stmt->nat.proto !=NULL )expr_postprocess (&rctx, stmt, &stmt->nat.proto);break ; +case STMT_REJECT : +stmt_reject_postprocess (rctx, stmt); +break ;default :break ; }
src/netlink_linearize.c +1 -1
@@ -609,7 +609,7 @@ static void netlink_gen_reject_stmt(struct netlink_linearize_ctx *ctx, nle =alloc_nft_expr ("reject" );nft_rule_expr_set_u32 (nle,NFT_EXPR_REJECT_TYPE , stmt->reject.type); -nft_rule_expr_set_u8 (nle,NFT_EXPR_REJECT_CODE ,0 ); +nft_rule_expr_set_u8 (nle,NFT_EXPR_REJECT_CODE , stmt->reject.icmp_code);nft_rule_add_expr (ctx->nlr, nle); }
src/parser.y +70 -3
102 lines not on this page · raw message
src/payload.c +26 -5
@@ -188,11 +188,32 @@ int payload_gen_dependency(struct eval_ctx *ctx, const struct expr *expr, } desc = ctx->pctx.protocol[expr->payload.base -1 ].desc; -/* Special case for mixed IPv4/IPv6 tables: use meta L4 proto */ -if (desc ==NULL && - ctx->pctx.family ==NFPROTO_INET && - expr->payload.base ==PROTO_BASE_TRANSPORT_HDR ) - desc = &proto_inet_service; +/* Special case for mixed IPv4/IPv6 and bridge tables */ +if (desc ==NULL ) { +switch (ctx->pctx.family) { +case NFPROTO_INET : +switch (expr->payload.base) { +case PROTO_BASE_TRANSPORT_HDR : + desc = &proto_inet_service; +break ; +case PROTO_BASE_LL_HDR : + desc = &proto_inet; +break ; +default : +break ; + } +break ; +case NFPROTO_BRIDGE : +switch (expr->payload.base) { +case PROTO_BASE_LL_HDR : + desc = &proto_eth; +break ; +default : +break ; + } +break ; + } + }if (desc ==NULL ) return expr_error(ctx->msgs, expr,
src/scanner.l +1 -0
@@ -295,6 +295,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "week" { return WEEK; } "reject" { return _REJECT; } +"with" { return WITH; } "snat" { return SNAT; } "dnat" { return DNAT; }
src/statement.c +29 -0
@@ -15,9 +15,15 @@#include <inttypes.h> #include <string.h> +#include <netinet/ip_icmp.h> +#include <netinet/icmp6.h> #include <statement.h> #include <utils.h> #include <list.h> +#include <arpa/inet.h> +#include <linux/netfilter.h> +#include <net/ethernet.h> +#include <utils.h> struct stmt *stmt_alloc (const struct location *loc,const struct stmt_ops *ops)
@@ -196,9 +202,32 @@ struct stmt *queue_stmt_alloc(const struct location *loc)return stmt_alloc (loc, &queue_stmt_ops); } +const char *reject_stmt_code_ip[] = { + [ICMP_NET_UNREACH ] ="icmp-net-unreach" , + [ICMP_HOST_UNREACH ] ="icmp-host-unreach" , + [ICMP_PROT_UNREACH ] ="icmp-prot-unreach" , + [ICMP_PORT_UNREACH ] ="icmp-port-unreach" , + [ICMP_NET_ANO ] ="icmp-net-prohibited" , + [ICMP_HOST_ANO ] ="icmp-host-prohibited" , + [ICMP_PKT_FILTERED ] ="icmp-admin-prohibited" , +}; + +const char *reject_stmt_code_ip6[] = { + [ICMP6_DST_UNREACH_NOROUTE ] ="icmpv6-no-route" , + [ICMP6_DST_UNREACH_ADMIN ] ="icmpv6-admin-prohibited" , + [ICMP6_DST_UNREACH_ADDR ] ="icmpv6-addr-unreach" , + [ICMP6_DST_UNREACH_NOPORT ] ="icmpv6-port-unreach" , +}; +static void reject_stmt_print (const struct stmt *stmt) {printf ("reject" ); +if (stmt->reject.icmp_code >=0 && stmt->reject.family ==NFPROTO_IPV4 ) +printf (" with %s" , + reject_stmt_code_ip[stmt->reject.icmp_code]); +if (stmt->reject.icmp_code >=0 && stmt->reject.family ==NFPROTO_IPV6 ) +printf (" with %s" , + reject_stmt_code_ip6[stmt->reject.icmp_code]); }static const struct stmt_ops reject_stmt_ops = {
Signature · 1 line
1.7.10.4