payload: generate dependency in the appropriate byteorder
PATCH v3 1/4 payload: generate dependency in the appropriate byteorder
If we add a dependency, the constant expression on the right hand side must be represented in the appropriate order.
Example without this patch:
nft add rule bridge filter input reject with icmp-host-unreach --debug netlink
[ payload load 2b @ link header + 12 => reg 1 ]
[ cmp eq reg 1 0x00000800 ]
[ reject type 0 code 1 ]
When we create the payload expression we have the right value in host endian but this has to be in big endian.
With this patch, if we add the same rule:
nft add rule bridge filter input reject with icmp-host-unreach --debug netlink
[ payload load 2b @ link header + 12 => reg 1 ]
[ cmp eq reg 1 0x00000008 ]
[ reject type 0 code 1 ]
The new dependency is converted to big endian.
---
[no changes in v3]
src/payload.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
src/payload.c +1 -2
@@ -216,8 +216,7 @@ int payload_gen_dependency(struct eval_ctx *ctx, const struct expr *expr, left =payload_expr_alloc (&expr->location, desc, desc->protocol_key); right =constant_expr_alloc (&expr->location, tmpl->dtype, -BYTEORDER_HOST_ENDIAN , - tmpl->len, + tmpl->dtype->byteorder, tmpl->len,constant_data_ptr (protocol, tmpl->len)); dep =relational_expr_alloc (&expr->location,OP_EQ , left, right);
Signature · 1 line
1.7.10.4
PATCH v3 2/4 src: Enhance payload_gen_dependency()
With this patch, this function returns a statement with the new dependency that we want to add, instead of an expression.
This change is needed in a follow up patch.
---
[no changes in v3]
include/payload.h | 3 ++- include/statement.h | 1 + src/evaluate.c | 9 ++------- src/payload.c | 18 +++++++++++++++--- 4 files changed, 20 insertions(+), 11 deletions(-)
include/payload.h +2 -1
@@ -11,8 +11,9 @@ extern void payload_init_raw(struct expr *expr, enum proto_bases base, unsigned int offset, unsigned int len);struct eval_ctx ; +struct stmt ;extern int payload_gen_dependency (struct eval_ctx *ctx,const struct expr *expr, -struct expr **res); +struct stmt **res);extern bool payload_is_adjacent (const struct expr *e1,const struct expr *e2); extern struct expr *payload_expr_join(const struct expr *e1,
include/statement.h +1 -0
@@ -166,6 +166,7 @@ struct stmt {extern struct stmt *stmt_alloc (const struct location *loc,const struct stmt_ops *ops); +int stmt_evaluate (struct eval_ctx *ctx,struct stmt *stmt);extern void stmt_free (struct stmt *stmt);extern void stmt_list_free (struct list_head *list);extern void stmt_print (const struct stmt *stmt);
src/evaluate.c +2 -7
@@ -26,7 +26,6 @@#include <utils.h> static int expr_evaluate (struct eval_ctx *ctx,struct expr **expr); -static int stmt_evaluate (struct eval_ctx *ctx,struct stmt *stmt);static const char *byteorder_names[] = { [BYTEORDER_INVALID ] ="invalid" ,
@@ -271,13 +270,9 @@ static int expr_evaluate_payload(struct eval_ctx *ctx, struct expr **expr)struct expr *payload = *expr;enum proto_bases base = payload->payload.base;struct stmt *nstmt; -struct expr *nexpr;if (ctx->pctx.protocol[base].desc ==NULL ) { -if (payload_gen_dependency (ctx, payload, &nexpr) <0 ) -return -1 ; - nstmt =expr_stmt_alloc (&nexpr->location, nexpr); -if (stmt_evaluate (ctx, nstmt) <0 ) +if (payload_gen_dependency (ctx, payload, &nstmt) <0 )return -1 ;list_add_tail (&nstmt->list, &ctx->stmt->list); }else if (ctx->pctx.protocol[base].desc != payload->payload.desc)
@@ -1205,7 +1200,7 @@ static int stmt_evaluate_log(struct eval_ctx *ctx, struct stmt *stmt)return 0 ; } -static int stmt_evaluate (struct eval_ctx *ctx,struct stmt *stmt) +int stmt_evaluate (struct eval_ctx *ctx,struct stmt *stmt) {#ifdef DEBUG if (debug_level &DEBUG_EVALUATION ) {
src/payload.c +15 -3
@@ -21,6 +21,7 @@#include <rule.h> #include <expression.h> +#include <statement.h> #include <payload.h> #include <gmputil.h> #include <utils.h>
@@ -160,12 +161,13 @@ void payload_init_raw(struct expr *expr, enum proto_bases base,* in the input path though. */ int payload_gen_dependency (struct eval_ctx *ctx,const struct expr *expr, -struct expr **res) +struct stmt **res) {const struct hook_proto_desc *h = &hook_proto_desc[ctx->pctx.family];const struct proto_desc *desc;const struct proto_hdr_template *tmpl;struct expr *dep, *left, *right; +struct stmt *stmt;int protocol;uint16_t type;
@@ -186,7 +188,12 @@ int payload_gen_dependency(struct eval_ctx *ctx, const struct expr *expr, 2 * BITS_PER_BYTE, &type); dep =relational_expr_alloc (&expr->location,OP_EQ , left, right); - *res = dep; + stmt =expr_stmt_alloc (&dep->location, dep); +if (stmt_evaluate (ctx, stmt) <0 ) { +return expr_error (ctx->msgs, expr, +"dependency statement is invalid" ); + } + *res = stmt;return 0 ; }
@@ -220,8 +227,13 @@ int payload_gen_dependency(struct eval_ctx *ctx, const struct expr *expr, constant_data_ptr(protocol, tmpl->len)); dep =relational_expr_alloc (&expr->location,OP_EQ , left, right); + stmt =expr_stmt_alloc (&dep->location, dep); +if (stmt_evaluate (ctx, stmt) <0 ) { +return expr_error (ctx->msgs, expr, +"dependency statement is invalid" ); + } left->ops->pctx_update (&ctx->pctx, dep); - *res = dep; + *res = stmt;return 0 ; }
Signature · 1 line
1.7.10.4
PATCH v3 3/4 datatype: Enhance symbolic_constant_parse()
In reply to Alvaro Neira AyusoWith this patch, this function finds the symbol inside the table. If the symbol doesn't exist we use the basetype to parse it and create the constant expression. Otherwise, return an error message.
This a refactorization to reuse this code in a follow up patch.
---
[changes in v3]
* Forgot to initialize res to NULL before to use basetype to parse it.
src/datatype.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-)
src/datatype.c +10 -20
@@ -112,6 +112,7 @@ struct error_record *symbolic_constant_parse(const struct expr *sym, {const struct symbolic_constant *s;const struct datatype *dtype; +struct error_record *erec;for (s = tbl->symbols; s->identifier !=NULL ; s++) {if (!strcmp (sym->identifier, s->identifier))
@@ -119,8 +120,16 @@ struct error_record *symbolic_constant_parse(const struct expr *sym, } dtype = sym->dtype; -if (s->identifier ==NULL ) +if (s->identifier ==NULL ) { + *res =NULL ; + erec = sym->dtype->basetype->parse (sym, res); +if (erec !=NULL ) +return erec; +if (*res) +return NULL ; +return error (&sym->location,"Could not parse %s" , dtype->desc); + } *res = constant_expr_alloc(&sym->location, dtype, dtype->byteorder, dtype->size,
@@ -660,25 +669,6 @@ static void mark_type_print(const struct expr *expr)static struct error_record *mark_type_parse (const struct expr *sym,struct expr **res) { -struct error_record *erec; -const struct symbolic_constant *s; - -for (s = mark_tbl->symbols; s->identifier !=NULL ; s++) { -if (!strcmp (sym->identifier, s->identifier)) { - *res =constant_expr_alloc (&sym->location, sym->dtype, - sym->dtype->byteorder, - sym->dtype->size, - &s->value); -return NULL ; - } - } - - *res =NULL ; - erec = sym->dtype->basetype->parse (sym, res); -if (erec !=NULL ) -return erec; -if (*res) -return NULL ;return symbolic_constant_parse (sym, mark_tbl, res); }
Signature · 1 line
1.7.10.4
PATCH v5 4/4 nft: complete reject support
In reply to Alvaro Neira AyusoThis patch allows to use the reject action in rules. For 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. For example:
nft add rule filter input tcp dport 22 reject with icmp type host-unreach
In the bridge tables and inet tables, we can use this action too. For example:
nft add rule inet filter input reject with icmp type host-unreach
In this rule above, this generates a meta nfproto dependency to match ipv4 traffic because we use a icmpv4 reason to reject.
If the reason is not specified, we infer it from the context.
Moreover, we have the new icmpx datatype. You can use this datatype for the bridge and the inet tables to simplify your ruleset. For example:
nft add rule inet filter input reject with icmpx type host-unreach
We have four icmpx reason and the mapping is:
ICMPX REASON | ICMPv6 | ICMPv4
| | admin-prohibited | admin-prohibited | admin-prohibited port-unreach | port-unreach | port-unreach no-route | no-route | net-unreach host-unreach | addr-unreach | host-unreach
---
[Changes in v5]
* Use a constant expression to print the icmp code in the delinearize step
* Fix some cosmetic errors
* Refactor code in evaluation step
* Add a new enum tcp_hdr_fields (TCPHDR_UNSPEC) to create a payload expression with a unspecific type.
[Tested with the rules]
[Error path]
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 icmp type no-route
* nft add rule ip6 filter input reject with icmpv6 type host-unreach
* nft add rule ip filter input reject with icmpv6 type no-route
* nft add rule ip6 filter input reject with icmp type host-unreach
Use tcp reset with a udp rule
* nft add rule ip filter input udp dport 9999 reject with tcp reset
[Success path]
IPV4
* nft add rule ip filter input udp dport 9999 \
reject with icmp type host-unreach
* nft add rule ip filter input tcp dport 9999 reject
* nft add rule ip filter input reject
* nft add rule ip filter input reject with tcp reset
IPV6
* nft add rule ip6 filter input udp dport 9999 reject
* nft add rule ip6 filter input tcp dport 9999 \
reject with icmpv6 type admin-prohibited
* nft add rule ip6 filter input reject
INET
* nft add rule inet filter input reject with icmp type host-unreach
* nft add rule inet filter input reject with icmpv6 type no-route
* nft add rule inet filter input udp dport 9999 counter \
reject with icmpv6 type no-route
* nft add rule inet filter input reject with tcp reset
* nft add rule inet filter input reject
BRIDGE
* nft add rule bridge filter input reject with icmp type host-unreach
* nft add rule bridge filter input reject with icmpv6 type no-route
include/datatype.h | 9 ++ include/linux/netfilter/nf_tables.h | 21 ++++ include/proto.h | 1 + include/statement.h | 3 + src/datatype.c | 105 ++++++++++++++++++++ src/evaluate.c | 181 ++++++++++++++++++++++++++++++++++- src/netlink_delinearize.c | 66 +++++++++++++ src/netlink_linearize.c | 5 +- src/parser.y | 54 ++++++++++- src/payload.c | 31 +++++- src/scanner.l | 3 + src/statement.c | 31 ++++++ 12 files changed, 500 insertions(+), 10 deletions(-)
include/datatype.h +9 -0
@@ -36,6 +36,9 @@* @TYPE_ICMP6_TYPE: ICMPv6 type codes (integer subtype) * @TYPE_CT_LABEL: Conntrack Label (bitmask subtype) * @TYPE_PKTTYPE: packet type (integer subtype) +* @TYPE_ICMP_CODE: icmp code (integer subtype) +* @TYPE_ICMPV6_CODE: icmpv6 code (integer subtype) +* @TYPE_ICMPX_CODE: icmpx code (integer subtype) */ enum datatypes {TYPE_INVALID ,
@@ -70,6 +73,9 @@ enum datatypes {TYPE_ICMP6_TYPE ,TYPE_CT_LABEL ,TYPE_PKTTYPE , +TYPE_ICMP_CODE , +TYPE_ICMPV6_CODE , +TYPE_ICMPX_CODE , __TYPE_MAX };#define TYPE_MAX (__TYPE_MAX - 1)
@@ -194,6 +200,9 @@ extern const struct datatype arphrd_type;extern const struct datatype inet_protocol_type;extern const struct datatype inet_service_type;extern const struct datatype mark_type; +extern const struct datatype icmp_code_type; +extern const struct datatype icmpv6_code_type; +extern const struct datatype icmpx_code_type;extern const struct datatype time_type;extern const struct datatype *concat_type_alloc (const struct expr *expr);
include/linux/netfilter/nf_tables.h +21 -0
@@ -749,13 +749,34 @@ enum nft_queue_attributes {* * @NFT_REJECT_ICMP_UNREACH: reject using ICMP unreachable * @NFT_REJECT_TCP_RST: reject using TCP RST +* @NFT_REJECT_ICMPX_UNREACH: abstracted ICMP unreachable for bridge and inet */ enum nft_reject_types {NFT_REJECT_ICMP_UNREACH ,NFT_REJECT_TCP_RST , +NFT_REJECT_ICMPX_UNREACH , };/** +* enum nft_reject_code - Abstracted reject codes +* +* @NFT_REJECT_ICMPX_NO_ROUTE: no route to host - network unreachable +* @NFT_REJECT_ICMPX_PORT_UNREACH: port unreachable +* @NFT_REJECT_ICMPX_HOST_UNREACH: host unreachable +* @NFT_REJECT_ICMPX_ADMIN_PROHIBITED: administratevely prohibited +* +* These codes are mapped to real ICMP and ICMPv6 codes. +*/ +enum nft_reject_inet_code { +NFT_REJECT_ICMPX_NO_ROUTE =0 , +NFT_REJECT_ICMPX_PORT_UNREACH , +NFT_REJECT_ICMPX_HOST_UNREACH , +NFT_REJECT_ICMPX_ADMIN_PROHIBITED , + __NFT_REJECT_ICMPX_MAX +}; +#define NFT_REJECT_ICMPX_MAX (__NFT_REJECT_ICMPX_MAX + 1) + +/** * enum nft_reject_attributes - nf_tables reject expression netlink attributes * * @NFTA_REJECT_TYPE: packet type to use (NLA_U32: nft_reject_types)
include/proto.h +1 -0
@@ -252,6 +252,7 @@ enum udp_hdr_fields {enum tcp_hdr_fields {TCPHDR_INVALID , +TCPHDR_UNSPEC =TCPHDR_INVALID ,TCPHDR_SPORT ,TCPHDR_DPORT ,TCPHDR_SEQ ,
include/statement.h +3 -0
@@ -56,7 +56,10 @@ struct limit_stmt {extern struct stmt *limit_stmt_alloc (const struct location *loc);struct reject_stmt { +struct expr *expr;enum nft_reject_types type; +int8_t icmp_code; +unsigned int family; };extern struct stmt *reject_stmt_alloc (const struct location *loc);
src/datatype.c +105 -0
123 lines not on this page · raw message
src/evaluate.c +180 -1
198 lines not on this page · raw message
src/netlink_delinearize.c +66 -0
92 lines not on this page · raw message
src/netlink_linearize.c +4 -1
@@ -612,7 +612,10 @@ 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 ); +if (stmt->reject.icmp_code !=-1 ) +nft_rule_expr_set_u8 (nle,NFT_EXPR_REJECT_CODE , + stmt->reject.icmp_code); +nft_rule_add_expr (ctx->nlr, nle); }
src/parser.y +51 -3
83 lines not on this page · raw message
src/payload.c +26 -5
@@ -198,11 +198,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 +3 -0
@@ -313,6 +313,9 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "week" { return WEEK; } "reject" { return _REJECT; } +"with" { return WITH; } +"reset" { return RESET; } +"icmpx" { return ICMPX; } "snat" { return SNAT; } "dnat" { return DNAT; }
src/statement.c +31 -0
@@ -16,6 +16,10 @@#include <string.h> #include <syslog.h> +#include <arpa/inet.h> +#include <linux/netfilter.h> +#include <netinet/ip_icmp.h> +#include <netinet/icmp6.h> #include <statement.h> #include <utils.h> #include <list.h>
@@ -227,6 +231,33 @@ struct stmt *queue_stmt_alloc(const struct location *loc)static void reject_stmt_print (const struct stmt *stmt) {printf ("reject" ); +switch (stmt->reject.type) { +case NFT_REJECT_TCP_RST : +printf (" with tcp reset" ); +break ; +case NFT_REJECT_ICMPX_UNREACH : +if (stmt->reject.icmp_code ==NFT_REJECT_ICMPX_PORT_UNREACH ) +break ; +printf (" with icmpx type " ); +expr_print (stmt->reject.expr); +break ; +case NFT_REJECT_ICMP_UNREACH : +switch (stmt->reject.family) { +case NFPROTO_IPV4 : +if (stmt->reject.icmp_code ==ICMP_PORT_UNREACH ) +break ; +printf (" with icmp type " ); +expr_print (stmt->reject.expr); +break ; +case NFPROTO_IPV6 : +if (stmt->reject.icmp_code ==ICMP6_DST_UNREACH_NOPORT ) +break ; +printf (" with icmpv6 type " ); +expr_print (stmt->reject.expr); +break ; + } +break ; + } }static const struct stmt_ops reject_stmt_ops = {
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerRe: [nft PATCH 4/4 v5] nft: complete reject support
Quoted 3 lines · Alvaro Neira Ayuso
This patch allows to use the reject action in rules. For example:
nft add rule filter input udp dport 22 reject
Series applies with some minor glitches, Thanks Alvaro.
See comments below.
include/proto.h enum udp_hdr_fields { · line 255
Quoted 9 lines
include/proto.h +1
-0@@ -252,6 +252,7 @@ enum udp_hdr_fields {enum tcp_hdr_fields {TCPHDR_INVALID , +TCPHDR_UNSPEC =TCPHDR_INVALID ,
I think we can rename all _INVALID to _UNSPEC in a follow up patch, given that we use this to generate the dependencies.
include/proto.h enum udp_hdr_fields { · line 258
Quoted 3 lines
include/proto.h +3
-0+static const struct symbol_table icmp_code_tbl = { + .symbols = { +SYMBOL ("port-unreach" ,ICMP_PORT_UNREACH ),
This one is shortened.
include/proto.h enum udp_hdr_fields {
Quoted 1 line
include/proto.h +1
-0+ SYMBOL("net-prohibited", ICMP_NET_ANO),
This one is full long name.
I have mangled the patch to use long names all the time for consistency. Once we get autocompletion I guess this shouldn't be a problem.
Quoted 7 lines
include/proto.h +7
-0+static int stmt_reject_gen_dependency (struct eval_ctx *ctx,struct stmt *stmt, +struct expr *expr) +{ +struct expr *payload =NULL ; +struct stmt *nstmt; + +switch (stmt->reject.type) {
include/proto.h
Quoted 10 lines
include/proto.h +10
-0+case NFT_REJECT_TCP_RST : +if (reject_payload_gen_dependency_tcp (ctx, stmt, &payload) <0 ) +return -1 ; +break ; +default : +if (reject_payload_gen_dependency_family (ctx, stmt, + &payload) <0 ) +return -1 ; +break ; + }
Beware with using 'default' in switch().
This is actually catching the NFT_REJECT_ICMP_UNREACH. And NFT_REJECT_ICMPX_UNREACH seems not possible.
I mangled this to catch NFT_REJECT_ICMP_UNREACH and the default case (which should not happen displays a bug).
Quoted 41 lines
include/proto.h +37
-0@@ -899,6 +909,59 @@ 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 : + stmt->reject.family = rctx.pctx.family; + stmt->reject.expr->dtype = &icmp_code_type; +break ; +case NFPROTO_IPV6 : + stmt->reject.family = rctx.pctx.family; + stmt->reject.expr->dtype = &icmpv6_code_type; +break ; +case NFPROTO_INET : +if (stmt->reject.type ==NFT_REJECT_ICMPX_UNREACH ) +break ; + base = rctx.pctx.protocol[PROTO_BASE_LL_HDR ].desc; + desc = rctx.pctx.protocol[PROTO_BASE_NETWORK_HDR ].desc; + protocol =proto_find_num (base, desc); +switch (protocol) { +case NFPROTO_IPV4 : + stmt->reject.expr->dtype = &icmp_code_type; +break ; +case NFPROTO_IPV6 : + stmt->reject.expr->dtype = &icmpv6_code_type; +break ; + } + stmt->reject.family = protocol; +break ; +case NFPROTO_BRIDGE : +if (stmt->reject.type ==NFT_REJECT_ICMPX_UNREACH ) +break ; + base = rctx.pctx.protocol[PROTO_BASE_LL_HDR ].desc; + desc = rctx.pctx.protocol[PROTO_BASE_NETWORK_HDR ].desc; + protocol =proto_find_num (base, desc); +switch (protocol) {
include/proto.h static void expr_postprocess(struct rule_pp_ctx *ctx, · line 958
Quoted 10 lines
include/proto.h +10
-0+case __constant_htons (ETH_P_IP ): + stmt->reject.family =NFPROTO_IPV4 ; + stmt->reject.expr->dtype = &icmp_code_type; +break ; +case __constant_htons (ETH_P_IPV6 ): + stmt->reject.family =NFPROTO_IPV6 ; + stmt->reject.expr->dtype = &icmpv6_code_type; +break ; +default : +break ;
Please, have a closer look to this 'default' case. We may have different ethertype in bridge (not just ipv4 and ipv6). Check if this works fine in that case.