Blame SOURCES/0005-monitor-Use-libnftables-JSON-output.patch

34625f
From 53693c43d94dddf1ae1a0e69bfa953fba2c098e0 Mon Sep 17 00:00:00 2001
34625f
From: Phil Sutter <phil@nwl.cc>
34625f
Date: Thu, 11 Oct 2018 17:49:00 +0200
34625f
Subject: [PATCH] monitor: Use libnftables JSON output
34625f
34625f
This switches 'nft monitor' JSON output from using libnftnl's to
34625f
libnftables' implementation.
34625f
34625f
Signed-off-by: Phil Sutter <phil@nwl.cc>
34625f
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
34625f
(cherry picked from commit 9e88aae28e9f44d010f3ecf7577357f4c0e7d622)
34625f
Signed-off-by: Phil Sutter <psutter@redhat.com>
34625f
---
34625f
 include/json.h |  51 +++++++++
34625f
 src/json.c     |  57 ++++++++++
34625f
 src/monitor.c  | 281 +++++++++++++++++++++++++------------------------
34625f
 src/rule.c     |   2 -
34625f
 4 files changed, 251 insertions(+), 140 deletions(-)
34625f
34625f
diff --git a/include/json.h b/include/json.h
34625f
index ae3938142aeac..af0f72f13dd60 100644
34625f
--- a/include/json.h
34625f
+++ b/include/json.h
34625f
@@ -9,9 +9,11 @@ struct expr;
34625f
 struct netlink_ctx;
34625f
 struct rule;
34625f
 struct set;
34625f
+struct obj;
34625f
 struct stmt;
34625f
 struct symbol_table;
34625f
 struct table;
34625f
+struct netlink_mon_handler;
34625f
 
34625f
 #ifdef HAVE_LIBJANSSON
34625f
 
34625f
@@ -81,6 +83,19 @@ int nft_parse_json_buffer(struct nft_ctx *nft, char *buf, size_t buflen,
34625f
 int nft_parse_json_filename(struct nft_ctx *nft, const char *filename,
34625f
 			    struct list_head *msgs, struct list_head *cmds);
34625f
 
34625f
+void monitor_print_table_json(struct netlink_mon_handler *monh,
34625f
+			      const char *cmd, struct table *t);
34625f
+void monitor_print_chain_json(struct netlink_mon_handler *monh,
34625f
+			      const char *cmd, struct chain *c);
34625f
+void monitor_print_set_json(struct netlink_mon_handler *monh,
34625f
+			    const char *cmd, struct set *s);
34625f
+void monitor_print_element_json(struct netlink_mon_handler *monh,
34625f
+				const char *cmd, struct set *s);
34625f
+void monitor_print_obj_json(struct netlink_mon_handler *monh,
34625f
+			    const char *cmd, struct obj *o);
34625f
+void monitor_print_rule_json(struct netlink_mon_handler *monh,
34625f
+			     const char *cmd, struct rule *r);
34625f
+
34625f
 #else /* ! HAVE_LIBJANSSON */
34625f
 
34625f
 typedef void json_t;
34625f
@@ -176,6 +191,42 @@ nft_parse_json_filename(struct nft_ctx *nft, const char *filename,
34625f
 	return -EINVAL;
34625f
 }
34625f
 
34625f
+static inline void monitor_print_table_json(struct netlink_mon_handler *monh,
34625f
+					    const char *cmd, struct table *t)
34625f
+{
34625f
+	/* empty */
34625f
+}
34625f
+
34625f
+static inline void monitor_print_chain_json(struct netlink_mon_handler *monh,
34625f
+					    const char *cmd, struct chain *c)
34625f
+{
34625f
+	/* empty */
34625f
+}
34625f
+
34625f
+static inline void monitor_print_set_json(struct netlink_mon_handler *monh,
34625f
+					  const char *cmd, struct set *s)
34625f
+{
34625f
+	/* empty */
34625f
+}
34625f
+
34625f
+static inline void monitor_print_element_json(struct netlink_mon_handler *monh,
34625f
+					      const char *cmd, struct set *s)
34625f
+{
34625f
+	/* empty */
34625f
+}
34625f
+
34625f
+static inline void monitor_print_obj_json(struct netlink_mon_handler *monh,
34625f
+					  const char *cmd, struct obj *o)
34625f
+{
34625f
+	/* empty */
34625f
+}
34625f
+
34625f
+static inline void monitor_print_rule_json(struct netlink_mon_handler *monh,
34625f
+					   const char *cmd, struct rule *r)
34625f
+{
34625f
+	/* empty */
34625f
+}
34625f
+
34625f
 #endif /* HAVE_LIBJANSSON */
34625f
 
34625f
 #endif /* NFTABLES_JSON_H */
34625f
diff --git a/src/json.c b/src/json.c
34625f
index af157212c081e..7d89754bd666d 100644
34625f
--- a/src/json.c
34625f
+++ b/src/json.c
34625f
@@ -147,6 +147,19 @@ static json_t *set_print_json(struct output_ctx *octx, const struct set *set)
34625f
 	return json_pack("{s:o}", type, root);
34625f
 }
34625f
 
34625f
+/* XXX: Merge with set_print_json()? */
34625f
+static json_t *element_print_json(struct output_ctx *octx,
34625f
+				  const struct set *set)
34625f
+{
34625f
+	json_t *root = expr_print_json(set->init, octx);
34625f
+
34625f
+	return json_pack("{s: {s:s, s:s, s:s, s:o}}", "element",
34625f
+			 "family", family2str(set->handle.family),
34625f
+			 "table", set->handle.table.name,
34625f
+			 "name", set->handle.set.name,
34625f
+			 "elem", root);
34625f
+}
34625f
+
34625f
 static json_t *stmt_print_json(const struct stmt *stmt, struct output_ctx *octx)
34625f
 {
34625f
 	char buf[1024];
34625f
@@ -1554,3 +1567,47 @@ int do_command_list_json(struct netlink_ctx *ctx, struct cmd *cmd)
34625f
 	json_decref(root);
34625f
 	return 0;
34625f
 }
34625f
+
34625f
+static void monitor_print_json(struct netlink_mon_handler *monh,
34625f
+			       const char *cmd, json_t *obj)
34625f
+{
34625f
+	obj = json_pack("{s:o}", cmd, obj);
34625f
+	json_dumpf(obj, monh->ctx->octx->output_fp, 0);
34625f
+	json_decref(obj);
34625f
+}
34625f
+
34625f
+void monitor_print_table_json(struct netlink_mon_handler *monh,
34625f
+			      const char *cmd, struct table *t)
34625f
+{
34625f
+	monitor_print_json(monh, cmd, table_print_json(monh->ctx->octx, t));
34625f
+}
34625f
+
34625f
+void monitor_print_chain_json(struct netlink_mon_handler *monh,
34625f
+			      const char *cmd, struct chain *c)
34625f
+{
34625f
+	monitor_print_json(monh, cmd, chain_print_json(monh->ctx->octx, c));
34625f
+}
34625f
+
34625f
+void monitor_print_set_json(struct netlink_mon_handler *monh,
34625f
+			    const char *cmd, struct set *s)
34625f
+{
34625f
+	monitor_print_json(monh, cmd, set_print_json(monh->ctx->octx, s));
34625f
+}
34625f
+
34625f
+void monitor_print_element_json(struct netlink_mon_handler *monh,
34625f
+				const char *cmd, struct set *s)
34625f
+{
34625f
+	monitor_print_json(monh, cmd, element_print_json(monh->ctx->octx, s));
34625f
+}
34625f
+
34625f
+void monitor_print_obj_json(struct netlink_mon_handler *monh,
34625f
+			    const char *cmd, struct obj *o)
34625f
+{
34625f
+	monitor_print_json(monh, cmd, obj_print_json(monh->ctx->octx, o));
34625f
+}
34625f
+
34625f
+void monitor_print_rule_json(struct netlink_mon_handler *monh,
34625f
+			     const char *cmd, struct rule *r)
34625f
+{
34625f
+	monitor_print_json(monh, cmd, rule_print_json(monh->ctx->octx, r));
34625f
+}
34625f
diff --git a/src/monitor.c b/src/monitor.c
34625f
index 3e70b89f0b2ab..213c40d119b4c 100644
34625f
--- a/src/monitor.c
34625f
+++ b/src/monitor.c
34625f
@@ -38,6 +38,7 @@
34625f
 #include <utils.h>
34625f
 #include <erec.h>
34625f
 #include <iface.h>
34625f
+#include <json.h>
34625f
 
34625f
 #define nft_mon_print(monh, ...) nft_print(monh->ctx->octx, __VA_ARGS__)
34625f
 
34625f
@@ -127,18 +128,39 @@ static uint32_t netlink_msg2nftnl_of(uint32_t msg)
34625f
 	case NFT_MSG_NEWSET:
34625f
 	case NFT_MSG_NEWSETELEM:
34625f
 	case NFT_MSG_NEWRULE:
34625f
+	case NFT_MSG_NEWOBJ:
34625f
+	case NFT_MSG_NEWFLOWTABLE:
34625f
 		return NFTNL_OF_EVENT_NEW;
34625f
 	case NFT_MSG_DELTABLE:
34625f
 	case NFT_MSG_DELCHAIN:
34625f
 	case NFT_MSG_DELSET:
34625f
 	case NFT_MSG_DELSETELEM:
34625f
 	case NFT_MSG_DELRULE:
34625f
+	case NFT_MSG_DELOBJ:
34625f
+	case NFT_MSG_DELFLOWTABLE:
34625f
 		return NFTNL_OF_EVENT_DEL;
34625f
 	}
34625f
 
34625f
 	return 0;
34625f
 }
34625f
 
34625f
+static const char *nftnl_of2cmd(uint32_t of)
34625f
+{
34625f
+	switch (of) {
34625f
+	case NFTNL_OF_EVENT_NEW:
34625f
+		return "add";
34625f
+	case NFTNL_OF_EVENT_DEL:
34625f
+		return "delete";
34625f
+	default:
34625f
+		return "???";
34625f
+	}
34625f
+}
34625f
+
34625f
+static const char *netlink_msg2cmd(uint32_t msg)
34625f
+{
34625f
+	return nftnl_of2cmd(netlink_msg2nftnl_of(msg));
34625f
+}
34625f
+
34625f
 static void nlr_for_each_set(struct nftnl_rule *nlr,
34625f
 			     void (*cb)(struct set *s, void *data),
34625f
 			     void *data, struct nft_cache *cache)
34625f
@@ -179,34 +201,29 @@ static int netlink_events_table_cb(const struct nlmsghdr *nlh, int type,
34625f
 				   struct netlink_mon_handler *monh)
34625f
 {
34625f
 	struct nftnl_table *nlt;
34625f
-	uint32_t family;
34625f
+	struct table *t;
34625f
+	const char *cmd;
34625f
 
34625f
 	nlt = netlink_table_alloc(nlh);
34625f
+	t = netlink_delinearize_table(monh->ctx, nlt);
34625f
+	cmd = netlink_msg2cmd(type);
34625f
 
34625f
 	switch (monh->format) {
34625f
 	case NFTNL_OUTPUT_DEFAULT:
34625f
-		if (type == NFT_MSG_NEWTABLE) {
34625f
-			nft_mon_print(monh, "add table ");
34625f
-		} else {
34625f
-			nft_mon_print(monh, "delete table ");
34625f
-		}
34625f
+		nft_mon_print(monh, "%s table ", cmd);
34625f
 
34625f
-		family = nftnl_table_get_u32(nlt, NFTNL_TABLE_FAMILY);
34625f
-
34625f
-		nft_mon_print(monh, "%s %s", family2str(family),
34625f
-		       nftnl_table_get_str(nlt, NFTNL_TABLE_NAME));
34625f
+		nft_mon_print(monh, "%s %s", family2str(t->handle.family),
34625f
+			      t->handle.table.name);
34625f
 		if (monh->ctx->octx->handle > 0)
34625f
 			nft_mon_print(monh, " # handle %" PRIu64 "",
34625f
-				      nftnl_table_get_u64(nlt, NFTNL_TABLE_HANDLE));
34625f
-		nft_mon_print(monh, "\n");
34625f
+				      t->handle.handle.id);
34625f
 		break;
34625f
 	case NFTNL_OUTPUT_JSON:
34625f
-		nftnl_table_fprintf(monh->ctx->octx->output_fp, nlt,
34625f
-				    monh->format, netlink_msg2nftnl_of(type));
34625f
-		nft_mon_print(monh, "\n");
34625f
+		monitor_print_table_json(monh, cmd, t);
34625f
 		break;
34625f
 	}
34625f
-
34625f
+	nft_mon_print(monh, "\n");
34625f
+	table_free(t);
34625f
 	nftnl_table_free(nlt);
34625f
 	return MNL_CB_OK;
34625f
 }
34625f
@@ -216,35 +233,34 @@ static int netlink_events_chain_cb(const struct nlmsghdr *nlh, int type,
34625f
 {
34625f
 	struct nftnl_chain *nlc;
34625f
 	struct chain *c;
34625f
-	uint32_t family;
34625f
+	const char *cmd;
34625f
 
34625f
 	nlc = netlink_chain_alloc(nlh);
34625f
+	c = netlink_delinearize_chain(monh->ctx, nlc);
34625f
+	cmd = netlink_msg2cmd(type);
34625f
 
34625f
 	switch (monh->format) {
34625f
 	case NFTNL_OUTPUT_DEFAULT:
34625f
+		nft_mon_print(monh, "%s ", cmd);
34625f
+
34625f
 		switch (type) {
34625f
 		case NFT_MSG_NEWCHAIN:
34625f
-			nft_mon_print(monh, "add ");
34625f
-
34625f
-			c = netlink_delinearize_chain(monh->ctx, nlc);
34625f
 			chain_print_plain(c, monh->ctx->octx);
34625f
-			chain_free(c);
34625f
 			break;
34625f
 		case NFT_MSG_DELCHAIN:
34625f
-			family = nftnl_chain_get_u32(nlc, NFTNL_CHAIN_FAMILY);
34625f
-			nft_mon_print(monh, "delete chain %s %s %s\n", family2str(family),
34625f
-			       nftnl_chain_get_str(nlc, NFTNL_CHAIN_TABLE),
34625f
-			       nftnl_chain_get_str(nlc, NFTNL_CHAIN_NAME));
34625f
+			nft_mon_print(monh, "chain %s %s %s",
34625f
+				      family2str(c->handle.family),
34625f
+				      c->handle.table.name,
34625f
+				      c->handle.chain.name);
34625f
 			break;
34625f
 		}
34625f
 		break;
34625f
 	case NFTNL_OUTPUT_JSON:
34625f
-		nftnl_chain_fprintf(monh->ctx->octx->output_fp, nlc,
34625f
-				    monh->format, netlink_msg2nftnl_of(type));
34625f
-		nft_mon_print(monh, "\n");
34625f
+		monitor_print_chain_json(monh, cmd, c);
34625f
 		break;
34625f
 	}
34625f
-
34625f
+	nft_mon_print(monh, "\n");
34625f
+	chain_free(c);
34625f
 	nftnl_chain_free(nlc);
34625f
 	return MNL_CB_OK;
34625f
 }
34625f
@@ -253,43 +269,44 @@ static int netlink_events_set_cb(const struct nlmsghdr *nlh, int type,
34625f
 				 struct netlink_mon_handler *monh)
34625f
 {
34625f
 	struct nftnl_set *nls;
34625f
+	const char *family, *cmd;
34625f
 	struct set *set;
34625f
-	uint32_t family, flags;
34625f
+	uint32_t flags;
34625f
 
34625f
 	nls = netlink_set_alloc(nlh);
34625f
 	flags = nftnl_set_get_u32(nls, NFTNL_SET_FLAGS);
34625f
 	if (flags & NFT_SET_ANONYMOUS)
34625f
 		goto out;
34625f
 
34625f
+	set = netlink_delinearize_set(monh->ctx, nls);
34625f
+	if (set == NULL) {
34625f
+		nftnl_set_free(nls);
34625f
+		return MNL_CB_ERROR;
34625f
+	}
34625f
+	family = family2str(set->handle.family);
34625f
+	cmd = netlink_msg2cmd(type);
34625f
+
34625f
 	switch (monh->format) {
34625f
 	case NFTNL_OUTPUT_DEFAULT:
34625f
+		nft_mon_print(monh, "%s ", cmd);
34625f
+
34625f
 		switch (type) {
34625f
 		case NFT_MSG_NEWSET:
34625f
-			nft_mon_print(monh, "add ");
34625f
-			set = netlink_delinearize_set(monh->ctx, nls);
34625f
-			if (set == NULL) {
34625f
-				nftnl_set_free(nls);
34625f
-				return MNL_CB_ERROR;
34625f
-			}
34625f
 			set_print_plain(set, monh->ctx->octx);
34625f
-			set_free(set);
34625f
-			nft_mon_print(monh, "\n");
34625f
 			break;
34625f
 		case NFT_MSG_DELSET:
34625f
-			family = nftnl_set_get_u32(nls, NFTNL_SET_FAMILY);
34625f
-			nft_mon_print(monh, "delete set %s %s %s\n",
34625f
-			       family2str(family),
34625f
-			       nftnl_set_get_str(nls, NFTNL_SET_TABLE),
34625f
-			       nftnl_set_get_str(nls, NFTNL_SET_NAME));
34625f
+			nft_mon_print(monh, "set %s %s %s", family,
34625f
+				      set->handle.table.name,
34625f
+				      set->handle.set.name);
34625f
 			break;
34625f
 		}
34625f
 		break;
34625f
 	case NFTNL_OUTPUT_JSON:
34625f
-		nftnl_set_fprintf(monh->ctx->octx->output_fp, nls,
34625f
-				  monh->format, netlink_msg2nftnl_of(type));
34625f
-		nft_mon_print(monh, "\n");
34625f
+		monitor_print_set_json(monh, cmd, set);
34625f
 		break;
34625f
 	}
34625f
+	nft_mon_print(monh, "\n");
34625f
+	set_free(set);
34625f
 out:
34625f
 	nftnl_set_free(nls);
34625f
 	return MNL_CB_OK;
34625f
@@ -360,13 +377,14 @@ static int netlink_events_setelem_cb(const struct nlmsghdr *nlh, int type,
34625f
 	struct nftnl_set *nls;
34625f
 	struct set *dummyset;
34625f
 	struct set *set;
34625f
-	const char *setname, *table;
34625f
+	const char *setname, *table, *cmd;
34625f
 	uint32_t family;
34625f
 
34625f
 	nls = netlink_setelem_alloc(nlh);
34625f
 	table = nftnl_set_get_str(nls, NFTNL_SET_TABLE);
34625f
 	setname = nftnl_set_get_str(nls, NFTNL_SET_NAME);
34625f
 	family = nftnl_set_get_u32(nls, NFTNL_SET_FAMILY);
34625f
+	cmd = netlink_msg2cmd(type);
34625f
 
34625f
 	set = set_lookup_global(family, table, setname, monh->cache);
34625f
 	if (set == NULL) {
34625f
@@ -374,70 +392,63 @@ static int netlink_events_setelem_cb(const struct nlmsghdr *nlh, int type,
34625f
 		goto out;
34625f
 	}
34625f
 
34625f
-	switch (monh->format) {
34625f
-	case NFTNL_OUTPUT_DEFAULT:
34625f
-		if (set->flags & NFT_SET_ANONYMOUS)
34625f
-			goto out;
34625f
-
34625f
-		/* we want to 'delinearize' the set_elem, but don't
34625f
-		 * modify the original cached set. This path is only
34625f
-		 * used by named sets, so use a dummy set.
34625f
-		 */
34625f
-		dummyset = set_alloc(monh->loc);
34625f
-		dummyset->key = expr_clone(set->key);
34625f
-		dummyset->datatype = set->datatype;
34625f
-		dummyset->flags = set->flags;
34625f
-		dummyset->init = set_expr_alloc(monh->loc, set);
34625f
+	if (set->flags & NFT_SET_ANONYMOUS)
34625f
+		goto out;
34625f
 
34625f
-		nlsei = nftnl_set_elems_iter_create(nls);
34625f
-		if (nlsei == NULL)
34625f
-			memory_allocation_error();
34625f
+	/* we want to 'delinearize' the set_elem, but don't
34625f
+	 * modify the original cached set. This path is only
34625f
+	 * used by named sets, so use a dummy set.
34625f
+	 */
34625f
+	dummyset = set_alloc(monh->loc);
34625f
+	dummyset->key = expr_clone(set->key);
34625f
+	dummyset->datatype = set->datatype;
34625f
+	dummyset->flags = set->flags;
34625f
+	dummyset->init = set_expr_alloc(monh->loc, set);
34625f
 
34625f
-		nlse = nftnl_set_elems_iter_next(nlsei);
34625f
-		while (nlse != NULL) {
34625f
-			if (netlink_event_ignore_range_event(nlse)) {
34625f
-				set_free(dummyset);
34625f
-				nftnl_set_elems_iter_destroy(nlsei);
34625f
-				goto out;
34625f
-			}
34625f
-			if (netlink_delinearize_setelem(nlse, dummyset,
34625f
-							monh->cache) < 0) {
34625f
-				set_free(dummyset);
34625f
-				nftnl_set_elems_iter_destroy(nlsei);
34625f
-				goto out;
34625f
-			}
34625f
-			nlse = nftnl_set_elems_iter_next(nlsei);
34625f
-		}
34625f
-		nftnl_set_elems_iter_destroy(nlsei);
34625f
+	nlsei = nftnl_set_elems_iter_create(nls);
34625f
+	if (nlsei == NULL)
34625f
+		memory_allocation_error();
34625f
 
34625f
-		if (netlink_event_range_cache(set, dummyset)) {
34625f
+	nlse = nftnl_set_elems_iter_next(nlsei);
34625f
+	while (nlse != NULL) {
34625f
+		if (netlink_event_ignore_range_event(nlse)) {
34625f
 			set_free(dummyset);
34625f
+			nftnl_set_elems_iter_destroy(nlsei);
34625f
 			goto out;
34625f
 		}
34625f
-
34625f
-		switch (type) {
34625f
-		case NFT_MSG_NEWSETELEM:
34625f
-			nft_mon_print(monh, "add ");
34625f
-			break;
34625f
-		case NFT_MSG_DELSETELEM:
34625f
-			nft_mon_print(monh, "delete ");
34625f
-			break;
34625f
-		default:
34625f
+		if (netlink_delinearize_setelem(nlse, dummyset,
34625f
+						monh->cache) < 0) {
34625f
 			set_free(dummyset);
34625f
+			nftnl_set_elems_iter_destroy(nlsei);
34625f
 			goto out;
34625f
 		}
34625f
-		nft_mon_print(monh, "element %s %s %s ", family2str(family), table, setname);
34625f
-		expr_print(dummyset->init, monh->ctx->octx);
34625f
-		nft_mon_print(monh, "\n");
34625f
+		nlse = nftnl_set_elems_iter_next(nlsei);
34625f
+	}
34625f
+	nftnl_set_elems_iter_destroy(nlsei);
34625f
 
34625f
+	if (netlink_event_range_cache(set, dummyset)) {
34625f
 		set_free(dummyset);
34625f
+		goto out;
34625f
+	}
34625f
+
34625f
+	switch (monh->format) {
34625f
+	case NFTNL_OUTPUT_DEFAULT:
34625f
+		nft_mon_print(monh, "%s element %s %s %s ",
34625f
+			      cmd, family2str(family), table, setname);
34625f
+		expr_print(dummyset->init, monh->ctx->octx);
34625f
 		break;
34625f
 	case NFTNL_OUTPUT_JSON:
34625f
-		nftnl_set_fprintf(monh->ctx->octx->output_fp, nls,
34625f
-				  monh->format, netlink_msg2nftnl_of(type));
34625f
-		nft_mon_print(monh, "\n");
34625f
+		dummyset->handle.family = family;
34625f
+		dummyset->handle.set.name = setname;
34625f
+		dummyset->handle.table.name = table;
34625f
+		monitor_print_element_json(monh, cmd, dummyset);
34625f
+		/* prevent set_free() from trying to free those */
34625f
+		dummyset->handle.set.name = NULL;
34625f
+		dummyset->handle.table.name = NULL;
34625f
 		break;
34625f
 	}
34625f
+	nft_mon_print(monh, "\n");
34625f
+	set_free(dummyset);
34625f
 out:
34625f
 	nftnl_set_free(nls);
34625f
 	return MNL_CB_OK;
34625f
@@ -446,43 +457,43 @@ out:
34625f
 static int netlink_events_obj_cb(const struct nlmsghdr *nlh, int type,
34625f
 				 struct netlink_mon_handler *monh)
34625f
 {
34625f
+	const char *family, *cmd;
34625f
 	struct nftnl_obj *nlo;
34625f
-	uint32_t family;
34625f
 	struct obj *obj;
34625f
 
34625f
 	nlo = netlink_obj_alloc(nlh);
34625f
 
34625f
+	obj = netlink_delinearize_obj(monh->ctx, nlo);
34625f
+	if (obj == NULL) {
34625f
+		nftnl_obj_free(nlo);
34625f
+		return MNL_CB_ERROR;
34625f
+	}
34625f
+	family = family2str(obj->handle.family);
34625f
+	cmd = netlink_msg2cmd(type);
34625f
+
34625f
 	switch (monh->format) {
34625f
 	case NFTNL_OUTPUT_DEFAULT:
34625f
+		nft_mon_print(monh, "%s ", cmd);
34625f
+
34625f
 		switch (type) {
34625f
 		case NFT_MSG_NEWOBJ:
34625f
-			nft_mon_print(monh, "add ");
34625f
-			obj = netlink_delinearize_obj(monh->ctx, nlo);
34625f
-			if (obj == NULL) {
34625f
-				nftnl_obj_free(nlo);
34625f
-				return MNL_CB_ERROR;
34625f
-			}
34625f
 			obj_print_plain(obj, monh->ctx->octx);
34625f
-			obj_free(obj);
34625f
-			nft_mon_print(monh, "\n");
34625f
 			break;
34625f
 		case NFT_MSG_DELOBJ:
34625f
-			family = nftnl_obj_get_u32(nlo, NFTNL_OBJ_FAMILY);
34625f
-			nft_mon_print(monh, "delete %s %s %s %s\n",
34625f
-			       obj_type_name(nftnl_obj_get_u32(nlo, NFTNL_OBJ_TYPE)),
34625f
-			       family2str(family),
34625f
-			       nftnl_obj_get_str(nlo, NFTNL_OBJ_TABLE),
34625f
-			       nftnl_obj_get_str(nlo, NFTNL_OBJ_NAME));
34625f
+			nft_mon_print(monh, "%s %s %s %s",
34625f
+			       obj_type_name(obj->type),
34625f
+			       family,
34625f
+			       obj->handle.table.name,
34625f
+			       obj->handle.obj.name);
34625f
 			break;
34625f
 		}
34625f
 		break;
34625f
 	case NFTNL_OUTPUT_JSON:
34625f
-		nftnl_obj_fprintf(monh->ctx->octx->output_fp, nlo,
34625f
-				  monh->format, netlink_msg2nftnl_of(type));
34625f
-		nft_mon_print(monh, "\n");
34625f
+		monitor_print_obj_json(monh, cmd, obj);
34625f
 		break;
34625f
 	}
34625f
-
34625f
+	nft_mon_print(monh, "\n");
34625f
+	obj_free(obj);
34625f
 	nftnl_obj_free(nlo);
34625f
 	return MNL_CB_OK;
34625f
 }
34625f
@@ -496,48 +507,42 @@ static void rule_map_decompose_cb(struct set *s, void *data)
34625f
 static int netlink_events_rule_cb(const struct nlmsghdr *nlh, int type,
34625f
 				  struct netlink_mon_handler *monh)
34625f
 {
34625f
+	const char *family, *cmd;
34625f
 	struct nftnl_rule *nlr;
34625f
-	const char *family;
34625f
-	const char *table;
34625f
-	const char *chain;
34625f
 	struct rule *r;
34625f
-	uint64_t handle;
34625f
-	uint32_t fam;
34625f
 
34625f
 	nlr = netlink_rule_alloc(nlh);
34625f
+	r = netlink_delinearize_rule(monh->ctx, nlr);
34625f
+	nlr_for_each_set(nlr, rule_map_decompose_cb, NULL, monh->cache);
34625f
+	cmd = netlink_msg2cmd(type);
34625f
+
34625f
 	switch (monh->format) {
34625f
 	case NFTNL_OUTPUT_DEFAULT:
34625f
-		fam = nftnl_rule_get_u32(nlr, NFTNL_RULE_FAMILY);
34625f
-		family = family2str(fam);
34625f
-		table = nftnl_rule_get_str(nlr, NFTNL_RULE_TABLE);
34625f
-		chain = nftnl_rule_get_str(nlr, NFTNL_RULE_CHAIN);
34625f
-		handle = nftnl_rule_get_u64(nlr, NFTNL_RULE_HANDLE);
34625f
+		family = family2str(r->handle.family);
34625f
+
34625f
+		nft_mon_print(monh, "%s rule %s %s %s ",
34625f
+			      cmd,
34625f
+			      family,
34625f
+			      r->handle.table.name,
34625f
+			      r->handle.chain.name);
34625f
 
34625f
 		switch (type) {
34625f
 		case NFT_MSG_NEWRULE:
34625f
-			r = netlink_delinearize_rule(monh->ctx, nlr);
34625f
-			nlr_for_each_set(nlr, rule_map_decompose_cb, NULL,
34625f
-					 monh->cache);
34625f
-
34625f
-			nft_mon_print(monh, "add rule %s %s %s ", family, table, chain);
34625f
 			rule_print(r, monh->ctx->octx);
34625f
-			nft_mon_print(monh, "\n");
34625f
 
34625f
-			rule_free(r);
34625f
 			break;
34625f
 		case NFT_MSG_DELRULE:
34625f
-			nft_mon_print(monh, "delete rule %s %s %s handle %u\n",
34625f
-			       family, table, chain, (unsigned int)handle);
34625f
+			nft_mon_print(monh, "handle %" PRIu64,
34625f
+				      r->handle.handle.id);
34625f
 			break;
34625f
 		}
34625f
 		break;
34625f
 	case NFTNL_OUTPUT_JSON:
34625f
-		nftnl_rule_fprintf(monh->ctx->octx->output_fp, nlr,
34625f
-				   monh->format, netlink_msg2nftnl_of(type));
34625f
-		nft_mon_print(monh, "\n");
34625f
+		monitor_print_rule_json(monh, cmd, r);
34625f
 		break;
34625f
 	}
34625f
-
34625f
+	nft_mon_print(monh, "\n");
34625f
+	rule_free(r);
34625f
 	nftnl_rule_free(nlr);
34625f
 	return MNL_CB_OK;
34625f
 }
34625f
diff --git a/src/rule.c b/src/rule.c
34625f
index eb06302d4f223..3065cc5474bbf 100644
34625f
--- a/src/rule.c
34625f
+++ b/src/rule.c
34625f
@@ -812,8 +812,6 @@ void chain_print_plain(const struct chain *chain, struct output_ctx *octx)
34625f
 	}
34625f
 	if (octx->handle > 0)
34625f
 		nft_print(octx, " # handle %" PRIu64, chain->handle.handle.id);
34625f
-
34625f
-	nft_print(octx, "\n");
34625f
 }
34625f
 
34625f
 struct table *table_alloc(void)
34625f
-- 
34625f
2.19.0
34625f