src: add xt compat support
Pablo Neira Ayuso
maintainerPATCH 1/1 src: add xt compat support
With this patch, you can use iptables (over nftables compat layer) and nft at the same time.
% libnftables/examples/./nft-rule-add ip test test
The output looks like:
% nft list table test > ruleset
% cat ruleset
table test {
chain test {
xt iprange [ --src-range 127.0.0.1-127.0.0.4 ] xt LOG [ --log-level 5 --log-tcp-sequence --log-tcp-options --log-ip-options --log-uid ]
}
}You can also reload the rule-set that uses xt extension via:
nft -f ruleset
This adds a new dependency with libxtables. You have to be careful to make sure nft uses the libxtables 7 (the one used by iptables-nftables), otherwise you'll hit problems. This should be resolved by:
1) forward porting iptables-nftables to current head (libxtables 10).
2) freezing binary interface of libxtables to ensure stability.
Benefits:
1) Automatic iptables to nft rule-set translation: We are planning to provide native translations of xt modules to nftables. That should really help to users to migrate from iptables to the new utility.
2) Access to all existing xt modules from nft. As we still lack of native replacements for several xt modules, users can use xt modules.
Limitations:
1) Works via -f and -i, but not from command line yet, as getopt_long in nft considers that xt extension parameters are not known.
2) No xt->parse support yet, this uses xt->x6_parse.
--- configure.ac | 3 + include/expression.h | 10 ++ include/linux/netfilter/nf_tables_compat.h | 20 +++ include/statement.h | 14 ++ include/xt.h | 23 ++++ src/Makefile.in | 1 + src/evaluate.c | 1 + src/expression.c | 24 ++++ src/netlink_delinearize.c | 71 ++++++++++ src/netlink_linearize.c | 97 +++++++++++++ src/parser.y | 56 +++++++- src/scanner.l | 8 ++ src/statement.c | 67 +++++++++ src/xt.c | 206 ++++++++++++++++++++++++++++ 14 files changed, 600 insertions(+), 1 deletion(-) create mode 100644 include/linux/netfilter/nf_tables_compat.h create mode 100644 include/xt.h create mode 100644 src/xt.c
configure.ac +3 -0
@@ -55,6 +55,9 @@ AC_CHECK_LIB([mnl], [mnl_socket_open], , AC_CHECK_LIB([nftables], [nft_rule_alloc], , AC_MSG_ERROR([No suitable version of libnftables found])) +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
@@ -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 {
@@ -242,6 +244,11 @@ struct expr {/* EXPR_CT */ enum nft_ct_keys key; } ct; +struct { +/* EXPR_OPTION */ +const char *id; +struct expr *val; + }; }; };
@@ -333,4 +340,7 @@ extern struct expr *map_expr_alloc(const struct location *loc,extern struct expr *set_ref_expr_alloc (const struct location *loc,struct set *set); +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
@@ -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
@@ -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);
@@ -60,6 +61,16 @@ struct nat_stmt {extern struct stmt *nat_stmt_alloc (const struct location *loc); +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 *
@@ -72,6 +83,7 @@ extern struct stmt *nat_stmt_alloc(const struct location *loc);* @STMT_LOG: log statement * @STMT_REJECT: REJECT statement * @STMT_NAT: NAT statement +* @STMT_XT: XT statement */ enum stmt_types {STMT_INVALID ,
@@ -83,6 +95,7 @@ enum stmt_types {STMT_LOG ,STMT_REJECT ,STMT_NAT , + STMT_XT, };/**
@@ -128,6 +141,7 @@ struct stmt {struct limit_stmt limit;struct reject_stmt reject;struct nat_stmt nat; +struct xt_stmt xt; }; };
include/xt.h new file +23 -0
@@ -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/Makefile.in +1 -0
@@ -22,6 +22,7 @@ nft-obj += gmputil.o nft-obj += utils.o nft-obj += erec.o nft-obj += mnl.o +nft-obj += xt.o nft-obj += parser.o nft-extra-clean-files += parser.c parser.h
src/evaluate.c +1 -0
@@ -1155,6 +1155,7 @@ static int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)case STMT_COUNTER :case STMT_LIMIT :case STMT_LOG : +case STMT_XT :return 0 ;case STMT_EXPRESSION :return stmt_evaluate_expr (ctx, stmt);
src/expression.c +24 -0
@@ -753,3 +753,27 @@ struct expr *set_ref_expr_alloc(const struct location *loc, struct set *set) expr->flags |=EXPR_F_CONSTANT ;return expr; } + +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 +71 -0
89 lines not on this page · raw message
src/netlink_linearize.c +97 -0
115 lines not on this page · raw message
src/parser.y +55 -1
89 lines not on this page · raw message
src/scanner.l +8 -0
@@ -112,6 +112,7 @@ hexstring 0[xX]{hexdigit}+ range ({decstring}?:{decstring}?) letter [a-zA-Z] string ({letter})({letter}|{digit}|[/\-_\.])* +option \-\-({letter}|{digit}|\-)* quotedstring \"[^"]*\" comment #.*$ slash \/
@@ -276,6 +277,8 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "snat" { return SNAT; } "dnat" { return DNAT; } +"xt" { return XT; } + "ll" { return LL_HDR; } "nh" { return NETWORK_HDR; } "th" { return TRANSPORT_HDR; }
@@ -418,6 +421,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 +206 -0
206 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Hi Pablo,
Are you sure you want this feature?
iptables-nftables has been planned to provide full compat with iptables, so it hides the nft commands.
But, little by little, the point is to move on with nft tool only, when people will realize it brings cooler stuff. And I am afraid that, with such patch, we are going to maintain legacy stuff also in nft.
To me I see iptables-nftables being the only entry point for legacy commands, and nowhere else.
Being able to list partially match/target (type and names) would be fine. But manipulating those should be only through iptables-nftables imho.
Br,
Tomasz
Pablo Neira Ayuso
maintainerQuoted 6 lines · Tomasz Bursztyka
Hi Pablo,
Are you sure you want this feature? iptables-nftables has been planned to provide full compat with iptables, so it hides the nft commands.
Quoted 7 lines
But, little by little, the point is to move on with nft tool only, when people will realize it brings cooler stuff.
And I am afraid that, with such patch, we are going to maintain legacy stuff also in nft.To me I see iptables-nftables being the only entry point for legacy commands, and nowhere else.
We can add native nft interfaces to several of the existing xt matches/targets, no need to reimplement all of them from scratch, we can reuse many of the existing xt extensions by adding nft interfaces.
If iptables-nftables starts translating existing matches/targets to native nft expressions, users will get their rule-set automatically translated to native nft expressions. Thus, they will get rid of the old rule expressed using the binary xt interface with no work at all. That can happen progressively, as iptables-nftables will provide more and more native replacements.
Quoted 3 lines
Being able to list partially match/target (type and names) would be fine. But manipulating those should be only through iptables-nftables imho.
With this approach, if we export all rules (including those using xt stuff) via `nft list table', then we cannot use that output to reload it via nft -f. We would have to ignore those rules. That will be problematic.
Hi Pablo,
Quoted 6 lines
Quoted 2 lines
To me I see iptables-nftables being the only entry point for legacy
Quoted 1 line
commands, and nowhere else.
We can add native nft interfaces to several of the existing xt matches/targets, no need to reimplement all of them from scratch, we can reuse many of the existing xt extensions by adding nft interfaces.
Quoted 6 lines
If iptables-nftables starts translating existing matches/targets to native nft expressions, users will get their rule-set automatically translated to native nft expressions. Thus, they will get rid of the old rule expressed using the binary xt interface with no work at all. That can happen progressively, as iptables-nftables will provide more and more native replacements.
Yes that has been the plan and I am committed to it, a patch is coming soon. We will translate, little by little, all xt matches/targets to nft expressions. Through iptables-nftables: it will be fully transparent for the user.
But your patch is bringing the old iptables-like commands into nft, and I think it's not a good idea. Users will have 2 totally different ways to express something in the same tool:
- One very flexible, new, based on a small subset of expressions: the
nft way.
- And one based on the iptables way, which is of course following a
different format than nft commands and misleading in case of the xt match/target not using the xt compat expression anymore.
Let's say an xt match M. User uses nft to add a rule using M:
nft add rule ip filter xt M [ <M match options> ] drop
But what's the user does not know, is that the xt match M will generate pure nft expressions, not using the xt compat expression (no memory blob etc...)
Then: (let's say M matches tcp protocol, port 12345)
nft list table filter
table global {
chain filter input {
ip protocol 6 tcp dport 12345 drop
}
}It's misleading. The user is not retrieving his command here. I am pretty sure lots of users will complain about that. (and you know bad habits are tough: users will also argue to keep such iptables-like mixing forever) And it's going to happen fast, since I believe a lot of xt extensions can be expressed already as pure nft expressions (tcp, snat/dnat, ...).
Imho, as long as a user wants to use iptables-like matches/targets: he should use only iptables-nftables. He will stay with the old fashioned way of managing chains, rules... The day he want to move to nft due to the fact it brings much more features, flexibility and so on... he will use nft, and its command format. No mixing.
We can enforce that in nft: if an xt match/target is found, we print a warning so they should use iptables-nftables instead for handling those. And we print a partial output only: like: ip xt_match <a_match_name> xt_target <a_target_name>, and of course nft -f is not going to handle such.
I understand your point of bringing this feature to the users (for full retro-compat issue), but somehow I feel it's going to be a hassle finally, and it won't help the users to fully move on properly with nft. We have to force them ;) "Want iptables way of doing thing: use iptables-nftables. Want the new features and flexibility: use nft".
Tomasz
Pablo Neira Ayuso
maintainerHi Tomasz,
On Tue, Jul 02, 2013 at 12:04:20PM +0300, Tomasz Bursztyka wrote:
[...]
Quoted 9 lines
nft add rule ip filter xt M [ <M match options> ] drop
But what's the user does not know, is that the xt match M will generate pure nft expressions, not using the xt compat expression (no memory blob etc...)
Then: (let's say M matches tcp protocol, port 12345)nft list table filter
Quoted 8 lines
table global { chain filter input { ip protocol 6 tcp dport 12345 drop } }It's misleading. The user is not retrieving his command here. I am pretty sure lots of users will complain about that.
We can document that xt commands from nft are translated to native whenever possible.
[...]
Quoted 2 lines
We have to force them ;) "Want iptables way of doing thing: use iptables-nftables. Want the new features and flexibility: use nft".
Many users have rule-sets with thousands of rules. Following this approach you propose, they will have to rewrite their rule-set *entirely* to native nft. That's a lot of work and a daunting task, they won't happy about that.
With this patch, users that want to migrate to get the new features can simply load their rule-set via iptables-nftables, then switch to nft to obtain the translation. If there is no native replacement for one of the rule selectors, they can *still* use the new nft. Thus, they can *progressively* migrate to native nft as soon as native replacements for existing features are provided.
Regards.