expr: add synproxy support
Subsystems (2)
PATCH 1/1 expr: add synproxy support
Signed-off-by Fernando Fernandez Mancera <[email protected]>
--- include/libnftnl/expr.h | 6 + include/linux/netfilter/nf_tables.h | 17 +++ src/Makefile.am | 1 + src/expr/synproxy.c | 170 ++++++++++++++++++++++++++++ src/expr_ops.c | 2 + 5 files changed, 196 insertions(+) create mode 100644 src/expr/synproxy.c
include/libnftnl/expr.h +6 -0
@@ -291,6 +291,12 @@ enum { NFTNL_EXPR_XFRM_SPNUM, }; +enum { +NFTNL_EXPR_SYNPROXY_MSS =NFTNL_EXPR_BASE , +NFTNL_EXPR_SYNPROXY_WSCALE , +NFTNL_EXPR_SYNPROXY_FLAGS , +}; + #ifdef __cplusplus }/* extern "C" */ #endif
include/linux/netfilter/nf_tables.h +17 -0
@@ -951,6 +951,23 @@ enum nft_osf_attributes { };#define NFTA_OSF_MAX (__NFTA_OSF_MAX - 1) +/** +* enum nft_synproxy_attributes - nf_tables synproxy expression +* netlink attributes +* +* @NFTA_SYNPROXY_MSS: mss value sent to the backend (NLA_U16) +* @NFTA_SYNPROXY_WSCALE: wscale value sent to the backend (NLA_U8) +* @NFTA_SYNPROXY_FLAGS: flags (NLA_U32) +*/ +enum nft_synproxy_attributes { +NFTA_SYNPROXY_UNSPEC , +NFTA_SYNPROXY_MSS , +NFTA_SYNPROXY_WSCALE , +NFTA_SYNPROXY_FLAGS , + __NFTA_SYNPROXY_MAX, +}; +#define NFTA_SYNPROXY_MAX (__NFTA_SYNPROXY_MAX - 1) +/** * enum nft_ct_keys - nf_tables ct expression keys *
src/Makefile.am +1 -0
@@ -58,6 +58,7 @@ libnftnl_la_SOURCES = utils.c \ expr/socket.c \ expr/osf.c \ expr/xfrm.c \ + expr/synproxy.c \ obj/counter.c \ obj/ct_helper.c \ obj/quota.c \
src/expr/synproxy.c new file +170 -0
170 lines not on this page · raw message
src/expr_ops.c +2 -0
@@ -40,6 +40,7 @@ extern struct expr_ops expr_ops_socket;extern struct expr_ops expr_ops_tunnel;extern struct expr_ops expr_ops_osf;extern struct expr_ops expr_ops_xfrm; +extern struct expr_ops expr_ops_synproxy;static struct expr_ops expr_ops_notrack = { .name ="notrack" ,
@@ -83,6 +84,7 @@ static struct expr_ops *expr_ops[] = { &expr_ops_tunnel, &expr_ops_osf, &expr_ops_xfrm, + &expr_ops_synproxy, NULL, };
Signature · 1 line
2.20.1
PATCH 1/1 netfilter: nf_tables: Add SYNPROXY support
Add SYNPROXY module support in nf_tables. It preserves the behaviour of the SYNPROXY target of iptables but structured in a different way to propose improvements in the future.
Signed-off-by Fernando Fernandez Mancera <[email protected]>
--- include/uapi/linux/netfilter/nf_SYNPROXY.h | 4 + include/uapi/linux/netfilter/nf_tables.h | 17 ++ net/netfilter/Kconfig | 11 + net/netfilter/Makefile | 1 + net/netfilter/nft_synproxy.c | 323 +++++++++++++++++++++ 5 files changed, 356 insertions(+) create mode 100644 net/netfilter/nft_synproxy.c
include/uapi/linux/netfilter/nf_SYNPROXY.h +4 -0
@@ -9,6 +9,10 @@#define NF_SYNPROXY_OPT_SACK_PERM 0x04#define NF_SYNPROXY_OPT_TIMESTAMP 0x08#define NF_SYNPROXY_OPT_ECN 0x10 +#define NF_SYNPROXY_OPT_MASK (NF_SYNPROXY_OPT_MSS | \ + NF_SYNPROXY_OPT_WSCALE | \ + NF_SYNPROXY_OPT_SACK_PERM | \ + NF_SYNPROXY_OPT_TIMESTAMP)struct nf_synproxy_info {__u8 options;
include/uapi/linux/netfilter/nf_tables.h +17 -0
@@ -1549,6 +1549,23 @@ enum nft_osf_flags { NFT_OSF_F_VERSION = (1 << 0), }; +/** +* enum nft_synproxy_attributes - nf_tables synproxy expression +* netlink attributes +* +* @NFTA_SYNPROXY_MSS: mss value sent to the backend (NLA_U16) +* @NFTA_SYNPROXY_WSCALE: wscale value sent to the backend (NLA_U8) +* @NFTA_SYNPROXY_FLAGS: flags (NLA_U32) +*/ +enum nft_synproxy_attributes { +NFTA_SYNPROXY_UNSPEC , +NFTA_SYNPROXY_MSS , +NFTA_SYNPROXY_WSCALE , +NFTA_SYNPROXY_FLAGS , + __NFTA_SYNPROXY_MAX, +}; +#define NFTA_SYNPROXY_MAX (__NFTA_SYNPROXY_MAX - 1) +/** * enum nft_device_attributes - nf_tables device netlink attributes *
net/netfilter/Kconfig +11 -0
@@ -651,6 +651,17 @@ config NFT_TPROXY help This makes transparent proxy support available in nftables. +config NFT_SYNPROXY + tristate "Netfilter nf_tables SYNPROXY expression support" + depends on NF_CONNTRACK && NETFILTER_ADVANCED + select NETFILTER_SYNPROXY + select SYN_COOKIES + help + The SYNPROXY expression allows you to intercept TCP connections and + establish them using syncookies before they are passed on to the + server. This allows to avoid conntrack and server resource usage + during SYN-flood attacks. + if NF_TABLES_NETDEV config NF_DUP_NETDEV
net/netfilter/Makefile +1 -0
@@ -110,6 +110,7 @@ obj-$(CONFIG_NFT_SOCKET) += nft_socket.o obj-$(CONFIG_NFT_OSF) += nft_osf.o obj-$(CONFIG_NFT_TPROXY) += nft_tproxy.o obj-$(CONFIG_NFT_XFRM) += nft_xfrm.o +obj-$(CONFIG_NFT_SYNPROXY) += nft_synproxy.o obj-$(CONFIG_NFT_NAT) += nft_chain_nat.o
net/netfilter/nft_synproxy.c new file +323 -0
323 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH 1/1 src: introduce SYNPROXY matching
In reply to Fernando Fernandez ManceraAdd support for "synproxy" statement. Example:
table ip x {
chain y {
type filter hook prerouting priority raw; policy accept;
tcp flags syn notrack
}
chain z {
type filter hook input priority filter; policy accept;
ct state { invalid, untracked } synproxy mss 1460 wscale 7 timestamp sack-perm
ct state invalid drop
}
}Signed-off-by Fernando Fernandez Mancera <[email protected]>
--- doc/statements.txt | 87 +++++++++++++++++++++++++ include/json.h | 1 + include/linux/netfilter/nf_SYNPROXY.h | 23 +++++++ include/linux/netfilter/nf_tables.h | 17 +++++ include/statement.h | 11 ++++ src/evaluate.c | 15 +++++ src/json.c | 29 +++++++++ src/netlink_delinearize.c | 17 +++++ src/netlink_linearize.c | 17 +++++ src/parser_bison.y | 48 ++++++++++++++ src/parser_json.c | 94 +++++++++++++++++++++++++++ src/scanner.l | 6 ++ src/statement.c | 51 +++++++++++++++ tests/py/inet/synproxy.t | 13 ++++ tests/py/inet/synproxy.t.json | 71 ++++++++++++++++++++ tests/py/inet/synproxy.t.payload | 72 ++++++++++++++++++++ 16 files changed, 572 insertions(+) create mode 100644 include/linux/netfilter/nf_SYNPROXY.h create mode 100644 tests/py/inet/synproxy.t create mode 100644 tests/py/inet/synproxy.t.json create mode 100644 tests/py/inet/synproxy.t.payload
doc/statements.txt +87 -0
@@ -483,6 +483,93 @@ table inet x { } ------------------------------------- +SYNPROXY STATEMENT +~~~~~~~~~~~~~~~~~~ +This statement will process TCP three-way-handshake parallel in netfilter +context to protect either local or backend system. This statement requires +connection tracking because sequence numbers need to be translated. + +[verse] +*synproxy* [*mss* 'mss_value'] [*wscale* 'wscale_value'] ['SYNPROXY_FLAGS'] + +.synproxy statement attributes +[options="header"] +|================= +| Name | Description +| mss | Maximum segment size announced to clients. This must match the backend. +| wscale | Window scale announced to clients. This must match the backend. +|================= + +.synproxy statement flags +[options="header"] +|================= +| Flag | Description +| sack-perm | +Pass client selective acknowledgement option to backend (will be disabled if +not present). +| timestamp | +Pass client timestamp option to backend (will be disabled if not present, also +needed for selective acknowledgement and window scaling). +|================= + +.Example ruleset for synproxy statement +--------------------------------------- +Determine tcp options used by backend, from an external system + + tcpdump -pni eth0 -c 1 'tcp[tcpflags] == (tcp-syn|tcp-ack)' + port 80 & + telnet 192.0.2.42 80 + 18:57:24.693307 IP 192.0.2.42.80 > 192.0.2.43.48757: + Flags [S.], seq 360414582, ack 788841994, win 14480, + options [mss 1460,sackOK, + TS val 1409056151 ecr 9690221, + nop,wscale 9], + length 0 + +Switch tcp_loose mode off, so conntrack will mark out-of-flow packets as state INVALID. + + echo 0 > /proc/sys/net/netfilter/nf_conntrack_tcp_loose + +Make SYN packets untracked. + + table ip x { + chain y { + type filter hook prerouting priority raw; policy accept; + tcp flags syn notrack + } + } + +Catch UNTRACKED (SYN packets) and INVALID (3WHS ACK packets) states and send +them to SYNPROXY. This rule will respond to SYN packets with SYN+ACK +syncookies, create ESTABLISHED for valid client response (3WHS ACK packets) and +drop incorrect cookies. Flags combinations not expected during 3WHS will not +match and continue (e.g. SYN+FIN, SYN+ACK). Finally, drop invalid packets, this +will be out-of-flow packets that were not matched by SYNPROXY. + + table ip foo { + chain z { + type filter hook input priority filter; policy accept; + ct state { invalid, untracked } synproxy mss 1460 wscale 9 timestamp sack-perm + ct state invalid drop + } + } + +The outcome ruleset of the steps above should be similar to the one below. + + table ip x { + chain y { + type filter hook prerouting priority raw; policy accept; + tcp flags syn notrack + } + + chain z { + type filter hook input priority filter; policy accept; + ct state { invalid, untracked } synproxy mss 1460 wscale 9 timestamp sack-perm + ct state invalid drop + } + } +--------------------------------------- + FLOW STATEMENT ~~~~~~~~~~~~~~ A flow statement allows us to select what flows you want to accelerate
include/json.h +1 -0
@@ -83,6 +83,7 @@ json_t *queue_stmt_json(const struct stmt *stmt, struct output_ctx *octx);json_t *verdict_stmt_json (const struct stmt *stmt,struct output_ctx *octx);json_t *connlimit_stmt_json (const struct stmt *stmt,struct output_ctx *octx);json_t *tproxy_stmt_json (const struct stmt *stmt,struct output_ctx *octx); +json_t *synproxy_stmt_json (const struct stmt *stmt,struct output_ctx *octx);int do_command_list_json (struct netlink_ctx *ctx,struct cmd *cmd);
include/linux/netfilter/nf_SYNPROXY.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _NF_SYNPROXY_H +#define _NF_SYNPROXY_H + +#include <linux/types.h> + +#define NF_SYNPROXY_OPT_MSS 0x01 +#define NF_SYNPROXY_OPT_WSCALE 0x02 +#define NF_SYNPROXY_OPT_SACK_PERM 0x04 +#define NF_SYNPROXY_OPT_TIMESTAMP 0x08 +#define NF_SYNPROXY_OPT_ECN 0x10 +#define NF_SYNPROXY_FLAGMASK (NF_SYNPROXY_OPT_MSS | \ + NF_SYNPROXY_OPT_WSCALE | \ + NF_SYNPROXY_OPT_SACK_PERM | \ + NF_SYNPROXY_OPT_TIMESTAMP) + +struct nf_synproxy_info { +__u8 options; +__u8 wscale; +__u16 mss; +}; + +#endif /* _NF_SYNPROXY_H */
include/linux/netfilter/nf_tables.h +17 -0
@@ -1529,6 +1529,23 @@ enum nft_osf_attributes { };#define NFTA_OSF_MAX (__NFTA_OSF_MAX - 1) +/** +* enum nft_synproxy_attributes - nftables synproxy expression +* netlink attributes +* +* @NFTA_SYNPROXY_MSS: mss value sent to the backend (NLA_U16) +* @NFTA_SYNPROXY_WSCALE: wscale value sent to the backend (NLA_U8) +* @NFTA_SYNPROXY_FLAGS: flags (NLA_U32) +*/ +enum nft_synproxy_attributes { +NFTA_SYNPROXY_UNSPEC , +NFTA_SYNPROXY_MSS , +NFTA_SYNPROXY_WSCALE , +NFTA_SYNPROXY_FLAGS , + __NFTA_SYNPROXY_MAX, +}; +#define NFTA_SYNPROXY_MAX (__NFTA_SYNPROXY_MAX - 1) +/** * enum nft_device_attributes - nf_tables device netlink attributes *
include/statement.h +11 -0
@@ -203,6 +203,14 @@ struct map_stmt {extern struct stmt *map_stmt_alloc (const struct location *loc); +struct synproxy_stmt { +uint16_t mss; +uint8_t wscale; +uint32_t flags; +}; + +extern struct stmt *synproxy_stmt_alloc (const struct location *loc); +struct meter_stmt {struct expr *set;struct expr *key;
@@ -270,6 +278,7 @@ extern struct stmt *xt_stmt_alloc(const struct location *loc);* @STMT_FLOW_OFFLOAD: flow offload statement * @STMT_CONNLIMIT: connection limit statement * @STMT_MAP: map statement +* @STMT_SYNPROXY: synproxy statement */ enum stmt_types {STMT_INVALID ,
@@ -297,6 +306,7 @@ enum stmt_types {STMT_FLOW_OFFLOAD ,STMT_CONNLIMIT ,STMT_MAP , + STMT_SYNPROXY, };/**
@@ -361,6 +371,7 @@ struct stmt {struct objref_stmt objref;struct flow_stmt flow;struct map_stmt map; +struct synproxy_stmt synproxy; }; };
src/evaluate.c +15 -0
@@ -17,6 +17,7 @@#include <linux/netfilter.h> #include <linux/netfilter_arp.h> #include <linux/netfilter/nf_tables.h> +#include <linux/netfilter/nf_SYNPROXY.h> #include <linux/netfilter_ipv4.h> #include <netinet/ip_icmp.h> #include <netinet/icmp6.h>
@@ -2706,6 +2707,18 @@ static int stmt_evaluate_tproxy(struct eval_ctx *ctx, struct stmt *stmt)return 0 ; } +static int stmt_evaluate_synproxy (struct eval_ctx *ctx,struct stmt *stmt) +{ +if (stmt->synproxy.flags !=0 && + !(stmt->synproxy.flags & (NF_SYNPROXY_OPT_MSS | +NF_SYNPROXY_OPT_WSCALE | +NF_SYNPROXY_OPT_TIMESTAMP | +NF_SYNPROXY_OPT_SACK_PERM ))) +return stmt_error (ctx, stmt,"This flags are not supported for SYNPROXY" ); + +return 0 ; +} +static int stmt_evaluate_dup (struct eval_ctx *ctx,struct stmt *stmt) {int err;
@@ -3050,6 +3063,8 @@ int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)return stmt_evaluate_objref (ctx, stmt);case STMT_MAP :return stmt_evaluate_map (ctx, stmt); +case STMT_SYNPROXY : +return stmt_evaluate_synproxy (ctx, stmt);default :BUG ("unknown statement type %s\n" , stmt->ops->name); }
src/json.c +29 -0
@@ -16,6 +16,7 @@#include <linux/netfilter/nf_log.h> #include <linux/netfilter/nf_nat.h> #include <linux/netfilter/nf_tables.h> +#include <linux/netfilter/nf_SYNPROXY.h> #include <linux/xfrm.h> #include <pwd.h> #include <grp.h>
@@ -1458,6 +1459,34 @@ json_t *tproxy_stmt_json(const struct stmt *stmt, struct output_ctx *octx)return json_pack ("{s:o}" ,"tproxy" , root); } +json_t *synproxy_stmt_json (const struct stmt *stmt,struct output_ctx *octx) +{ +json_t *root =json_object (), *flags =json_array (); + +if (stmt->synproxy.flags &NF_SYNPROXY_OPT_MSS ) +json_object_set_new (root,"mss" , +json_integer (stmt->synproxy.mss)); +if (stmt->synproxy.flags &NF_SYNPROXY_OPT_WSCALE ) +json_object_set_new (root,"wscale" , +json_integer (stmt->synproxy.wscale)); +if (stmt->synproxy.flags &NF_SYNPROXY_OPT_TIMESTAMP ) +json_array_append_new (flags,json_string ("timestamp" )); +if (stmt->synproxy.flags &NF_SYNPROXY_OPT_SACK_PERM ) +json_array_append_new (flags,json_string ("sack-perm" )); + +if (json_array_size (flags) >0 ) +json_object_set_new (root,"flags" , flags); +else +json_decref (flags); + +if (!json_object_size (root)) { +json_decref (root); + root =json_null (); + } + +return json_pack ("{s:o}" ,"synproxy" , root); +} +static json_t *table_print_json_full (struct netlink_ctx *ctx,struct table *table) {
src/netlink_delinearize.c +17 -0
@@ -1010,6 +1010,22 @@ out_err:xfree (stmt); } +static void netlink_parse_synproxy (struct netlink_parse_ctx *ctx, +const struct location *loc, +const struct nftnl_expr *nle) +{ +struct stmt *stmt; + + stmt =synproxy_stmt_alloc (loc); + stmt->synproxy.mss =nftnl_expr_get_u16 (nle,NFTNL_EXPR_SYNPROXY_MSS ); + stmt->synproxy.wscale =nftnl_expr_get_u8 (nle, +NFTNL_EXPR_SYNPROXY_WSCALE ); + stmt->synproxy.flags =nftnl_expr_get_u32 (nle, +NFTNL_EXPR_SYNPROXY_FLAGS ); + + ctx->stmt = stmt; +} +static void netlink_parse_tproxy (struct netlink_parse_ctx *ctx,const struct location *loc,const struct nftnl_expr *nle)
@@ -1476,6 +1492,7 @@ static const struct { { .name = "tcpopt", .parse = netlink_parse_exthdr }, { .name = "flow_offload", .parse = netlink_parse_flow_offload }, { .name = "xfrm", .parse = netlink_parse_xfrm }, + { .name = "synproxy", .parse = netlink_parse_synproxy }, }; static int netlink_parse_expr(const struct nftnl_expr *nle,
src/netlink_linearize.c +17 -0
@@ -1141,6 +1141,21 @@ static void netlink_gen_tproxy_stmt(struct netlink_linearize_ctx *ctx,nftnl_rule_add_expr (ctx->nlr, nle); } +static void netlink_gen_synproxy_stmt (struct netlink_linearize_ctx *ctx, +const struct stmt *stmt) +{ +struct nftnl_expr *nle; + + nle =alloc_nft_expr ("synproxy" ); +nftnl_expr_set_u16 (nle,NFTNL_EXPR_SYNPROXY_MSS , stmt->synproxy.mss); +nftnl_expr_set_u8 (nle,NFTNL_EXPR_SYNPROXY_WSCALE , + stmt->synproxy.wscale); +nftnl_expr_set_u32 (nle,NFTNL_EXPR_SYNPROXY_FLAGS , + stmt->synproxy.flags); + +nftnl_rule_add_expr (ctx->nlr, nle); +} +static void netlink_gen_dup_stmt (struct netlink_linearize_ctx *ctx,const struct stmt *stmt) {
@@ -1382,6 +1397,8 @@ static void netlink_gen_stmt(struct netlink_linearize_ctx *ctx,return netlink_gen_nat_stmt (ctx, stmt);case STMT_TPROXY :return netlink_gen_tproxy_stmt (ctx, stmt); +case STMT_SYNPROXY : +return netlink_gen_synproxy_stmt (ctx, stmt);case STMT_DUP :return netlink_gen_dup_stmt (ctx, stmt);case STMT_QUEUE :
src/parser_bison.y +48 -0
78 lines not on this page · raw message
src/parser_json.c +94 -0
112 lines not on this page · raw message
src/scanner.l +6 -0
@@ -543,6 +543,12 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "osf" { return OSF; } +"synproxy" { return SYNPROXY; } +"mss" { return MSS; } +"wscale" { return WSCALE; } +"timestamp" { return TIMESTAMP; } +"sack-perm" { return SACKPERM; } + "notrack" { return NOTRACK; } "options" { return OPTIONS; }
src/statement.c +51 -0
60 lines not on this page · raw message
tests/py/inet/synproxy.t new file +13 -0
@@ -0,0 +1,13 @@ +:synproxychain;type filter hook input priority 0 + +*ip;synproxyip;synproxychain +*ip6;synproxyip6;synproxychain +*inet;synproxyinet;synproxychain + +synproxy;ok +synproxy mss 1460 wscale 7;ok +synproxy mss 1460 wscale 5 timestamp sack-perm;ok +synproxy timestamp sack-perm;ok +synproxy timestamp;ok +synproxy sack-perm;ok +
tests/py/inet/synproxy.t.json new file +71 -0
71 lines not on this page · raw message
tests/py/inet/synproxy.t.payload new file +72 -0
72 lines not on this page · raw message
Signature · 1 line
2.20.1
Applied, thanks Fernando.