Skip to contentSkip to conversation

src: add parse_ctx object

PATCH In review Pablo Neira Ayuso 2 participants 3 messages Aug 7, 2019 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 src: add parse_ctx object

This object stores the dynamic symbol tables that are loaded from files. Pass this object to datatype parse functions, although this is not used yet.

---
 include/datatype.h | 14 +++++++++++---
 include/nftables.h |  8 ++++++++
 src/ct.c           |  3 ++-
 src/datatype.c     | 46 +++++++++++++++++++++++++++++-----------------
 src/evaluate.c     |  6 ++++--
 src/meta.c         | 17 +++++++++++------
 src/rt.c           |  5 +++--
 7 files changed, 68 insertions(+), 31 deletions(-)
include/datatype.h +11 -3
index 63617ebd2753..018f013aea04 100644
@@ -123,6 +123,7 @@ enum datatype_flags {
 	DTYPE_F_PREFIX		= (1 << 1),
 };
 
+struct parse_ctx;
 /**
  * struct datatype
  *
@@ -154,7 +155,8 @@ struct datatype {
 						 struct output_ctx *octx);
 	json_t				*(*json)(const struct expr *expr,
 						 struct output_ctx *octx);
-	struct error_record		*(*parse)(const struct expr *sym,
+	struct error_record		*(*parse)(struct parse_ctx *ctx,
+						  const struct expr *sym,
 						  struct expr **res);
 	const struct symbol_table	*sym_tbl;
 	unsigned int			refcnt;
@@ -166,7 +168,12 @@ extern struct datatype *datatype_get(const struct datatype *dtype);
 extern void datatype_set(struct expr *expr, const struct datatype *dtype);
 extern void datatype_free(const struct datatype *dtype);
 
-extern struct error_record *symbol_parse(const struct expr *sym,
+struct parse_ctx {
+	struct symbol_tables	*tbl;
+};
+
+extern struct error_record *symbol_parse(struct parse_ctx *ctx,
+					 const struct expr *sym,
 					 struct expr **res);
 extern void datatype_print(const struct expr *expr, struct output_ctx *octx);
 
@@ -218,7 +225,8 @@ struct symbol_table {
 	struct symbolic_constant	symbols[];
 };
 
-extern struct error_record *symbolic_constant_parse(const struct expr *sym,
+extern struct error_record *symbolic_constant_parse(struct parse_ctx *ctx,
+						    const struct expr *sym,
 						    const struct symbol_table *tbl,
 						    struct expr **res);
 extern void symbolic_constant_print(const struct symbol_table *tbl,
include/nftables.h +8 -0
index ed446e2d16cf..407d76130e9f 100644
@@ -15,6 +15,13 @@ struct cookie {
 	size_t pos;
 };
 
+struct symbol_tables {
+	const struct symbol_table	*mark;
+	const struct symbol_table	*devgroup;
+	const struct symbol_table	*ct_label;
+	const struct symbol_table	*realm;
+};
+
 struct output_ctx {
 	unsigned int flags;
 	union {
@@ -25,6 +32,7 @@ struct output_ctx {
 		FILE *error_fp;
 		struct cookie error_cookie;
 	};
+	struct symbol_tables tbl;
 };
 
 static inline bool nft_output_reversedns(const struct output_ctx *octx)
src/ct.c +2 -1
index 14cc0e5e8a4e..c66b327a2237 100644
@@ -171,7 +171,8 @@ static void ct_label_type_print(const struct expr *expr,
 	nft_print(octx, "%lu", bit);
 }
 
-static struct error_record *ct_label_type_parse(const struct expr *sym,
+static struct error_record *ct_label_type_parse(struct parse_ctx *ctx,
+						const struct expr *sym,
 						struct expr **res)
 {
 	const struct symbolic_constant *s;
src/datatype.c +29 -17

139 lines not on this page · raw message

src/evaluate.c +4 -2
index 48c65cd2f35a..df8e808f92a9 100644
@@ -223,6 +223,7 @@ static int set_not_found(struct eval_ctx *ctx, const struct location *loc,
  */
 static int expr_evaluate_symbol(struct eval_ctx *ctx, struct expr **expr)
 {
+	struct parse_ctx parse_ctx = { .tbl = &ctx->nft->output.tbl, };
 	struct error_record *erec;
 	struct table *table;
 	struct set *set;
@@ -231,7 +232,7 @@ static int expr_evaluate_symbol(struct eval_ctx *ctx, struct expr **expr)
 	switch ((*expr)->symtype) {
 	case SYMBOL_VALUE:
 		datatype_set(*expr, ctx->ectx.dtype);
-		erec = symbol_parse(*expr, &new);
+		erec = symbol_parse(&parse_ctx, *expr, &new);
 		if (erec != NULL) {
 			erec_queue(erec, ctx->msgs);
 			return -1;
@@ -2541,10 +2542,11 @@ static int stmt_evaluate_reject_default(struct eval_ctx *ctx,
 
 static int stmt_evaluate_reject_icmp(struct eval_ctx *ctx, struct stmt *stmt)
 {
+	struct parse_ctx parse_ctx = { .tbl = &ctx->nft->output.tbl, };
 	struct error_record *erec;
 	struct expr *code;
 
-	erec = symbol_parse(stmt->reject.expr, &code);
+	erec = symbol_parse(&parse_ctx, stmt->reject.expr, &code);
 	if (erec != NULL) {
 		erec_queue(erec, ctx->msgs);
 		return -1;
src/meta.c +11 -6
index 1e8964eb48c4..5c0c4e29c062 100644
@@ -68,7 +68,8 @@ static void tchandle_type_print(const struct expr *expr,
 	}
 }
 
-static struct error_record *tchandle_type_parse(const struct expr *sym,
+static struct error_record *tchandle_type_parse(struct parse_ctx *ctx,
+						const struct expr *sym,
 						struct expr **res)
 {
 	uint32_t handle;
@@ -142,7 +143,8 @@ static void ifindex_type_print(const struct expr *expr, struct output_ctx *octx)
 		nft_print(octx, "%d", ifindex);
 }
 
-static struct error_record *ifindex_type_parse(const struct expr *sym,
+static struct error_record *ifindex_type_parse(struct parse_ctx *ctx,
+					       const struct expr *sym,
 					       struct expr **res)
 {
 	int ifindex;
@@ -220,7 +222,8 @@ static void uid_type_print(const struct expr *expr, struct output_ctx *octx)
 	expr_basetype(expr)->print(expr, octx);
 }
 
-static struct error_record *uid_type_parse(const struct expr *sym,
+static struct error_record *uid_type_parse(struct parse_ctx *ctx,
+					   const struct expr *sym,
 					   struct expr **res)
 {
 	struct passwd *pw;
@@ -273,7 +276,8 @@ static void gid_type_print(const struct expr *expr, struct output_ctx *octx)
 	expr_basetype(expr)->print(expr, octx);
 }
 
-static struct error_record *gid_type_parse(const struct expr *sym,
+static struct error_record *gid_type_parse(struct parse_ctx *ctx,
+					   const struct expr *sym,
 					   struct expr **res)
 {
 	struct group *gr;
@@ -355,10 +359,11 @@ static void devgroup_type_print(const struct expr *expr,
 	return symbolic_constant_print(devgroup_tbl, expr, true, octx);
 }
 
-static struct error_record *devgroup_type_parse(const struct expr *sym,
+static struct error_record *devgroup_type_parse(struct parse_ctx *ctx,
+						const struct expr *sym,
 						struct expr **res)
 {
-	return symbolic_constant_parse(sym, devgroup_tbl, res);
+	return symbolic_constant_parse(ctx, sym, devgroup_tbl, res);
 }
 
 const struct datatype devgroup_type = {
src/rt.c +3 -2
index 3ad77bcdda4d..cdd5e9d82b44 100644
@@ -40,10 +40,11 @@ static void realm_type_print(const struct expr *expr, struct output_ctx *octx)
 	return symbolic_constant_print(realm_tbl, expr, true, octx);
 }
 
-static struct error_record *realm_type_parse(const struct expr *sym,
+static struct error_record *realm_type_parse(struct parse_ctx *ctx,
+					     const struct expr *sym,
 					     struct expr **res)
 {
-	return symbolic_constant_parse(sym, realm_tbl, res);
+	return symbolic_constant_parse(ctx, sym, realm_tbl, res);
 }
 
 const struct datatype realm_type = {
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

PATCH 2/2 src: remove global symbol_table

Store symbol tables in context object instead.

---
 include/ct.h       |  3 ++-
 include/datatype.h |  4 +---
 include/meta.h     |  2 --
 include/nftables.h | 18 ++++++++----------
 src/ct.c           | 17 ++++++++---------
 src/datatype.c     | 16 +++++++---------
 src/json.c         |  6 +++---
 src/libnftables.c  | 29 ++++++++++++++---------------
 src/meta.c         | 27 +++++++--------------------
 src/rt.c           | 13 ++++++-------
 10 files changed, 56 insertions(+), 79 deletions(-)
include/ct.h +2 -1
index 063f8cdf4aa4..efb2d4185543 100644
@@ -33,7 +33,8 @@ extern struct stmt *notrack_stmt_alloc(const struct location *loc);
 extern struct stmt *flow_offload_stmt_alloc(const struct location *loc,
 					    const char *table_name);
 extern const char *ct_dir2str(int dir);
-extern const char *ct_label2str(unsigned long value);
+extern const char *ct_label2str(const struct symbol_table *tbl,
+				unsigned long value);
 
 extern const struct datatype ct_dir_type;
 extern const struct datatype ct_state_type;
include/datatype.h +1 -3
index 018f013aea04..cf1151582245 100644
@@ -238,9 +238,7 @@ extern void symbol_table_print(const struct symbol_table *tbl,
 			       struct output_ctx *octx);
 
 extern struct symbol_table *rt_symbol_table_init(const char *filename);
-extern void rt_symbol_table_free(struct symbol_table *tbl);
-
-extern struct symbol_table *mark_tbl;
+extern void rt_symbol_table_free(const struct symbol_table *tbl);
 
 extern const struct datatype invalid_type;
 extern const struct datatype verdict_type;
include/meta.h +0 -2
index a49b4ff54970..0fe95fd66824 100644
@@ -42,6 +42,4 @@ extern const struct datatype devgroup_type;
 extern const struct datatype pkttype_type;
 extern const struct datatype ifname_type;
 
-extern struct symbol_table *devgroup_tbl;
-
 #endif /* NFTABLES_META_H */
include/nftables.h +8 -10
index 407d76130e9f..ef737c839b2e 100644
@@ -184,19 +184,17 @@ struct input_descriptor {
 	off_t				line_offset;
 };
 
-void ct_label_table_init(void);
-void mark_table_init(void);
+void ct_label_table_init(struct nft_ctx *ctx);
+void mark_table_init(struct nft_ctx *ctx);
 void gmp_init(void);
-void realm_table_rt_init(void);
-void devgroup_table_init(void);
-void realm_table_meta_init(void);
+void realm_table_rt_init(struct nft_ctx *ctx);
+void devgroup_table_init(struct nft_ctx *ctx);
 void xt_init(void);
 
-void ct_label_table_exit(void);
-void mark_table_exit(void);
-void realm_table_meta_exit(void);
-void devgroup_table_exit(void);
-void realm_table_rt_exit(void);
+void ct_label_table_exit(struct nft_ctx *ctx);
+void mark_table_exit(struct nft_ctx *ctx);
+void devgroup_table_exit(struct nft_ctx *ctx);
+void realm_table_rt_exit(struct nft_ctx *ctx);
 
 int nft_print(struct output_ctx *octx, const char *fmt, ...)
 	__attribute__((format(printf, 2, 3)));
src/ct.c +8 -9
index c66b327a2237..ed458e6b679b 100644
@@ -141,11 +141,10 @@ static const struct datatype ct_event_type = {
 	.sym_tbl	= &ct_events_tbl,
 };
 
-static struct symbol_table *ct_label_tbl;
-
 #define CT_LABEL_BIT_SIZE 128
 
-const char *ct_label2str(unsigned long value)
+const char *ct_label2str(const struct symbol_table *ct_label_tbl,
+			 unsigned long value)
 {
 	const struct symbolic_constant *s;
 
@@ -161,7 +160,7 @@ static void ct_label_type_print(const struct expr *expr,
 				 struct output_ctx *octx)
 {
 	unsigned long bit = mpz_scan1(expr->value, 0);
-	const char *labelstr = ct_label2str(bit);
+	const char *labelstr = ct_label2str(octx->tbl.ct_label, bit);
 
 	if (labelstr) {
 		nft_print(octx, "\"%s\"", labelstr);
@@ -181,7 +180,7 @@ static struct error_record *ct_label_type_parse(struct parse_ctx *ctx,
 	uint64_t bit;
 	mpz_t value;
 
-	for (s = ct_label_tbl->symbols; s->identifier != NULL; s++) {
+	for (s = ctx->tbl->ct_label->symbols; s->identifier != NULL; s++) {
 		if (!strcmp(sym->identifier, s->identifier))
 			break;
 	}
@@ -230,14 +229,14 @@ static const struct datatype ct_label_type = {
 	.parse		= ct_label_type_parse,
 };
 
-void ct_label_table_init(void)
+void ct_label_table_init(struct nft_ctx *ctx)
 {
-	ct_label_tbl = rt_symbol_table_init(CONNLABEL_CONF);
+	ctx->output.tbl.ct_label = rt_symbol_table_init(CONNLABEL_CONF);
 }
 
-void ct_label_table_exit(void)
+void ct_label_table_exit(struct nft_ctx *ctx)
 {
-	rt_symbol_table_free(ct_label_tbl);
+	rt_symbol_table_free(ctx->output.tbl.ct_label);
 }
 
 #ifndef NF_CT_HELPER_NAME_LEN
src/datatype.c +7 -9
index 039b4e529af0..396a300cba4b 100644
@@ -779,7 +779,7 @@ out:
 	return tbl;
 }
 
-void rt_symbol_table_free(struct symbol_table *tbl)
+void rt_symbol_table_free(const struct symbol_table *tbl)
 {
 	const struct symbolic_constant *s;
 
@@ -788,28 +788,26 @@ void rt_symbol_table_free(struct symbol_table *tbl)
 	xfree(tbl);
 }
 
-struct symbol_table *mark_tbl = NULL;
-
-void mark_table_init(void)
+void mark_table_init(struct nft_ctx *ctx)
 {
-	mark_tbl = rt_symbol_table_init("/etc/iproute2/rt_marks");
+	ctx->output.tbl.mark = rt_symbol_table_init("/etc/iproute2/rt_marks");
 }
 
-void mark_table_exit(void)
+void mark_table_exit(struct nft_ctx *ctx)
 {
-	rt_symbol_table_free(mark_tbl);
+	rt_symbol_table_free(ctx->output.tbl.mark);
 }
 
 static void mark_type_print(const struct expr *expr, struct output_ctx *octx)
 {
-	return symbolic_constant_print(mark_tbl, expr, true, octx);
+	return symbolic_constant_print(octx->tbl.mark, expr, true, octx);
 }
 
 static struct error_record *mark_type_parse(struct parse_ctx *ctx,
 					    const struct expr *sym,
 					    struct expr **res)
 {
-	return symbolic_constant_parse(ctx, sym, mark_tbl, res);
+	return symbolic_constant_parse(ctx, sym, ctx->tbl->mark, res);
 }
 
 const struct datatype mark_type = {
src/json.c +3 -3
index 33e0ec15f2ee..9dfa3076429d 100644
@@ -1006,18 +1006,18 @@ json_t *inet_service_type_json(const struct expr *expr, struct output_ctx *octx)
 
 json_t *mark_type_json(const struct expr *expr, struct output_ctx *octx)
 {
-	return symbolic_constant_json(mark_tbl, expr, octx);
+	return symbolic_constant_json(octx->tbl.mark, expr, octx);
 }
 
 json_t *devgroup_type_json(const struct expr *expr, struct output_ctx *octx)
 {
-	return symbolic_constant_json(devgroup_tbl, expr, octx);
+	return symbolic_constant_json(octx->tbl.devgroup, expr, octx);
 }
 
 json_t *ct_label_type_json(const struct expr *expr, struct output_ctx *octx)
 {
 	unsigned long bit = mpz_scan1(expr->value, 0);
-	const char *labelstr = ct_label2str(bit);
+	const char *labelstr = ct_label2str(octx->tbl.ct_label, bit);
 
 	if (labelstr)
 		return json_string(labelstr);
src/libnftables.c +14 -15
index 4a139c58b2b3..a693c0c69075 100644
@@ -84,26 +84,25 @@ out:
 	return ret;
 }
 
-static void nft_init(void)
+static void nft_init(struct nft_ctx *ctx)
 {
-	mark_table_init();
-	realm_table_rt_init();
-	devgroup_table_init();
-	realm_table_meta_init();
-	ct_label_table_init();
+	mark_table_init(ctx);
+	realm_table_rt_init(ctx);
+	devgroup_table_init(ctx);
+	ct_label_table_init(ctx);
+
 	gmp_init();
 #ifdef HAVE_LIBXTABLES
 	xt_init();
 #endif
 }
 
-static void nft_exit(void)
+static void nft_exit(struct nft_ctx *ctx)
 {
-	ct_label_table_exit();
-	realm_table_rt_exit();
-	devgroup_table_exit();
-	realm_table_meta_exit();
-	mark_table_exit();
+	ct_label_table_exit(ctx);
+	realm_table_rt_exit(ctx);
+	devgroup_table_exit(ctx);
+	mark_table_exit(ctx);
 }
 
 EXPORT_SYMBOL(nft_ctx_add_include_path);
@@ -145,10 +144,10 @@ struct nft_ctx *nft_ctx_new(uint32_t flags)
 {
 	struct nft_ctx *ctx;
 
-	nft_init();
 	ctx = xzalloc(sizeof(struct nft_ctx));
-	ctx->state = xzalloc(sizeof(struct parser_state));
+	nft_init(ctx);
 
+	ctx->state = xzalloc(sizeof(struct parser_state));
 	nft_ctx_add_include_path(ctx, DEFAULT_INCLUDE_PATH);
 	ctx->parser_max_errors	= 10;
 	init_list_head(&ctx->cache.list);
@@ -291,7 +290,7 @@ void nft_ctx_free(struct nft_ctx *ctx)
 	nft_ctx_clear_include_paths(ctx);
 	xfree(ctx->state);
 	xfree(ctx);
-	nft_exit();
+	nft_exit(ctx);
 }
 
 EXPORT_SYMBOL(nft_ctx_set_output);
src/meta.c +7 -20
index 5c0c4e29c062..5901c9919ed8 100644
@@ -37,17 +37,6 @@
 #include <iface.h>
 #include <json.h>
 
-static struct symbol_table *realm_tbl;
-void realm_table_meta_init(void)
-{
-	realm_tbl = rt_symbol_table_init("/etc/iproute2/rt_realms");
-}
-
-void realm_table_meta_exit(void)
-{
-	rt_symbol_table_free(realm_tbl);
-}
-
 static void tchandle_type_print(const struct expr *expr,
 				struct output_ctx *octx)
 {
@@ -341,29 +330,27 @@ const struct datatype pkttype_type = {
 	.sym_tbl	= &pkttype_type_tbl,
 };
 
-struct symbol_table *devgroup_tbl = NULL;
-
-void devgroup_table_init(void)
+void devgroup_table_init(struct nft_ctx *ctx)
 {
-	devgroup_tbl = rt_symbol_table_init("/etc/iproute2/group");
+	ctx->output.tbl.devgroup = rt_symbol_table_init("/etc/iproute2/group");
 }
 
-void devgroup_table_exit(void)
+void devgroup_table_exit(struct nft_ctx *ctx)
 {
-	rt_symbol_table_free(devgroup_tbl);
+	rt_symbol_table_free(ctx->output.tbl.devgroup);
 }
 
 static void devgroup_type_print(const struct expr *expr,
-				 struct output_ctx *octx)
+				struct output_ctx *octx)
 {
-	return symbolic_constant_print(devgroup_tbl, expr, true, octx);
+	return symbolic_constant_print(octx->tbl.devgroup, expr, true, octx);
 }
 
 static struct error_record *devgroup_type_parse(struct parse_ctx *ctx,
 						const struct expr *sym,
 						struct expr **res)
 {
-	return symbolic_constant_parse(ctx, sym, devgroup_tbl, res);
+	return symbolic_constant_parse(ctx, sym, ctx->tbl->devgroup, res);
 }
 
 const struct datatype devgroup_type = {
src/rt.c +6 -7
index cdd5e9d82b44..b19c44d6eefe 100644
@@ -24,27 +24,26 @@
 #include <rule.h>
 #include <json.h>
 
-static struct symbol_table *realm_tbl;
-void realm_table_rt_init(void)
+void realm_table_rt_init(struct nft_ctx *ctx)
 {
-	realm_tbl = rt_symbol_table_init("/etc/iproute2/rt_realms");
+	ctx->output.tbl.realm = rt_symbol_table_init("/etc/iproute2/rt_realms");
 }
 
-void realm_table_rt_exit(void)
+void realm_table_rt_exit(struct nft_ctx *ctx)
 {
-	rt_symbol_table_free(realm_tbl);
+	rt_symbol_table_free(ctx->output.tbl.realm);
 }
 
 static void realm_type_print(const struct expr *expr, struct output_ctx *octx)
 {
-	return symbolic_constant_print(realm_tbl, expr, true, octx);
+	return symbolic_constant_print(octx->tbl.realm, expr, true, octx);
 }
 
 static struct error_record *realm_type_parse(struct parse_ctx *ctx,
 					     const struct expr *sym,
 					     struct expr **res)
 {
-	return symbolic_constant_parse(ctx, sym, realm_tbl, res);
+	return symbolic_constant_parse(ctx, sym, ctx->tbl->realm, res);
 }
 
 const struct datatype realm_type = {
Signature · 1 line
2.11.0
maintainer
#2 lore raw

Re: [PATCH nft 2/2] src: remove global symbol_table

Quoted 1 line · Pablo Neira Ayuso

Store symbol tables in context object instead.

Looks good to me and works fine for my purposes (no crash anymore),