Skip to contentSkip to conversation

src: introduce abi_breakage()

PATCH New Pablo Neira Ayuso 1 participant 3 messages May 2, 2019 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/3 src: introduce abi_breakage()

Changes in the netlink attributes layout is considered to be a kernel ABI breakage, so report this immediately and stop execution, instead of lazy error back to the client application, which cannot do anything with this.

---
 include/internal/internal.h |  7 +++++++
 src/conntrack/parse_mnl.c   | 49 ++++++++++++++++++++++-----------------------
 src/expect/parse_mnl.c      |  8 ++++----
 src/main.c                  |  7 +++++++
 4 files changed, 42 insertions(+), 29 deletions(-)
include/internal/internal.h +7 -0
index 45b4aa7cc111..bb44e124082e 100644
@@ -85,4 +85,11 @@ struct nf_ct_tcp_flags {
 #define NFCT_BITMASK_AND	0
 #define NFCT_BITMASK_OR		1
 
+#define __noreturn	__attribute__((__noreturn__))
+
+void __noreturn __abi_breakage(const char *file, int line, const char *reason);
+
+#define abi_breakage()	\
+	__abi_breakage(__FILE__, __LINE__, strerror(errno));
+
 #endif
src/conntrack/parse_mnl.c +24 -25

171 lines not on this page · raw message

src/expect/parse_mnl.c +4 -4
index 741b46ef690e..69feef5379b0 100644
@@ -26,21 +26,21 @@ static int nlmsg_parse_expection_attr_cb(const struct nlattr *attr, void *data)
 	case CTA_EXPECT_TUPLE:
 	case CTA_EXPECT_MASK:
 		if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
-			return MNL_CB_ERROR;
+			abi_breakage();
 		break;
 	case CTA_EXPECT_TIMEOUT:
 	case CTA_EXPECT_FLAGS:
 	case CTA_EXPECT_ID:
 		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
-			return MNL_CB_ERROR;
+			abi_breakage();
 		break;
 	case CTA_EXPECT_HELP_NAME:
 		if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
-			return MNL_CB_ERROR;
+			abi_breakage();
 		break;
 	case CTA_EXPECT_ZONE:
 		if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
-			return MNL_CB_ERROR;
+			abi_breakage();
 		break;
 	}
 	tb[type] = attr;
src/main.c +7 -0
index 2cbf79ee1e74..4011ad6fc7fc 100644
@@ -154,3 +154,10 @@ const struct nfnl_handle *nfct_nfnlh(struct nfct_handle *cth)
 /**
  * @}
  */
+
+void __noreturn __abi_breakage(const char *file, int line, const char *reason)
+{
+	fprintf(stderr, "ctnetlink kernel ABI is broken, contact your vendor.\n"
+			"%s:%d reason: %s\n", file, line, reason);
+	exit(EXIT_FAILURE);
+}
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

PATCH 2/3 expect: add missing handling for CTA_EXPECT_* attributes

Add missing code to handle CTA_EXPECT_CLASS, CTA_EXPECT_NAT and CTA_EXPECT_FN from libmnl parser.

---
 src/expect/parse_mnl.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 61 insertions(+), 3 deletions(-)
src/expect/parse_mnl.c +61 -3
index 69feef5379b0..e7bbc16cadac 100644
@@ -47,6 +47,53 @@ static int nlmsg_parse_expection_attr_cb(const struct nlattr *attr, void *data)
 	return MNL_CB_OK;
 }
 
+static int nfexp_nlmsg_parse_nat_attr_cb(const struct nlattr *attr, void *data)
+{
+	int type = mnl_attr_get_type(attr);
+	const struct nlattr **tb = data;
+
+	if (mnl_attr_type_valid(attr, CTA_MAX) < 0)
+		return MNL_CB_OK;
+
+	switch(type) {
+	case CTA_EXPECT_NAT_TUPLE:
+		if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
+			abi_breakage();
+		break;
+	case CTA_EXPECT_NAT_DIR:
+		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+			abi_breakage();
+		break;
+	}
+
+	tb[type] = attr;
+	return MNL_CB_OK;
+}
+
+static void nfexp_nlmsg_parse_nat(struct nfgenmsg *nfg,
+				  const struct nlattr *attr,
+				  struct nf_expect *exp)
+{
+	struct nlattr *tb[CTA_EXPECT_NAT_MAX + 1] = {};
+
+	if (mnl_attr_parse_nested(attr, nfexp_nlmsg_parse_nat_attr_cb, tb) < 0)
+		return;
+
+	exp->nat.orig.l3protonum = nfg->nfgen_family;
+	set_bit(ATTR_ORIG_L3PROTO, exp->nat.set);
+
+	if (tb[CTA_EXPECT_NAT_TUPLE]) {
+		nfct_parse_tuple(tb[CTA_EXPECT_NAT_TUPLE], &exp->nat.orig,
+				 __DIR_ORIG, exp->nat.set);
+		set_bit(ATTR_EXP_NAT_TUPLE, exp->set);
+	}
+	if (tb[CTA_EXPECT_NAT_DIR]) {
+		exp->nat_dir =
+			ntohl(mnl_attr_get_u32(tb[CTA_EXPECT_NAT_DIR]));
+		set_bit(ATTR_EXP_NAT_DIR, exp->set);
+	}
+}
+
 int nfexp_nlmsg_parse(const struct nlmsghdr *nlh, struct nf_expect *exp)
 {
 	struct nlattr *tb[CTA_EXPECT_MAX+1] = {};
@@ -83,22 +130,33 @@ int nfexp_nlmsg_parse(const struct nlmsghdr *nlh, struct nf_expect *exp)
 		exp->timeout = ntohl(mnl_attr_get_u32(tb[CTA_EXPECT_TIMEOUT]));
 		set_bit(ATTR_EXP_TIMEOUT, exp->set);
 	}
-
 	if (tb[CTA_EXPECT_ZONE]) {
 		exp->zone = ntohs(mnl_attr_get_u16(tb[CTA_EXPECT_ZONE]));
 		set_bit(ATTR_EXP_ZONE, exp->set);
 	}
-
 	if (tb[CTA_EXPECT_FLAGS]) {
 		exp->flags = ntohl(mnl_attr_get_u32(tb[CTA_EXPECT_FLAGS]));
 		set_bit(ATTR_EXP_FLAGS, exp->set);
 	}
-
 	if (tb[CTA_EXPECT_HELP_NAME]) {
 		strncpy(exp->helper_name,
 			mnl_attr_get_str(tb[CTA_EXPECT_HELP_NAME]),
 			NFCT_HELPER_NAME_MAX);
 		set_bit(ATTR_EXP_HELPER_NAME, exp->set);
 	}
+	if (tb[CTA_EXPECT_CLASS]) {
+		exp->class = ntohl(mnl_attr_get_u32(tb[CTA_EXPECT_CLASS]));
+		set_bit(ATTR_EXP_CLASS, exp->set);
+	}
+	if (tb[CTA_EXPECT_NAT])
+		nfexp_nlmsg_parse_nat(nfg, tb[CTA_EXPECT_NAT], exp);
+
+	if (tb[CTA_EXPECT_FN]) {
+		strncpy(exp->expectfn, mnl_attr_get_payload(tb[CTA_EXPECT_FN]),
+			__NFCT_EXPECTFN_MAX);
+		exp->expectfn[__NFCT_EXPECTFN_MAX - 1] = '\0';
+		set_bit(ATTR_EXP_FN, exp->set);
+	}
+
 	return 0;
 }
Signature · 1 line
2.11.0
author maintainer
#2 lore raw

PATCH 3/3 src: replace old libnfnetlink parser

In reply to Pablo Neira Ayuso

Use the new libmnl version, remove duplicated code.

---
Something similar for the build path would be good to remove duplicated code, while leaving the libnfnetlink API in place.

 include/internal/prototypes.h |   5 -
 src/callback.c                |   8 +-
 src/conntrack/Makefile.am     |   2 +-
 src/conntrack/api.c           |  31 ++-
 src/conntrack/parse.c         | 627 ------------------------------------------
 src/expect/Makefile.am        |   2 +-
 src/expect/api.c              |  30 +-
 src/expect/parse.c            | 120 --------
 8 files changed, 45 insertions(+), 780 deletions(-)
 delete mode 100644 src/conntrack/parse.c
 delete mode 100644 src/expect/parse.c
include/internal/prototypes.h +0 -5
index 251e57d4b277..c0047b3b2f01 100644
@@ -6,9 +6,6 @@
  */
 int __build_conntrack(struct nfnl_subsys_handle *ssh, struct nfnlhdr *req, size_t size, uint16_t type, uint16_t flags, const struct nf_conntrack *ct);
 void __build_tuple(struct nfnlhdr *req, size_t size, const struct __nfct_tuple *t, const int type);
-int __parse_message_type(const struct nlmsghdr *nlh);
-void __parse_conntrack(const struct nlmsghdr *nlh, struct nfattr *cda[], struct nf_conntrack *ct);
-void __parse_tuple(const struct nfattr *attr, struct __nfct_tuple *tuple, int dir, uint32_t *set);
 int __snprintf_conntrack(char *buf, unsigned int len, const struct nf_conntrack *ct, unsigned int type, unsigned int msg_output, unsigned int flags, struct nfct_labelmap *);
 int __snprintf_address(char *buf, unsigned int len, const struct __nfct_tuple *tuple, const char *src_tag, const char *dst_tag);
 int __snprintf_protocol(char *buf, unsigned int len, const struct nf_conntrack *ct);
@@ -47,8 +44,6 @@ int nfct_parse_tuple(const struct nlattr *attr, struct __nfct_tuple *tuple, int
  * expectation internal prototypes
  */
 int __build_expect(struct nfnl_subsys_handle *ssh, struct nfnlhdr *req, size_t size, uint16_t type, uint16_t flags, const struct nf_expect *exp);
-int __parse_expect_message_type(const struct nlmsghdr *nlh);
-void __parse_expect(const struct nlmsghdr *nlh, struct nfattr *cda[], struct nf_expect *exp);
 int __expect_callback(struct nlmsghdr *nlh, struct nfattr *nfa[], void *data);
 int __cmp_expect(const struct nf_expect *exp1, const struct nf_expect *exp2, unsigned int flags);
 int __snprintf_expect(char *buf, unsigned int len, const struct nf_expect *exp, unsigned int type, unsigned int msg_output, unsigned int flags);
src/callback.c +6 -2
index 69640dacd71d..7671dd64ffe8 100644
@@ -8,6 +8,7 @@
  */
 
 #include "internal/internal.h"
+#include <libmnl/libmnl.h>
 
 static int __parse_message(const struct nlmsghdr *nlh)
 {
@@ -29,6 +30,9 @@ static int __parse_message(const struct nlmsghdr *nlh)
 	return ret;
 }
 
+/* This function uses libmnl helpers, the nfa[] array is intentionally not used
+ * since it has a different layout.
+ */
 int __callback(struct nlmsghdr *nlh, struct nfattr *nfa[], void *data)
 {
 	int ret = NFNL_CB_STOP;
@@ -52,7 +56,7 @@ int __callback(struct nlmsghdr *nlh, struct nfattr *nfa[], void *data)
 		if (ct == NULL)
 			return NFNL_CB_FAILURE;
 
-		__parse_conntrack(nlh, nfa, ct);
+		nfct_nlmsg_parse(nlh, ct);
 
 		if (container->h->cb) {
 			ret = container->h->cb(type, ct, container->data);
@@ -66,7 +70,7 @@ int __callback(struct nlmsghdr *nlh, struct nfattr *nfa[], void *data)
 		if (exp == NULL)
 			return NFNL_CB_FAILURE;
 
-		__parse_expect(nlh, nfa, exp);
+		nfexp_nlmsg_parse(nlh, exp);
 
 		if (container->h->expect_cb) {
 			ret = container->h->expect_cb(type, exp,
src/conntrack/Makefile.am +1 -1
index e1d8768b7850..602ed33ffe60 100644
@@ -5,7 +5,7 @@ noinst_LTLIBRARIES = libnfconntrack.la
 libnfconntrack_la_SOURCES = api.c \
 			    getter.c setter.c \
 			    labels.c \
-			    parse.c build.c \
+			    build.c \
 			    parse_mnl.c build_mnl.c \
 			    snprintf.c \
 			    snprintf_default.c snprintf_xml.c \
src/conntrack/api.c +19 -12
index bed2e42c8f43..3a1746e4c050 100644
@@ -12,6 +12,7 @@
 #include <string.h> /* for memset */
 #include <errno.h>
 #include <assert.h>
+#include <libmnl/libmnl.h>
 
 #include "internal/internal.h"
 
@@ -880,6 +881,23 @@ int nfct_build_query(struct nfnl_subsys_handle *ssh,
 	return __build_query_ct(ssh, qt, data, buffer, size);
 }
 
+static int __parse_message_type(const struct nlmsghdr *nlh)
+{
+	uint16_t type = NFNL_MSG_TYPE(nlh->nlmsg_type);
+	uint16_t flags = nlh->nlmsg_flags;
+	int ret = NFCT_T_UNKNOWN;
+
+	if (type == IPCTNL_MSG_CT_NEW) {
+		if (flags & (NLM_F_CREATE|NLM_F_EXCL))
+			ret = NFCT_T_NEW;
+		else
+			ret = NFCT_T_UPDATE;
+	} else if (type == IPCTNL_MSG_CT_DELETE)
+		ret = NFCT_T_DESTROY;
+
+	return ret;
+}
+
 /**
  * nfct_parse_conntrack - translate a netlink message to a conntrack object
  * \param type do the translation iif the message type is of a certain type
@@ -909,26 +927,15 @@ int nfct_parse_conntrack(enum nf_conntrack_msg_type type,
 			 struct nf_conntrack *ct)
 {
 	unsigned int flags;
-	int len = nlh->nlmsg_len;
-	struct nfgenmsg *nfhdr = NLMSG_DATA(nlh);
-	struct nfattr *cda[CTA_MAX];
 
 	assert(nlh != NULL);
 	assert(ct != NULL);
 
-	len -= NLMSG_LENGTH(sizeof(struct nfgenmsg));
-	if (len < 0) {
-		errno = EINVAL;
-		return NFCT_T_ERROR;
-	}
-
 	flags = __parse_message_type(nlh);
 	if (!(flags & type))
 		return 0;
 
-	nfnl_parse_attr(cda, CTA_MAX, NFA_DATA(nfhdr), len);
-
-	__parse_conntrack(nlh, cda, ct);
+	nfct_nlmsg_parse(nlh, ct);
 
 	return flags;
 }
src/conntrack/parse.c deleted +0 -627

627 lines not on this page · raw message

src/expect/Makefile.am +1 -1
index 61d5cbdc709e..8f07daff7758 100644
@@ -5,7 +5,7 @@ noinst_LTLIBRARIES = libnfexpect.la
 libnfexpect_la_SOURCES = api.c \
 			 compare.c \
 			 getter.c setter.c \
-			 parse.c build.c \
+			 build.c \
 			 snprintf.c \
 			 snprintf_default.c \
 			 snprintf_xml.c \
src/expect/api.c +18 -12
index 3f1763038ede..b50a47f171c1 100644
@@ -594,6 +594,23 @@ int nfexp_build_query(struct nfnl_subsys_handle *ssh,
 	return __build_query_exp(ssh, qt, data, buffer, size);
 }
 
+static int __parse_expect_message_type(const struct nlmsghdr *nlh)
+{
+	uint16_t type = NFNL_MSG_TYPE(nlh->nlmsg_type);
+	uint16_t flags = nlh->nlmsg_flags;
+	int ret = NFCT_T_UNKNOWN;
+
+	if (type == IPCTNL_MSG_EXP_NEW) {
+		if (flags & (NLM_F_CREATE|NLM_F_EXCL))
+			ret = NFCT_T_NEW;
+		else
+			ret = NFCT_T_UPDATE;
+	} else if (type == IPCTNL_MSG_EXP_DELETE)
+		ret = NFCT_T_DESTROY;
+
+	return ret;
+}
+
 /**
  * nfexp_parse_expect - translate a netlink message to a conntrack object
  * \param type do the translation iif the message type is of a certain type
@@ -623,26 +640,15 @@ int nfexp_parse_expect(enum nf_conntrack_msg_type type,
 		       struct nf_expect *exp)
 {
 	unsigned int flags;
-	int len = nlh->nlmsg_len;
-	struct nfgenmsg *nfhdr = NLMSG_DATA(nlh);
-	struct nfattr *cda[CTA_EXPECT_MAX];
 
 	assert(nlh != NULL);
 	assert(exp != NULL);
 
-	len -= NLMSG_LENGTH(sizeof(struct nfgenmsg));
-	if (len < 0) {
-		errno = EINVAL;
-		return NFCT_T_ERROR;
-	}
-
 	flags = __parse_expect_message_type(nlh);
 	if (!(flags & type))
 		return 0;
 
-	nfnl_parse_attr(cda, CTA_EXPECT_MAX, NFA_DATA(nfhdr), len);
-
-	__parse_expect(nlh, cda, exp);
+	nfexp_nlmsg_parse(nlh, exp);
 
 	return flags;
 }
src/expect/parse.c deleted +0 -120

120 lines not on this page · raw message

Signature · 1 line
2.11.0