Skip to contentSkip to conversation

tproxy with nftables

DISCUSSION Andreas Schultz 2 participants 3 messages Jan 5, 2015 – Jan 12, 2015 1 message not in the archive netfilter

lore
Share & tools
lore
#0 lore raw

Hi,

Is it possible to use tproxy with nftables?

It seem that nft has no support for the tproxy table. It might be possible to use a mix of nft and xtable-compat to get tproxy to work on nft.

But I would like to use a map to lookup a match and then use tproxy as action. I there a way to get this working?

Andreas

#1 lore raw
Quoted 3 lines · Andreas Schultz

Hi,

Is it possible to use tproxy with nftables?

No native tproxy support yet and noone is scheduled to work on that anytime soon, sorry.

Quoted 3 lines

It seem that nft has no support for the tproxy table. It might be possible to use a mix of nft and xtable-compat to get tproxy to work on nft.

I posted a patch time ago to:

http://patchwork.ozlabs.org/patch/255896/

the main problem was that getopt() gets confused when this is used from the command-line (so it works fine with nft -f). The internal state of getopt() needs to be reset before calling libxtables to parse the options.

Quoted 2 lines

But I would like to use a map to lookup a match and then use tproxy as action. I there a way to get this working?

I would need to know more on your use-case to be sure that the patch above will help you to achieve this, otherwise wait for the native tproxy support :-(.

#3 lore raw
In reply to a message not in the archive

Hi Pablo,

Quoted 12 lines · ----- On 8 Jan, 2015, at 11:56, Pablo Neira Ayuso [email protected] wrote:
Quoted 3 lines · Andreas Schultz

Hi,

Is it possible to use tproxy with nftables?

No native tproxy support yet and noone is scheduled to work on that anytime soon, sorry.

Quoted 3 lines

It seem that nft has no support for the tproxy table. It might be possible to use a mix of nft and xtable-compat to get tproxy to work on nft.

Quoted 8 lines

I posted a patch time ago to:

http://patchwork.ozlabs.org/patch/255896/

the main problem was that getopt() gets confused when this is used from the command-line (so it works fine with nft -f). The internal state of getopt() needs to be reset before calling libxtables to parse the options.

That patch does not apply to the nftable 0.4. I've forward ported it (patch attached) and 'nft list' does work fine. However, attempting to restore that output using 'nft -f' fails. It tried to debug it, but my grasp of the nft structures is not good enough to understand what exactly is wrong.

As simple chain like:

table mangle {
	chain test {
		tcp dport 80 counter packets 0 bytes 0 xt TPROXY [ --on-port 3128 --on-ip 0.0.0.0 --tproxy-mark 0x80000000/0x80000000 ]
	}
}

results in:

# nft -f rule
requesting `TPROXY' rev=1 type=1 via nft_compat
1:1:1-2: Error: Could not process rule: Invalid argument
Quoted 7 lines
Quoted 3 lines

But I would like to use a map to lookup a match and then use tproxy as action. I there a way to get this working?

I would need to know more on your use-case to be sure that the patch above will help you to achieve this, otherwise wait for the native tproxy support :-(.

Andreas

configure.ac +3 -0
Index: nftables-0.4/configure.ac
===================================================================
@@ -73,6 +73,9 @@ AM_CONDITIONAL([BUILD_PDF], [test "$DBLA
 PKG_CHECK_MODULES([LIBMNL], [libmnl >= 1.0.3])
 PKG_CHECK_MODULES([LIBNFTNL], [libnftnl >= 1.0.2])
 
+AC_CHECK_LIB([xtables], [xtables_find_target], ,
+	     AC_MSG_ERROR([No suitable version of libxtables found]))
+
 AC_CHECK_LIB([gmp], [__gmpz_init], ,
 	     AC_MSG_ERROR([No suitable version of libgmp found]))
 
include/expression.h +10 -0
Index: nftables-0.4/include/expression.h
===================================================================
@@ -32,6 +32,7 @@
  * @EXPR_UNARY:		byteorder conversion, generated during evaluation
  * @EXPR_BINOP:		binary operations (bitwise, shifts)
  * @EXPR_RELATIONAL:	equality and relational expressions
+ * @EXPR_OPTION:	--option value expression
  */
 enum expr_types {
 	EXPR_INVALID,
@@ -53,6 +54,7 @@ enum expr_types {
 	EXPR_UNARY,
 	EXPR_BINOP,
 	EXPR_RELATIONAL,
+	EXPR_OPTION,
 };
 
 enum ops {
@@ -265,6 +267,11 @@ struct expr {
 			/* EXPR_CT */
 			enum nft_ct_keys	key;
 		} ct;
+		struct {
+			/* EXPR_OPTION */
+			const char		*id;
+			struct expr		*val;
+		};
 	};
 };
 
@@ -367,4 +374,7 @@ extern struct expr *set_ref_expr_alloc(c
 extern void range_expr_value_low(mpz_t rop, const struct expr *expr);
 extern void range_expr_value_high(mpz_t rop, const struct expr *expr);
 
+struct expr *option_expr_alloc(const struct location *loc, const char *id,
+			       struct expr *value);
+
 #endif /* NFTABLES_EXPRESSION_H */
include/linux/netfilter/nf_tables_compat.h new file +20 -0
Index: nftables-0.4/include/linux/netfilter/nf_tables_compat.h
===================================================================
@@ -0,0 +1,20 @@
+#ifndef _NFT_COMPAT_NFNETLINK_H_
+#define _NFT_COMPAT_NFNETLINK_H_
+
+#define NFT_COMPAT_NAME_MAX	32
+
+enum {
+	NFNL_MSG_COMPAT_GET,
+	NFNL_MSG_COMPAT_MAX
+};
+
+enum {
+	NFTA_COMPAT_UNSPEC = 0,
+	NFTA_COMPAT_NAME,
+	NFTA_COMPAT_REV,
+	NFTA_COMPAT_TYPE,
+	__NFTA_COMPAT_MAX,
+};
+#define NFTA_COMPAT_MAX (__NFTA_COMPAT_MAX - 1)
+
+#endif
include/statement.h +14 -0
Index: nftables-0.4/include/statement.h
===================================================================
@@ -3,6 +3,7 @@
 
 #include <list.h>
 #include <expression.h>
+#include <xt.h>
 
 extern struct stmt *expr_stmt_alloc(const struct location *loc,
 				    struct expr *expr);
@@ -104,6 +105,16 @@ extern struct stmt *ct_stmt_alloc(const
 				  enum nft_ct_keys key,
 				  struct expr *expr);
 
+struct xt_stmt {
+	const char		*name;
+	struct xtables_target	*target;
+	struct xtables_match	*match;
+	void			*entry;
+	struct expr		*list;
+};
+
+extern struct stmt *xt_stmt_alloc(const struct location *loc);
+
 /**
  * enum stmt_types - statement types
  *
@@ -120,6 +131,7 @@ extern struct stmt *ct_stmt_alloc(const
  * @STMT_REDIR:		redirect statement
  * @STMT_QUEUE:		QUEUE statement
  * @STMT_CT:		conntrack statement
+ * @STMT_XT:		XT statement
  */
 enum stmt_types {
 	STMT_INVALID,
@@ -135,6 +147,7 @@ enum stmt_types {
 	STMT_REDIR,
 	STMT_QUEUE,
 	STMT_CT,
+	STMT_XT,
 };
 
 /**
@@ -184,6 +197,7 @@ struct stmt {
 		struct redir_stmt	redir;
 		struct queue_stmt	queue;
 		struct ct_stmt		ct;
+		struct xt_stmt		xt;
 	};
 };
 
include/xt.h new file +23 -0
Index: nftables-0.4/include/xt.h
===================================================================
@@ -0,0 +1,23 @@
+#ifndef _NFT_XT_H_
+#define _NFT_XT_H_
+
+#include <xtables.h>
+
+enum nft_xt_ext_type {
+	NFT_XT_MATCH = 0,
+	NFT_XT_TARGET,
+};
+
+struct nft_xt_ext {
+	enum nft_xt_ext_type	type;
+	union {
+		struct xtables_target *tg;
+		struct xtables_match *mt;
+	};
+	void			*info;
+};
+
+int xt_argv_to_binary(const char *name, int argc, char *argv[],
+		      struct nft_xt_ext *ext);
+
+#endif
src/evaluate.c +1 -0
Index: nftables-0.4/src/evaluate.c
===================================================================
@@ -1633,6 +1633,7 @@ int stmt_evaluate(struct eval_ctx *ctx,
 	switch (stmt->ops->type) {
 	case STMT_COUNTER:
 	case STMT_LIMIT:
+	case STMT_XT:
 		return 0;
 	case STMT_EXPRESSION:
 		return stmt_evaluate_expr(ctx, stmt);
src/expression.c +24 -0
Index: nftables-0.4/src/expression.c
===================================================================
@@ -917,3 +917,27 @@ void range_expr_value_high(mpz_t rop, co
 		BUG("invalid range expression type %s\n", expr->ops->name);
 	}
 }
+
+static void option_expr_destroy(struct expr *expr)
+{
+	xfree(expr->id);
+	expr_free(expr->val);
+}
+
+static const struct expr_ops option_expr_ops = {
+	.type		= EXPR_OPTION,
+	.name		= "option",
+	.destroy	= option_expr_destroy,
+};
+
+struct expr *option_expr_alloc(const struct location *loc, const char *id,
+			       struct expr *value)
+{
+	struct expr *expr;
+
+	expr = expr_alloc(loc, &option_expr_ops, &invalid_type,
+			  BYTEORDER_INVALID, 0);
+	expr->id = id;
+	expr->val = value;
+	return expr;
+}
src/netlink_delinearize.c +72 -0

90 lines not on this page · raw message

src/netlink_linearize.c +112 -0

130 lines not on this page · raw message

src/scanner.l +8 -0
Index: nftables-0.4/src/scanner.l
===================================================================
@@ -113,6 +113,7 @@ hexstring	0[xX]{hexdigit}+
 range		({decstring}?:{decstring}?)
 letter		[a-zA-Z]
 string		({letter})({letter}|{digit}|[/\-_\.])*
+option		\-\-({letter}|{digit}|\-)*
 quotedstring	\"[^"]*\"
 comment		#.*$
 slash		\/
@@ -325,6 +326,8 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr
 "fully-random"		{ return FULLY_RANDOM; }
 "persistent"		{ return PERSISTENT; }
 
+"xt"			{ return XT; }
+
 "ll"			{ return LL_HDR; }
 "nh"			{ return NETWORK_HDR; }
 "th"			{ return TRANSPORT_HDR; }
@@ -488,6 +491,11 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr
 				return STRING;
 			}
 
+{option}		{
+				yylval->string = xstrdup(yytext);
+				return OPTION;
+			}
+
 \\{newline}		{
 				reset_pos(yyget_extra(yyscanner), yylloc);
 			}
src/statement.c +67 -0

70 lines not on this page · raw message

src/xt.c new file +210 -0

210 lines not on this page · raw message

src/Makefile.am +1 -0
Index: nftables-0.4/src/Makefile.am
===================================================================
@@ -44,6 +44,7 @@ nft_SOURCES =	main.c				\
 		utils.c				\
 		erec.c				\
 		mnl.c				\
+		xt.c				\
 		scanner.l			\
 		parser_bison.y
 
src/parser_bison.y +57 -1

91 lines not on this page · raw message