Skip to contentSkip to conversation

src: remove old libnfnetlink parsers

PATCH New Pablo Neira Ayuso 1 participant 1 message Mar 7, 2017 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 src: remove old libnfnetlink parsers

Use the new parsers that are based on libmnl, yet another small step to reduce dependencies with the old libnfnetlink library.

Backward compatibility is preserved after this change. Otherwise, report a bug since this patch should not affect existing behaviour.

---
 include/internal/prototypes.h |   5 -
 src/callback.c                |   8 +-
 src/conntrack/Makefile.am     |   2 +-
 src/conntrack/api.c           |  31 ++-
 src/conntrack/parse.c         | 599 ------------------------------------------
 src/expect/Makefile.am        |   2 +-
 src/expect/api.c              |  30 ++-
 src/expect/parse.c            | 120 ---------
 8 files changed, 45 insertions(+), 752 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 -599

599 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.1.4