include: cache ip_tables.h, ip6_tables.h, arp_tables.h and ebtables.h
Subsystems (2)
PATCH v3 1/3 include: cache ip_tables.h, ip6_tables.h, arp_tables.h and ebtables.h
From: Pablo Neira <[email protected]>
The xt over nft support that comes in follow up patches need this, and update the corresponding Makefile.am.
Based on patch from Arturo Borrero Gonzalez.
---
Resubmission of xt compat support preparatory patch.
configure.ac | 4 + include/linux/Makefile.am | 7 +- include/linux/netfilter_arp/Makefile.am | 1 + include/linux/netfilter_arp/arp_tables.h | 204 ++++++++++++++++++++++ include/linux/netfilter_bridge/Makefile.am | 1 + include/linux/netfilter_bridge/ebtables.h | 266 +++++++++++++++++++++++++++++ include/linux/netfilter_ipv4/Makefile.am | 1 + include/linux/netfilter_ipv4/ip_tables.h | 227 ++++++++++++++++++++++++ include/linux/netfilter_ipv6/Makefile.am | 1 + include/linux/netfilter_ipv6/ip6_tables.h | 265 ++++++++++++++++++++++++++++ 10 files changed, 976 insertions(+), 1 deletion(-) create mode 100644 include/linux/netfilter_arp/Makefile.am create mode 100644 include/linux/netfilter_arp/arp_tables.h create mode 100644 include/linux/netfilter_bridge/Makefile.am create mode 100644 include/linux/netfilter_bridge/ebtables.h create mode 100644 include/linux/netfilter_ipv4/Makefile.am create mode 100644 include/linux/netfilter_ipv4/ip_tables.h create mode 100644 include/linux/netfilter_ipv6/Makefile.am create mode 100644 include/linux/netfilter_ipv6/ip6_tables.h
configure.ac +4 -0
@@ -126,6 +126,10 @@ AC_CONFIG_FILES([ \ include/Makefile \ include/linux/Makefile \ include/linux/netfilter/Makefile \ + include/linux/netfilter_arp/Makefile \ + include/linux/netfilter_bridge/Makefile \ + include/linux/netfilter_ipv4/Makefile \ + include/linux/netfilter_ipv6/Makefile \ doc/Makefile \ files/Makefile \ files/nftables/Makefile \
include/linux/Makefile.am +6 -1
@@ -1,4 +1,9 @@ -SUBDIRS = netfilter +SUBDIRS = netfilter \ + netfilter_arp \ + netfilter_bridge \ + netfilter_ipv4 \ + netfilter_ipv6 + noinst_HEADERS = netfilter_arp.h \ netfilter_bridge.h \ netfilter_decnet.h \
include/linux/netfilter_arp/Makefile.am new file +1 -0
@@ -0,0 +1 @@ +noinst_HEADERS = arp_tables.h
include/linux/netfilter_arp/arp_tables.h new file +204 -0
204 lines not on this page · raw message
include/linux/netfilter_bridge/Makefile.am new file +1 -0
@@ -0,0 +1 @@ +noinst_HEADERS = ebtables.h
include/linux/netfilter_bridge/ebtables.h new file +266 -0
266 lines not on this page · raw message
include/linux/netfilter_ipv4/Makefile.am new file +1 -0
@@ -0,0 +1 @@ +noinst_HEADERS = ip_tables.h
include/linux/netfilter_ipv4/ip_tables.h new file +227 -0
227 lines not on this page · raw message
include/linux/netfilter_ipv6/Makefile.am new file +1 -0
@@ -0,0 +1 @@ +noinst_HEADERS = ip6_tables.h
include/linux/netfilter_ipv6/ip6_tables.h new file +265 -0
265 lines not on this page · raw message
Signature · 1 line
2.9.0
PATCH v3 2/3 src: expose delinearize/linearize structures and stmt_error()
From: Pablo Neira <[email protected]>
Needed by the follow up xt compatibility layer patch.
---
Resubmission of xt compat support preparatory patch.
include/erec.h | 12 ++++++++++++ include/netlink.h | 14 ++++++++++++++ src/erec.c | 17 +++++++++++++++++ src/evaluate.c | 34 ++++++---------------------------- src/netlink_delinearize.c | 14 -------------- 5 files changed, 49 insertions(+), 42 deletions(-)
include/erec.h +12 -0
@@ -61,4 +61,16 @@ static inline void erec_queue(struct error_record *erec, extern void erec_print(FILE *f, const struct error_record *erec); extern void erec_print_list(FILE *f, struct list_head *list); +struct eval_ctx; + +extern int __fmtstring(4, 5) __binary_error(struct eval_ctx *ctx, + const struct location *l1, + const struct location *l2, + const char *fmt, ...); + +#define stmt_error(ctx, s1, fmt, args...) \ + __binary_error(ctx, &(s1)->location, NULL, fmt, ## args) +#define stmt_binary_error(ctx, s1, s2, fmt, args...) \ + __binary_error(ctx, &(s1)->location, &(s2)->location, fmt, ## args) + #endif /* NFTABLES_EREC_H */
include/netlink.h +14 -0
@@ -12,6 +12,20 @@#include <rule.h> +struct netlink_parse_ctx { +struct list_head *msgs; +struct table *table; +struct rule *rule; +struct stmt *stmt; +struct expr *registers[1 +NFT_REG32_15 -NFT_REG32_00 +1 ]; +}; + +struct rule_pp_ctx { +struct proto_ctx pctx; +struct payload_dep_ctx pdctx; +struct stmt *stmt; +}; +extern const struct input_descriptor indesc_netlink;extern const struct location netlink_location;
src/erec.c +17 -0
@@ -176,3 +176,20 @@ void erec_print_list(FILE *f, struct list_head *list)erec_destroy (erec); } } + +int __fmtstring(4, 5) __binary_error(struct eval_ctx *ctx, + const struct location *l1, + const struct location *l2, + const char *fmt, ...) +{ + struct error_record *erec; +va_list ap; + +va_start (ap, fmt); + erec =erec_vcreate (EREC_ERROR , l1, fmt, ap); +if (l2 !=NULL ) +erec_add_location (erec, l2); +va_end (ap); +erec_queue (erec, ctx->msgs); +return -1 ; +}
src/evaluate.c +6 -28
@@ -37,40 +37,18 @@ static const char *byteorder_names[] = { [BYTEORDER_BIG_ENDIAN] = "big endian", }; -static int __fmtstring(4, 5) __stmt_binary_error(struct eval_ctx *ctx, - const struct location *l1, - const struct location *l2, - const char *fmt, ...) -{ - struct error_record *erec; - va_list ap; - - va_start(ap, fmt); - erec = erec_vcreate(EREC_ERROR, l1, fmt, ap); - if (l2 != NULL) - erec_add_location(erec, l2); - va_end(ap); - erec_queue(erec, ctx->msgs); - return -1; - -} - -#define stmt_error(ctx, s1, fmt, args...) \ - __stmt_binary_error(ctx, &(s1)->location, NULL, fmt, ## args) -#define stmt_binary_error(ctx, s1, s2, fmt, args...) \ - __stmt_binary_error(ctx, &(s1)->location, &(s2)->location, fmt, ## args) #define chain_error(ctx, s1, fmt, args...) \ - __stmt_binary_error(ctx, &(s1)->location, NULL, fmt, ## args) + __binary_error(ctx, &(s1)->location, NULL, fmt, ## args) #define monitor_error(ctx, s1, fmt, args...) \ - __stmt_binary_error(ctx, &(s1)->location, NULL, fmt, ## args) + __binary_error(ctx, &(s1)->location, NULL, fmt, ## args) #define cmd_error(ctx, fmt, args...) \ - __stmt_binary_error(ctx, &(ctx->cmd)->location, NULL, fmt, ## args) + __binary_error(ctx, &(ctx->cmd)->location, NULL, fmt, ## args) #define handle_error(ctx, fmt, args...) \ - __stmt_binary_error(ctx, &ctx->cmd->handle.handle.location, NULL, fmt, ## args) + __binary_error(ctx, &ctx->cmd->handle.handle.location, NULL, fmt, ## args) #define position_error(ctx, fmt, args...) \ - __stmt_binary_error(ctx, &ctx->cmd->handle.position.location, NULL, fmt, ## args) + __binary_error(ctx, &ctx->cmd->handle.position.location, NULL, fmt, ## args) #define handle_position_error(ctx, fmt, args...) \ - __stmt_binary_error(ctx, &ctx->cmd->handle.handle.location, &ctx->cmd->handle.position.location, fmt, ## args) + __binary_error(ctx, &ctx->cmd->handle.handle.location, &ctx->cmd->handle.position.location, fmt, ## args) static int __fmtstring(3, 4) set_error(struct eval_ctx *ctx, const struct set *set,
src/netlink_delinearize.c +0 -14
@@ -27,14 +27,6 @@#include <sys/socket.h> #include <libnftnl/udata.h> -struct netlink_parse_ctx { -struct list_head *msgs; -struct table *table; -struct rule *rule; -struct stmt *stmt; -struct expr *registers[1 +NFT_REG32_15 -NFT_REG32_00 +1 ]; -}; -static int netlink_parse_expr (const struct nftnl_expr *nle,struct netlink_parse_ctx *ctx);
@@ -1047,12 +1039,6 @@ struct stmt *netlink_parse_set_expr(const struct set *set,return pctx->stmt; } -struct rule_pp_ctx { -struct proto_ctx pctx; -struct payload_dep_ctx pdctx; -struct stmt *stmt; -}; -static void expr_postprocess (struct rule_pp_ctx *ctx,struct expr **exprp);static void integer_type_postprocess (struct expr *expr)
Signature · 1 line
2.9.0
PATCH v3 3/3 src: add xt compat support
In reply to Pablo M. Bermudo GarayFrom: Pablo Neira <[email protected]>
At compilation time, you have to pass this option.
# ./configure --with-xtables
And libxtables needs to be installed in your system.
This patch allows to list a ruleset containing xt extensions loaded through iptables-compat-restore tool.
Example:
$ cat iptables-save *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -p tcp -m multiport --dports 80,81 -j REJECT COMMIT
$ sudo iptables-compat-restore iptables-save
$ sudo nft list chain filter INPUT
table ip filter {
chain INPUT {
type filter hook input priority 0; policy accept;
ip protocol tcp tcp dport { 80,81} counter packets 0 bytes 0 reject
}
}A translation of the extension is shown if this is available. In other case, match or target definition is preceded by a hash:
$ sudo nft list chain mangle POSTROUTING
table ip mangle {
chain POSTROUTING {
type filter hook postrouting priority -150; policy accept;
ip protocol tcp tcp dport 80 counter packets 0 bytes 0 # CLASSIFY set 20:10
^^^
}
}If the whole ruleset is translatable, the users can (re)load it using "nft -f" and get nft native support for all their rules.
---
New version of the original work made by Arturo and Pablo.
Work in progress:
- Fix extra space between matches.
- Fix escaped quotation marks issue.
- Mark rules using xt compat support.
configure.ac | 13 +- include/linux/netfilter/nf_tables_compat.h | 38 ++ include/statement.h | 34 ++ include/xt.h | 52 +++ src/Makefile.am | 8 + src/evaluate.c | 3 + src/netlink_delinearize.c | 6 + src/statement.c | 25 ++ src/xt.c | 575 +++++++++++++++++++++++++++++ 9 files changed, 753 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 +12 -1
@@ -92,6 +92,16 @@ AC_DEFINE([HAVE_LIBREADLINE], [1], []) AC_SUBST(with_cli) AM_CONDITIONAL([BUILD_CLI], [test "x$with_cli" != xno]) +AC_ARG_WITH([xtables], [AS_HELP_STRING([--with-xtables], + [Use libxtables for iptables interaction)])], + [with_libxtables=yes], [with_libxtables=no]) +AS_IF([test "x$with_libxtables" != xno], [ +PKG_CHECK_MODULES([XTABLES], [xtables >= 1.4.21]) +AC_DEFINE([HAVE_LIBXTABLES], [1], [0]) +]) +AC_SUBST(with_libxtables) +AM_CONDITIONAL([BUILD_XTABLES], [test "x$with_libxtables" == xyes]) + # Checks for header files. AC_HEADER_STDC AC_HEADER_ASSERT
@@ -140,4 +150,5 @@ echo " nft configuration: cli support: ${with_cli} enable debugging: ${with_debug} - use mini-gmp: ${with_mini_gmp}" + use mini-gmp: ${with_mini_gmp} + libxtables support: ${with_libxtables}"
include/linux/netfilter/nf_tables_compat.h new file +38 -0
@@ -0,0 +1,38 @@ +#ifndef _NFT_COMPAT_NFNETLINK_H_ +#define _NFT_COMPAT_NFNETLINK_H_ + +enum nft_target_attributes { +NFTA_TARGET_UNSPEC , +NFTA_TARGET_NAME , +NFTA_TARGET_REV , +NFTA_TARGET_INFO , + __NFTA_TARGET_MAX +}; +#define NFTA_TARGET_MAX (__NFTA_TARGET_MAX - 1) + +enum nft_match_attributes { +NFTA_MATCH_UNSPEC , +NFTA_MATCH_NAME , +NFTA_MATCH_REV , +NFTA_MATCH_INFO , + __NFTA_MATCH_MAX +}; +#define NFTA_MATCH_MAX (__NFTA_MATCH_MAX - 1) + +#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 +34 -0
@@ -148,6 +148,37 @@ struct flow_stmt {extern struct stmt *flow_stmt_alloc (const struct location *loc);/** +* enum nft_xt_type - xtables statement types +* +* @NFT_XT_MATCH: match +* @NFT_XT_TARGET: target +* @NFT_XT_WATCHER: watcher (only for the bridge family) +*/ +enum nft_xt_type { +NFT_XT_MATCH =0 , +NFT_XT_TARGET , +NFT_XT_WATCHER , +NFT_XT_MAX +}; + +struct xtables_match ; +struct xtables_target ; + +struct xt_stmt { +const char *name; +enum nft_xt_type type; +uint32_t proto; +union { +struct xtables_match *match; +struct xtables_target *target; + }; +const char *opts; +void *entry; +}; + +extern struct stmt *xt_stmt_alloc (const struct location *loc); + +/** * enum stmt_types - statement types * * @STMT_INVALID: uninitialised
@@ -168,6 +199,7 @@ extern struct stmt *flow_stmt_alloc(const struct location *loc);* @STMT_SET: set statement * @STMT_DUP: dup statement * @STMT_FWD: forward statement +* @STMT_XT: XT statement */ enum stmt_types {STMT_INVALID ,
@@ -188,6 +220,7 @@ enum stmt_types {STMT_SET ,STMT_DUP ,STMT_FWD , + STMT_XT, };/**
@@ -243,6 +276,7 @@ struct stmt {struct set_stmt set;struct dup_stmt dup;struct fwd_stmt fwd; +struct xt_stmt xt; }; };
include/xt.h new file +52 -0
52 lines not on this page · raw message
src/Makefile.am +8 -0
@@ -8,6 +8,9 @@ AM_CPPFLAGS += -DDEFAULT_INCLUDE_PATH="\"${sysconfdir}\"" \ if BUILD_DEBUG AM_CPPFLAGS += -g -DDEBUG endif +if BUILD_XTABLES +AM_CPPFLAGS += ${XTABLES_CFLAGS} +endif AM_CFLAGS = -Wall \ -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations \
@@ -59,3 +62,8 @@ nft_SOURCES += mini-gmp.c endif nft_LDADD = ${LIBMNL_LIBS} ${LIBNFTNL_LIBS} + +if BUILD_XTABLES +nft_SOURCES += xt.c +nft_LDADD += ${XTABLES_LIBS} +endif
src/evaluate.c +3 -0
@@ -28,6 +28,7 @@#include <erec.h> #include <gmputil.h> #include <utils.h> +#include <xt.h> static int expr_evaluate (struct eval_ctx *ctx,struct expr **expr);
@@ -2294,6 +2295,8 @@ int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)return stmt_evaluate_fwd (ctx, stmt);case STMT_SET :return stmt_evaluate_set (ctx, stmt); +case STMT_XT : +return stmt_evaluate_xt (ctx, stmt);default :BUG ("unknown statement type %s\n" , stmt->ops->name); }
src/netlink_delinearize.c +6 -0
@@ -26,6 +26,7 @@#include <erec.h> #include <sys/socket.h> #include <libnftnl/udata.h> +#include <xt.h> static int netlink_parse_expr (const struct nftnl_expr *nle,struct netlink_parse_ctx *ctx);
@@ -986,6 +987,8 @@ static const struct { { .name = "queue", .parse = netlink_parse_queue }, { .name = "dynset", .parse = netlink_parse_dynset }, { .name = "fwd", .parse = netlink_parse_fwd }, + { .name = "target", .parse = netlink_parse_target }, + { .name = "match", .parse = netlink_parse_match }, }; static int netlink_parse_expr(const struct nftnl_expr *nle,
@@ -1798,6 +1801,9 @@ static void rule_parse_postprocess(struct netlink_parse_ctx *ctx, struct rule *rif (stmt->fwd.to !=NULL )expr_postprocess (&rctx, &stmt->fwd.to);break ; +case STMT_XT : +stmt_xt_postprocess (&rctx, stmt, rule); +break ;default :break ; }
src/statement.c +25 -0
@@ -23,6 +23,7 @@#include <statement.h> #include <utils.h> #include <list.h> +#include <xt.h> #include <netinet/in.h> #include <linux/netfilter/nf_nat.h>
@@ -567,3 +568,27 @@ struct stmt *fwd_stmt_alloc(const struct location *loc) {return stmt_alloc (loc, &fwd_stmt_ops); } + +static void xt_stmt_print (const struct stmt *stmt) +{ +xt_stmt_xlate (stmt); +} + +static void xt_stmt_destroy (struct stmt *stmt) +{ +xfree (stmt->xt.name); +xfree (stmt->xt.opts); +xt_stmt_release (stmt); +} + +static const struct stmt_ops xt_stmt_ops = { + .type =STMT_XT , + .name ="xt" , + .print = xt_stmt_print, + .destroy = xt_stmt_destroy, +}; + +struct stmt *xt_stmt_alloc (const struct location *loc) +{ +return stmt_alloc (loc, &xt_stmt_ops); +}
src/xt.c new file +575 -0
575 lines not on this page · raw message
Signature · 1 line
2.9.0
Pablo Neira Ayuso
maintainerRe: [nft PATCH 3/3 v3] src: add xt compat support
Quoted 13 lines · Pablo M. Bermudo Garay
src/evaluate.c +1
-0@@ -28,6 +28,7 @@#include <erec.h> #include <gmputil.h> #include <utils.h> +#include <xt.h> static int expr_evaluate (struct eval_ctx *ctx,struct expr **expr);
src/evaluate.c int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt) · line 2299
Quoted 5 lines
src/evaluate.c +2
-0@@ -2294,6 +2295,8 @@ int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)return stmt_evaluate_fwd (ctx, stmt);case STMT_SET :return stmt_evaluate_set (ctx, stmt); +case STMT_XT : +return stmt_evaluate_xt (ctx, stmt);
You can get rid of stmt_evaluate_xt() and the underlying code there. We don't exercise this code anymore.
This removal simplify your patchset even more.