Skip to contentSkip to conversation

nftables: basic support for extended netlink errors

PATCH Superseded Florian Westphal 1 participant 1 message Feb 17, 2018 netfilter

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 nftables: basic support for extended netlink errors

Recent kernels gained ability to emit error string back to userspace to improve error reporting.

At this time, as nftables kernel side doesn't generate such error messages, so this will always show 'unknown netlink error'. This will hopefully improve with newer kernels.

src/nft add rule ip filter input set add ip protocol @protocols
Error: Could not process rule: Invalid argument (unknown netlink error)

the -EINVAL stems from attempt to modify set from packet path, but kernel picked a set backend that lacks an ->update() facilty. Fixing this would obviously result in the command to succeed. However, given similar future bugs, kernel might have told us something like 'expression failed inititialisation' or 'set lacks update callback', which is much more helpful for developers to pinpoint the place where netlink processing failed on nftables kernel side.

---
 include/mnl.h     |  2 ++
 include/netlink.h | 20 +++++++++++++++
 src/libnftables.c |  4 +--
 src/mnl.c         | 74 +++++++++++++++++++++++++++++++++++++++++++++++++------
 src/netlink.c     |  6 ++++-
 5 files changed, 96 insertions(+), 10 deletions(-)
include/mnl.h +2 -0
index 4662cd04df9b..b8cee1fa1f51 100644
@@ -15,6 +15,8 @@ struct mnl_err {
 	struct list_head	head;
 	int			err;
 	uint32_t		seqnum;
+	uint32_t		offset;
+	char			*kern_errmsg;
 };
 
 void mnl_err_list_free(struct mnl_err *err);
include/netlink.h +20 -0
index 66686e5a3313..b4c1a45099f6 100644
@@ -13,6 +13,22 @@
 
 #include <rule.h>
 
+#ifndef NETLINK_EXT_ACK
+#define NETLINK_EXT_ACK                 11
+
+enum nlmsgerr_attrs {
+	NLMSGERR_ATTR_UNUSED,
+	NLMSGERR_ATTR_MSG,
+	NLMSGERR_ATTR_OFFS,
+	NLMSGERR_ATTR_COOKIE,
+
+	__NLMSGERR_ATTR_MAX,
+	NLMSGERR_ATTR_MAX = __NLMSGERR_ATTR_MAX - 1
+};
+#define NLM_F_CAPPED	0x100	/* request was capped */
+#define NLM_F_ACK_TLVS	0x200	/* extended ACK TVLs were included */
+#endif
+
 struct netlink_parse_ctx {
 	struct list_head	*msgs;
 	struct table		*table;
@@ -223,6 +239,10 @@ extern int netlink_monitor(struct netlink_mon_handler *monhandler,
 			    struct mnl_socket *nf_sock);
 bool netlink_batch_supported(struct mnl_socket *nf_sock, uint32_t *seqnum);
 
+struct netlink_cb_data {
+	struct netlink_ctx	*nl_ctx;
+	struct list_head	*err_list;
+};
 int netlink_echo_callback(const struct nlmsghdr *nlh, void *data);
 
 struct ruleset_parse {
src/libnftables.c +2 -2
index c86d89477e77..aa0eb899cdf1 100644
@@ -61,8 +61,8 @@ static int nft_netlink(struct nft_ctx *nft,
 			if (err->seqnum == cmd->seqnum ||
 			    err->seqnum == batch_seqnum) {
 				netlink_io_error(&ctx, &cmd->location,
-						 "Could not process rule: %s",
-						 strerror(err->err));
+						 "Could not process rule: %s (%s)",
+						 strerror(err->err), err->kern_errmsg);
 				errno = err->err;
 				if (err->seqnum == cmd->seqnum) {
 					mnl_err_list_free(err);
src/mnl.c +67 -7

115 lines not on this page · raw message

src/netlink.c +5 -1
index 488ae6f3971f..ec0111c0215d 100644
@@ -54,6 +54,7 @@ const struct location netlink_location = {
 struct mnl_socket *netlink_open_sock(void)
 {
 	struct mnl_socket *nf_sock;
+	int one = 1;
 
 	nf_sock = mnl_socket_open(NETLINK_NETFILTER);
 	if (nf_sock == NULL)
@@ -61,6 +62,8 @@ struct mnl_socket *netlink_open_sock(void)
 
 	fcntl(mnl_socket_get_fd(nf_sock), F_SETFL, O_NONBLOCK);
 
+	mnl_socket_setsockopt(nf_sock, NETLINK_EXT_ACK, &one, sizeof(one));
+
 	return nf_sock;
 }
 
@@ -3001,7 +3004,8 @@ static int netlink_events_cb(const struct nlmsghdr *nlh, void *data)
 
 int netlink_echo_callback(const struct nlmsghdr *nlh, void *data)
 {
-	struct netlink_ctx *ctx = data;
+	struct netlink_cb_data *cb_data = data;
+	struct netlink_ctx *ctx = cb_data->nl_ctx;
 	struct netlink_mon_handler echo_monh = {
 		.format = NFTNL_OUTPUT_DEFAULT,
 		.ctx = ctx,
Signature · 1 line
2.13.6