Skip to contentSkip to conversation

introduce new functions to use without nflog_handle

RFC PATCH In review Ken-ichirou MATSUZAWA 2 participants 20 messages Aug 10, 2015 – Aug 26, 2015 netfilter

lore
Share & tools
lore
#0 lore raw

cover introduce new functions to use without nflog_handle

Hi,

I am trying to extend ulogd to introduce mmaped netlink socket. The current libnetfilter_log can not be used since it presumes to use a normal socket.

This patch adds new functions which can parse nlmsg without nflog_handle. It would be helpful for me to be accepted to the master, how about it?

Thanks,

#1 lore raw

RFC PATCH 1/2 introduce new functions to use without nflog_handle

---
 Make_global.am                              |  2 +-
 configure.ac                                |  1 +
 include/libnetfilter_log/libnetfilter_log.h |  6 ++
 src/Makefile.am                             |  4 +-
 src/libnetfilter_log.c                      | 43 ++++++++++++++
 src/nlmsg.c                                 | 89 +++++++++++++++++++++++++++++
 6 files changed, 142 insertions(+), 3 deletions(-)
 create mode 100644 src/nlmsg.c
Make_global.am +1 -1
index a4e9bd9..9bc8ea1 100644
@@ -1,2 +1,2 @@
-AM_CPPFLAGS = -I${top_srcdir}/include ${LIBNFNETLINK_CFLAGS}
+AM_CPPFLAGS = -I${top_srcdir}/include ${LIBNFNETLINK_CFLAGS} ${LIBMNL_CFLAGS}
 AM_CFLAGS = -Wall
configure.ac +1 -0
index cdcbc90..ead9399 100644
@@ -30,6 +30,7 @@ AM_CONDITIONAL([BUILD_IPULOG], [test "x$with_ipulog" != xno])
 
 dnl Dependencies
 PKG_CHECK_MODULES([LIBNFNETLINK], [libnfnetlink >= 0.0.41])
+PKG_CHECK_MODULES([LIBMNL], [libmnl >= 1.0.3])
 
 dnl Output the makefile
 AC_CONFIG_FILES([Makefile src/Makefile include/Makefile
include/libnetfilter_log/libnetfilter_log.h +6 -0
index 7812877..044f406 100644
@@ -11,6 +11,7 @@
 
 #include <stdint.h>
 #include <sys/types.h>
+#include <linux/netlink.h>
 #include <libnetfilter_log/linux_nfnetlink_log.h>
 
 struct nflog_handle;
@@ -82,4 +83,9 @@ enum {
 
 extern int nflog_snprintf_xml(char *buf, size_t len, struct nflog_data *tb, int flags);
 
+extern struct nflog_data *nflog_data_alloc(void);
+extern void nflog_data_free(struct nflog_data *nfad);
+extern int nflog_nlmsg_parse_attrs(const struct nlmsghdr *nlh, struct nlattr **attr);
+extern int nflog_nlmsg_parse(const struct nlmsghdr *nlh, struct nflog_data *nfad);
+
 #endif	/* __LIBNETFILTER_LOG_H */
src/Makefile.am +2 -2
index 33933a4..aa56152 100644
@@ -26,8 +26,8 @@ lib_LTLIBRARIES = libnetfilter_log.la
 
 libnetfilter_log_la_LDFLAGS = -Wc,-nostartfiles -lnfnetlink	\
 			      -version-info $(LIBVERSION)
-libnetfilter_log_la_SOURCES = libnetfilter_log.c 
-libnetfilter_log_la_LIBADD  = ${LIBNFNETLINK_LIBS}
+libnetfilter_log_la_SOURCES = libnetfilter_log.c nlmsg.c
+libnetfilter_log_la_LIBADD  = ${LIBNFNETLINK_LIBS} ${LIBMNL_LIBS}
 
 if BUILD_IPULOG
 lib_LTLIBRARIES += libnetfilter_log_libipulog.la
src/libnetfilter_log.c +43 -0

54 lines not on this page · raw message

src/nlmsg.c new file +89 -0

89 lines not on this page · raw message

Signature · 1 line
2.1.4
#2 lore raw

On Mon, Aug 10, 2015 at 05:15:54PM +0900, Ken-ichirou MATSUZAWA wrote:
[...]

Quoted 24 lines
src/libnetfilter_log.c +9 -0
index e92576b..422c550 100644
@@ -26,6 +26,7 @@
 #include <errno.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
+#include <linux/netlink.h>
 
 #include <libnetfilter_log/linux_nfnetlink_log.h>
 
@@ -1067,5 +1068,47 @@ int nflog_snprintf_xml(char *buf, size_t rem, struct nflog_data *tb, int flags)
 }
 
 /**
+ * nflog_data_alloc - allocate a new nflog data
+ *
+ * In case of success, this function returns a valid pointer to a memory blob,
+ * otherwise NULL is returned and errno is set appropiately.
+ */
+struct nflog_data *nflog_data_alloc(void)
+{
+	struct nflog_data *nfad = calloc(1, sizeof(struct nflog_data));

src/libnetfilter_log.c int nflog_snprintf_xml(char *buf, size_t rem, struct nflog_data *tb, int flags) · line 1088

Quoted 10 lines
src/libnetfilter_log.c +10 -0
+	if (nfad == NULL)
+		return NULL;
+	nfad->nfa = (struct nfattr **)calloc(NFULA_MAX + 1,
+					     sizeof(struct nlattr *));
+	if (nfad->nfa == NULL) {
+		free(nfad);
+		return NULL;
+	}
+	return nfad;
+}

[...]

The libnetfilter_log.c file contains the old API, its use is discouraged since it depends on libnfnetlink.

src/nlmsg.c

Quoted 5 lines
src/nlmsg.c new file +0 -0
new file mode 100644
index 0000000..8611b9d

I like that you started the libmnl support for libnetfilter_log.

The idea is to provide a set of helper functions that we can use in conjunction with libmnl, similar to what we have in libnetfilter_queue/nlmsg.c

Thanks.

#3 lore raw

RFC PATCH 2/2 utils: take a example from libmnl and use nflog_nlmsg_parse

In reply to Ken-ichirou MATSUZAWA
---
 utils/Makefile.am |   6 +-
 utils/nf-log.c    | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 183 insertions(+), 1 deletion(-)
 create mode 100644 utils/nf-log.c
utils/Makefile.am +5 -1
index f961b6c..dfe5f34 100644
@@ -1,11 +1,15 @@
 include ${top_srcdir}/Make_global.am
 
-check_PROGRAMS = nfulnl_test
+check_PROGRAMS = nfulnl_test nf-log
 
 nfulnl_test_SOURCES = nfulnl_test.c
 nfulnl_test_LDADD = ../src/libnetfilter_log.la
 nfulnl_test_LDFLAGS = -dynamic
 
+nf_log_SOURCES = nf-log.c
+nf_log_LDADD = ../src/libnetfilter_log.la
+nf_log_LDFLAGS = -dynamic -lmnl
+
 if BUILD_IPULOG
 check_PROGRAMS += ulog_test
 
utils/nf-log.c new file +178 -0

178 lines not on this page · raw message

Signature · 1 line
2.1.4
#4 lore raw

Re: [lnf-log RFC PATCH 2/2] utils: take a example from libmnl and use nflog_nlmsg_parse

This code can be generalized a bit so you can place it in src/nlmsg.c, see below.

On Mon, Aug 10, 2015 at 05:17:53PM +0900, Ken-ichirou MATSUZAWA wrote:
[...]

Quoted 8 lines
utils/nf-log.c new file +8 -0
+static struct nlmsghdr *
+nflog_build_cfg_pf_request(char *buf, uint8_t command)
+{
+	struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
+	nlh->nlmsg_type	= (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG;
+	nlh->nlmsg_flags = NLM_F_REQUEST;
+
+	struct nfgenmsg *nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));

utils/nf-log.c line 59

Quoted 10 lines
utils/nf-log.c new file +10 -0
+	nfg->nfgen_family = AF_INET;
+	nfg->version = NFNETLINK_V0;
+
+	struct nfulnl_msg_config_cmd cmd = {
+		.command = command,
+	};
+	mnl_attr_put(nlh, NFULA_CFG_CMD, sizeof(cmd), &cmd);
+
+	return nlh;
+}

I suggest you use and implement the following new helper functions for libnetfilter_log:

struct nlmsghdr *
nfnl_nlmsg_put_header(char *buf, uint8_t type, uint8_t cmd, uint16_t qnum);

int nfnl_attr_put_cfg_mode(struct nlmsghdr *nlh, struct nfulnl_msg_config_mode *mode); int nfnl_attr_put_cfg_cmd(struct nlmsghdr *nlh, struct nfulnl_msg_config_cmd *cmd);

You can reuse code in the functions here below to make it. Thanks.

Quoted 41 lines
utils/nf-log.c new file +41 -0
+static struct nlmsghdr *
+nflog_build_cfg_request(char *buf, uint8_t command, int qnum)
+{
+	struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
+	nlh->nlmsg_type	= (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG;
+	nlh->nlmsg_flags = NLM_F_REQUEST;
+
+	struct nfgenmsg *nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
+	nfg->nfgen_family = AF_INET;
+	nfg->version = NFNETLINK_V0;
+	nfg->res_id = htons(qnum);
+
+	struct nfulnl_msg_config_cmd cmd = {
+		.command = command,
+	};
+	mnl_attr_put(nlh, NFULA_CFG_CMD, sizeof(cmd), &cmd);
+
+	return nlh;
+}
+
+static struct nlmsghdr *
+nflog_build_cfg_params(char *buf, uint8_t mode, int range, int qnum)
+{
+	struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
+	nlh->nlmsg_type	= (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG;
+	nlh->nlmsg_flags = NLM_F_REQUEST;
+
+	struct nfgenmsg *nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
+	nfg->nfgen_family = AF_UNSPEC;
+	nfg->version = NFNETLINK_V0;
+	nfg->res_id = htons(qnum);
+
+	struct nfulnl_msg_config_mode params = {
+		.copy_range = htonl(range),
+		.copy_mode = mode,
+	};
+	mnl_attr_put(nlh, NFULA_CFG_MODE, sizeof(params), &params);
+
+	return nlh;
+}
+

[...]

#5 lore raw

Re: [lnf-log RFC PATCH 2/2] utils: take a example from libmnl and use nflog_nlmsg_parse

Thank you for comment.

Quoted 8 lines · Pablo Neira Ayuso

I suggest you use and implement the following new helper functions for libnetfilter_log:

struct nlmsghdr *
nfnl_nlmsg_put_header(char *buf, uint8_t type, uint8_t cmd, uint16_t qnum);

int nfnl_attr_put_cfg_mode(struct nlmsghdr *nlh, struct nfulnl_msg_config_mode *mode); int nfnl_attr_put_cfg_cmd(struct nlmsghdr *nlh, struct nfulnl_msg_config_cmd *cmd);

Would you confirm the functions for nlmsg building which you suggested to add because it's a little different from you suggested. I am worried about it's in accodance with your intention.

In addition, I added NFLOG_DATA_SIZE as is, the size of struct nflog_data. This enables to preallocate buffer, and I added functions which initialize a buffer based on this.

Would you see it again?

Changes from previous:
* introduce nlmsg three build functions:

    struct nlmsghdr *
      nfnl_nlmsg_put_header(char *buf, uint8_t type, uint8_t family, uint16_t qnum);
    int nfnl_attr_put_cfg_mode(struct nlmsghdr *nlh, struct nfulnl_msg_config_mode *mode);
    int nfnl_attr_put_cfg_cmd(struct nlmsghdr *nlh, struct nfulnl_msg_config_cmd *cmd);

* introduce NFLOG_DATA_SIZE macro and use it at allocate functions.
* update utils/nf-log to fit with the above.

#6 lore raw

RFC PATCH v2 1/2 introduce new functions independent from libnfnetlink

The libnetfilter_log.c file contains the old API, its use is discouraged since it depends on libnfnetlink.

The idea is to provide a set of helper functions that we can use in conjunction with libmnl, similar to what we have in libnetfilter_queue/nlmsg.c

---
 Make_global.am                              |   2 +-
 configure.ac                                |   1 +
 include/libnetfilter_log/libnetfilter_log.h |  12 +++
 src/Makefile.am                             |   4 +-
 src/libnetfilter_log.c                      |  52 ++++++++++
 src/nlmsg.c                                 | 149 ++++++++++++++++++++++++++++
 6 files changed, 217 insertions(+), 3 deletions(-)
 create mode 100644 src/nlmsg.c
Make_global.am +1 -1
index a4e9bd9..9bc8ea1 100644
@@ -1,2 +1,2 @@
-AM_CPPFLAGS = -I${top_srcdir}/include ${LIBNFNETLINK_CFLAGS}
+AM_CPPFLAGS = -I${top_srcdir}/include ${LIBNFNETLINK_CFLAGS} ${LIBMNL_CFLAGS}
 AM_CFLAGS = -Wall
configure.ac +1 -0
index cdcbc90..ead9399 100644
@@ -30,6 +30,7 @@ AM_CONDITIONAL([BUILD_IPULOG], [test "x$with_ipulog" != xno])
 
 dnl Dependencies
 PKG_CHECK_MODULES([LIBNFNETLINK], [libnfnetlink >= 0.0.41])
+PKG_CHECK_MODULES([LIBMNL], [libmnl >= 1.0.3])
 
 dnl Output the makefile
 AC_CONFIG_FILES([Makefile src/Makefile include/Makefile
include/libnetfilter_log/libnetfilter_log.h +12 -0
index 7812877..c662b63 100644
@@ -11,6 +11,7 @@
 
 #include <stdint.h>
 #include <sys/types.h>
+#include <linux/netlink.h>
 #include <libnetfilter_log/linux_nfnetlink_log.h>
 
 struct nflog_handle;
@@ -82,4 +83,15 @@ enum {
 
 extern int nflog_snprintf_xml(char *buf, size_t len, struct nflog_data *tb, int flags);
 
+#define NFLOG_DATA_SIZE (sizeof(struct nfattr **) + sizeof(struct nlattr *) * (NFULA_MAX + 1))
+extern struct nflog_data *nflog_data_alloc(void);
+extern struct nflog_data *nflog_data_init(void *data);
+extern void nflog_data_free(struct nflog_data *nfad);
+extern struct nlmsghdr *
+nfnl_nlmsg_put_header(char *buf, uint8_t type, uint8_t family, uint16_t qnum);
+extern int nfnl_attr_put_cfg_mode(struct nlmsghdr *nlh, struct nfulnl_msg_config_mode *mode);
+extern int nfnl_attr_put_cfg_cmd(struct nlmsghdr *nlh, struct nfulnl_msg_config_cmd *cmd);
+extern int nflog_nlmsg_parse_attrs(const struct nlmsghdr *nlh, struct nlattr **attr);
+extern int nflog_nlmsg_parse(const struct nlmsghdr *nlh, struct nflog_data *nfad);
+
 #endif	/* __LIBNETFILTER_LOG_H */
src/Makefile.am +2 -2
index 33933a4..aa56152 100644
@@ -26,8 +26,8 @@ lib_LTLIBRARIES = libnetfilter_log.la
 
 libnetfilter_log_la_LDFLAGS = -Wc,-nostartfiles -lnfnetlink	\
 			      -version-info $(LIBVERSION)
-libnetfilter_log_la_SOURCES = libnetfilter_log.c 
-libnetfilter_log_la_LIBADD  = ${LIBNFNETLINK_LIBS}
+libnetfilter_log_la_SOURCES = libnetfilter_log.c nlmsg.c
+libnetfilter_log_la_LIBADD  = ${LIBNFNETLINK_LIBS} ${LIBMNL_LIBS}
 
 if BUILD_IPULOG
 lib_LTLIBRARIES += libnetfilter_log_libipulog.la
src/libnetfilter_log.c +52 -0

63 lines not on this page · raw message

src/nlmsg.c new file +149 -0

149 lines not on this page · raw message

Signature · 1 line
2.1.4
#7 lore raw

RFC PATCH v2 2/2 utils: take a example from libmnl and use new functions

In reply to Ken-ichirou MATSUZAWA
---
 utils/.gitignore  |   1 +
 utils/Makefile.am |   6 ++-
 utils/nf-log.c    | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 145 insertions(+), 1 deletion(-)
 create mode 100644 utils/nf-log.c
utils/.gitignore +1 -0
index 64e4588..01fa66d 100644
@@ -1,2 +1,3 @@
 /nfulnl_test
 /ulog_test
+/nf-log
utils/Makefile.am +5 -1
index f961b6c..dfe5f34 100644
@@ -1,11 +1,15 @@
 include ${top_srcdir}/Make_global.am
 
-check_PROGRAMS = nfulnl_test
+check_PROGRAMS = nfulnl_test nf-log
 
 nfulnl_test_SOURCES = nfulnl_test.c
 nfulnl_test_LDADD = ../src/libnetfilter_log.la
 nfulnl_test_LDFLAGS = -dynamic
 
+nf_log_SOURCES = nf-log.c
+nf_log_LDADD = ../src/libnetfilter_log.la
+nf_log_LDFLAGS = -dynamic -lmnl
+
 if BUILD_IPULOG
 check_PROGRAMS += ulog_test
 
utils/nf-log.c new file +139 -0

139 lines not on this page · raw message

Signature · 1 line
2.1.4
#8 lore raw

Re: [lnf-log RFC PATCH 2/2] utils: take a example from libmnl and use nflog_nlmsg_parse

In reply to Ken-ichirou MATSUZAWA

On Wed, Aug 19, 2015 at 04:11:03PM +0900, Ken-ichirou MATSUZAWA wrote:
[...]

Quoted 3 lines

Would you confirm the functions for nlmsg building which you suggested to add because it's a little different from you suggested. I am worried about it's in accodance with your intention.

I suggest you remove the nflog_data_init(), nflog_data_alloc(), nflog_data_free() and nflog_nlmsg_parse().

Those are part of the old API, we should focus on API that we can combine with libmnl.

More comments below:

Quoted 4 lines

In addition, I added NFLOG_DATA_SIZE as is, the size of struct nflog_data. This enables to preallocate buffer, and I added functions which initialize a buffer based on this.

Quoted 8 lines

Would you see it again?

Changes from previous:
* introduce nlmsg three build functions:

    struct nlmsghdr *
      nfnl_nlmsg_put_header(char *buf, uint8_t type, uint8_t family, uint16_t qnum);
    int nfnl_attr_put_cfg_mode(struct nlmsghdr *nlh, struct nfulnl_msg_config_mode *mode);
    int nfnl_attr_put_cfg_cmd(struct nlmsghdr *nlh, struct nfulnl_msg_config_cmd *cmd);

One small change more, please change this interface from:

+ nfcmd.command = NFULNL_CFG_CMD_PF_UNBIND;
+ if (nfnl_attr_put_cfg_cmd(nlh, &nfcmd) < 0) {

to:

        if (nfnl_attr_put_cfg_cmd(nlh, NFULNL_CFG_CMD_PF_UNBIND) < 0) {

Same thing with nfnl_attr_put_cfg_mode(), replace this:

+       nfmode.copy_mode = NFULNL_COPY_PACKET;
+       nfmode.copy_range = 0xFFFF;
+       if (nfnl_attr_put_cfg_mode(nlh, &nfmode) < 0) {

to:

        if (nfnl_attr_put_cfg_mode(nlh, NFULNL_COPY_PACKET, 0xffff)
Quoted 1 line

* introduce NFLOG_DATA_SIZE macro and use it at allocate functions.

You can remove NFLOG_DATA_SIZE, this is only needed by the old API, not for libmnl.

Quoted 1 line

* update utils/nf-log to fit with the above.

Thanks a lot!

#9 lore raw

Re: [lnf-log RFC PATCH 2/2] utils: take a example from libmnl and use nflog_nlmsg_parse

Thank you for the reply.

Quoted 5 lines · Pablo Neira Ayuso

I suggest you remove the nflog_data_init(), nflog_data_alloc(), nflog_data_free() and nflog_nlmsg_parse().

Those are part of the old API, we should focus on API that we can combine with libmnl.

I think I can follow your suggestion, is it correct?

Please let me ask two more things.
With these functions, nflog attrs can be acquired by mnl_attr_get_payload, but can not show as XML in one shot. To do it, may I post another patch:

    struct nflog_data *nfnl_attrs2data(struct nlattr **attr)
    {
        struct nflog_data *nfad = calloc(1, sizeof(struct nflog_data));
        if (nfad == NULL)
            return NULL;
        nfad->nfa = (struct nfattr **)&attr[1];
        return nfad;
    }

Is it acceptable?

It's just my curiosity, would you tell me what does nfnl_ prefix stand for?

Thanks,

#10 lore raw

PATCH 1/2 introduce new functions independent from libnfnetlink

The libnetfilter_log.c file contains the old API, its use is discouraged since it depends on libnfnetlink.

The idea is to provide a set of helper functions that we can use in conjunction with libmnl, similar to what we have in libnetfilter_queue/nlmsg.c

---
 Make_global.am                              |   2 +-
 configure.ac                                |   1 +
 include/libnetfilter_log/libnetfilter_log.h |   7 ++
 src/Makefile.am                             |   4 +-
 src/nlmsg.c                                 | 161 ++++++++++++++++++++++++++++
 5 files changed, 172 insertions(+), 3 deletions(-)
 create mode 100644 src/nlmsg.c
Make_global.am +1 -1
index a4e9bd9..9bc8ea1 100644
@@ -1,2 +1,2 @@
-AM_CPPFLAGS = -I${top_srcdir}/include ${LIBNFNETLINK_CFLAGS}
+AM_CPPFLAGS = -I${top_srcdir}/include ${LIBNFNETLINK_CFLAGS} ${LIBMNL_CFLAGS}
 AM_CFLAGS = -Wall
configure.ac +1 -0
index cdcbc90..ead9399 100644
@@ -30,6 +30,7 @@ AM_CONDITIONAL([BUILD_IPULOG], [test "x$with_ipulog" != xno])
 
 dnl Dependencies
 PKG_CHECK_MODULES([LIBNFNETLINK], [libnfnetlink >= 0.0.41])
+PKG_CHECK_MODULES([LIBMNL], [libmnl >= 1.0.3])
 
 dnl Output the makefile
 AC_CONFIG_FILES([Makefile src/Makefile include/Makefile
include/libnetfilter_log/libnetfilter_log.h +7 -0
index 7812877..723d812 100644
@@ -11,6 +11,7 @@
 
 #include <stdint.h>
 #include <sys/types.h>
+#include <linux/netlink.h>
 #include <libnetfilter_log/linux_nfnetlink_log.h>
 
 struct nflog_handle;
@@ -82,4 +83,10 @@ enum {
 
 extern int nflog_snprintf_xml(char *buf, size_t len, struct nflog_data *tb, int flags);
 
+extern struct nlmsghdr *
+nfnl_nlmsg_put_header(char *buf, uint8_t type, uint8_t family, uint16_t qnum);
+extern int nfnl_attr_put_cfg_mode(struct nlmsghdr *nlh, uint8_t mode, uint32_t range);
+extern int nfnl_attr_put_cfg_cmd(struct nlmsghdr *nlh, uint8_t cmd);
+extern int nfnl_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr);
+
 #endif	/* __LIBNETFILTER_LOG_H */
src/Makefile.am +2 -2
index 33933a4..aa56152 100644
@@ -26,8 +26,8 @@ lib_LTLIBRARIES = libnetfilter_log.la
 
 libnetfilter_log_la_LDFLAGS = -Wc,-nostartfiles -lnfnetlink	\
 			      -version-info $(LIBVERSION)
-libnetfilter_log_la_SOURCES = libnetfilter_log.c 
-libnetfilter_log_la_LIBADD  = ${LIBNFNETLINK_LIBS}
+libnetfilter_log_la_SOURCES = libnetfilter_log.c nlmsg.c
+libnetfilter_log_la_LIBADD  = ${LIBNFNETLINK_LIBS} ${LIBMNL_LIBS}
 
 if BUILD_IPULOG
 lib_LTLIBRARIES += libnetfilter_log_libipulog.la
src/nlmsg.c new file +161 -0

161 lines not on this page · raw message

Signature · 1 line
2.1.4
#11 lore raw

PATCH 2/2 utils: take a example from libmnl and use new functions

In reply to Ken-ichirou MATSUZAWA
---
 utils/.gitignore  |   1 +
 utils/Makefile.am |   6 ++-
 utils/nf-log.c    | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+), 1 deletion(-)
 create mode 100644 utils/nf-log.c
utils/.gitignore +1 -0
index 64e4588..01fa66d 100644
@@ -1,2 +1,3 @@
 /nfulnl_test
 /ulog_test
+/nf-log
utils/Makefile.am +5 -1
index f961b6c..dfe5f34 100644
@@ -1,11 +1,15 @@
 include ${top_srcdir}/Make_global.am
 
-check_PROGRAMS = nfulnl_test
+check_PROGRAMS = nfulnl_test nf-log
 
 nfulnl_test_SOURCES = nfulnl_test.c
 nfulnl_test_LDADD = ../src/libnetfilter_log.la
 nfulnl_test_LDFLAGS = -dynamic
 
+nf_log_SOURCES = nf-log.c
+nf_log_LDADD = ../src/libnetfilter_log.la
+nf_log_LDFLAGS = -dynamic -lmnl
+
 if BUILD_IPULOG
 check_PROGRAMS += ulog_test
 
utils/nf-log.c new file +131 -0

131 lines not on this page · raw message

Signature · 1 line
2.1.4
#12 lore raw

Re: [lnf-log RFC PATCH 2/2] utils: take a example from libmnl and use nflog_nlmsg_parse

In reply to Ken-ichirou MATSUZAWA
Quoted 26 lines · Ken-ichirou MATSUZAWA

Thank you for the reply.

Quoted 5 lines · Pablo Neira Ayuso

I suggest you remove the nflog_data_init(), nflog_data_alloc(), nflog_data_free() and nflog_nlmsg_parse().

Those are part of the old API, we should focus on API that we can combine with libmnl.

I think I can follow your suggestion, is it correct?

Please let me ask two more things.
With these functions, nflog attrs can be acquired by mnl_attr_get_payload, but can not show as XML in one shot.

To do it, may I post another patch:

    struct nflog_data *nfnl_attrs2data(struct nlattr **attr)
    {
        struct nflog_data *nfad = calloc(1, sizeof(struct nflog_data));
        if (nfad == NULL)
            return NULL;
        nfad->nfa = (struct nfattr **)&attr[1];
        return nfad;
    }
Quoted 1 line

Is it acceptable?

I would like not to have any code that mixes both APIs. The idea is to deprecate libnfnetlink and any client of that library at some point, that will take quite time though since we'll have to mark those old interfaces as deprecated.

Please, see my rough proposal patch attached so you also have access to the XML output, it applies on top of your patchset. I made some quick testing here, review it. Note that I have also included the nlmsghdr as it contains useful information that we may want to display in the feature (eg. queue number).

Quoted 2 lines

It's just my curiosity, would you tell me what does nfnl_ prefix stand for?

Good point, we should be using nflog_ prefix instead, sorry. nfnl_ is already used by libnfnetlink so we can't use it.

Will you send me a new round of patches? Thanks!

include/internal.h new file +9 -0
new file mode 100644
index 0000000..7839fd8
@@ -0,0 +1,9 @@
+#ifndef _LIBNETFILTER_LOG_INTERNAL_H
+#define _LIBNETFILTER_LOG_INTERNAL_H
+
+struct nflog_data
+{
+	struct nfattr **nfa;
+};
+
+#endif
include/libnetfilter_log/libnetfilter_log.h +8 -0
index 723d812..60cabb9 100644
@@ -89,4 +89,12 @@ extern int nfnl_attr_put_cfg_mode(struct nlmsghdr *nlh, uint8_t mode, uint32_t r
 extern int nfnl_attr_put_cfg_cmd(struct nlmsghdr *nlh, uint8_t cmd);
 extern int nfnl_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr);
 
+enum nflog_output_type {
+	NFLOG_OUTPUT_XML	= 0,
+};
+
+int nflog_nlmsg_snprintf(char *buf, size_t bufsiz, const struct nlmsghdr *nlh,
+			 struct nlattr **attr, enum nflog_output_type type,
+			 uint32_t flags);
+
 #endif	/* __LIBNETFILTER_LOG_H */
src/libnetfilter_log.c +1 -5
index e92576b..567049c 100644
@@ -26,6 +26,7 @@
 #include <errno.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
+#include "internal.h"
 
 #include <libnetfilter_log/linux_nfnetlink_log.h>
 
@@ -78,11 +79,6 @@ struct nflog_g_handle
 	void *data;
 };
 
-struct nflog_data
-{
-	struct nfattr **nfa;
-};
-
 int nflog_errno;
 
 /***********************************************************************
src/nlmsg.c +24 -0
index 5c133fa..86da579 100644
@@ -9,6 +9,9 @@
 #include <arpa/inet.h>
 #include <linux/netfilter/nfnetlink_log.h>
 #include <libmnl/libmnl.h>
+#include <libnetfilter_log/libnetfilter_log.h>
+#include <errno.h>
+#include "internal.h"
 
 /**
  * \defgroup nlmsg Netlink message helper functions
@@ -156,6 +159,27 @@ int nfnl_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr)
 			      nfnl_parse_attr_cb, attr);
 }
 
+int nflog_nlmsg_snprintf(char *buf, size_t bufsiz, const struct nlmsghdr *nlh,
+			 struct nlattr **attr, enum nflog_output_type type,
+			 uint32_t flags)
+{
+	/* This is a hack to re-use the existing old API code. */
+	struct nflog_data nfad = {
+		.nfa	= (struct nfattr **)&attr[1],
+	};
+	int ret;
+
+	switch (type) {
+	case NFLOG_OUTPUT_XML:
+		ret = nflog_snprintf_xml(buf, bufsiz, &nfad, flags);
+	default:
+		ret = -1;
+		errno = EOPNOTSUPP;
+		break;
+	}
+	return ret;
+}
+
 /**
  * @}
  */
utils/nf-log.c +5 -0
index 0ff1392..dafaeb7 100644
@@ -14,6 +14,7 @@ static int log_cb(const struct nlmsghdr *nlh, void *data)
 	struct nfulnl_msg_packet_hdr *ph = NULL;
 	const char *prefix = NULL;
 	uint32_t mark = 0;
+	char buf[4096];
 	int ret;
 
 	ret = nfnl_nlmsg_parse(nlh, attrs);
@@ -31,6 +32,10 @@ static int log_cb(const struct nlmsghdr *nlh, void *data)
 		prefix ? prefix : "", ntohs(ph->hw_protocol), ph->hook,
 		mark);
 
+	nflog_nlmsg_snprintf(buf, sizeof(buf), nlh, attrs, NFLOG_OUTPUT_XML,
+			     NFLOG_XML_ALL);
+	printf("%s (ret =%d)\n", buf, ret);
+
 	return MNL_CB_OK;
 }
 
#13 lore raw

Re: [lnf-log RFC PATCH 2/2] utils: take a example from libmnl and use nflog_nlmsg_parse

Thank you for the reply and patch.

Quoted 2 lines · Pablo Neira Ayuso

Please, see my rough proposal patch attached so you also have access to the XML output, it applies on top of your patchset. I made some

Thank you, I can printf a nflog in XML by nlattrs.

Quoted 1 line

Will you send me a new round of patches? Thanks!

Is the following patches right to start a new round? # I quote your message in log many times, thanks.

Thanks,

#14 lore raw

PATCH v2 1/3 introduce new functions independent from libnfnetlink

The libnetfilter_log.c file contains the old API, its use is discouraged since it depends on libnfnetlink.

The idea is to provide a set of helper functions that we can use in conjunction with libmnl, similar to what we have in libnetfilter_queue/nlmsg.c

---
 Make_global.am                              |   2 +-
 configure.ac                                |   1 +
 include/libnetfilter_log/libnetfilter_log.h |   7 ++
 src/Makefile.am                             |   4 +-
 src/nlmsg.c                                 | 161 ++++++++++++++++++++++++++++
 5 files changed, 172 insertions(+), 3 deletions(-)
 create mode 100644 src/nlmsg.c
Make_global.am +1 -1
index a4e9bd9..9bc8ea1 100644
@@ -1,2 +1,2 @@
-AM_CPPFLAGS = -I${top_srcdir}/include ${LIBNFNETLINK_CFLAGS}
+AM_CPPFLAGS = -I${top_srcdir}/include ${LIBNFNETLINK_CFLAGS} ${LIBMNL_CFLAGS}
 AM_CFLAGS = -Wall
configure.ac +1 -0
index cdcbc90..ead9399 100644
@@ -30,6 +30,7 @@ AM_CONDITIONAL([BUILD_IPULOG], [test "x$with_ipulog" != xno])
 
 dnl Dependencies
 PKG_CHECK_MODULES([LIBNFNETLINK], [libnfnetlink >= 0.0.41])
+PKG_CHECK_MODULES([LIBMNL], [libmnl >= 1.0.3])
 
 dnl Output the makefile
 AC_CONFIG_FILES([Makefile src/Makefile include/Makefile
include/libnetfilter_log/libnetfilter_log.h +7 -0
index 7812877..5087f6f 100644
@@ -11,6 +11,7 @@
 
 #include <stdint.h>
 #include <sys/types.h>
+#include <linux/netlink.h>
 #include <libnetfilter_log/linux_nfnetlink_log.h>
 
 struct nflog_handle;
@@ -82,4 +83,10 @@ enum {
 
 extern int nflog_snprintf_xml(char *buf, size_t len, struct nflog_data *tb, int flags);
 
+extern struct nlmsghdr *
+nflog_nlmsg_put_header(char *buf, uint8_t type, uint8_t family, uint16_t qnum);
+extern int nflog_attr_put_cfg_mode(struct nlmsghdr *nlh, uint8_t mode, uint32_t range);
+extern int nflog_attr_put_cfg_cmd(struct nlmsghdr *nlh, uint8_t cmd);
+extern int nflog_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr);
+
 #endif	/* __LIBNETFILTER_LOG_H */
src/Makefile.am +2 -2
index 33933a4..aa56152 100644
@@ -26,8 +26,8 @@ lib_LTLIBRARIES = libnetfilter_log.la
 
 libnetfilter_log_la_LDFLAGS = -Wc,-nostartfiles -lnfnetlink	\
 			      -version-info $(LIBVERSION)
-libnetfilter_log_la_SOURCES = libnetfilter_log.c 
-libnetfilter_log_la_LIBADD  = ${LIBNFNETLINK_LIBS}
+libnetfilter_log_la_SOURCES = libnetfilter_log.c nlmsg.c
+libnetfilter_log_la_LIBADD  = ${LIBNFNETLINK_LIBS} ${LIBMNL_LIBS}
 
 if BUILD_IPULOG
 lib_LTLIBRARIES += libnetfilter_log_libipulog.la
src/nlmsg.c new file +161 -0

161 lines not on this page · raw message

Signature · 1 line
2.1.4
#15 lore raw

Re: [lnf-log PATCHv2 1/3] introduce new functions independent from libnfnetlink

Quoted 6 lines · Ken-ichirou MATSUZAWA

The libnetfilter_log.c file contains the old API, its use is discouraged since it depends on libnfnetlink.

The idea is to provide a set of helper functions that we can use in conjunction with libmnl, similar to what we have in libnetfilter_queue/nlmsg.c

Applied with minor change, thanks!

src/nlmsg.c line 65

Quoted 11 lines
src/nlmsg.c new file +11 -0
+int nflog_attr_put_cfg_mode(struct nlmsghdr *nlh, uint8_t mode, uint32_t range)
+{
+	struct nfulnl_msg_config_mode nfmode = {
+		.copy_mode = mode,
+		.copy_range = htonl(range)
+	};
+
+	mnl_attr_put(nlh, NFULA_CFG_MODE, sizeof(nfmode), &nfmode);
+
+	/* it may returns -1 in future */
+	return 1;

This is now returning 0 instead.

So we reserve negative values for errors, 0 means success and >= 0 are left unused for future use.

#16 lore raw

PATCH v2 2/3 utils: take a example from libmnl and use new functions

In reply to Ken-ichirou MATSUZAWA
---
 utils/.gitignore  |   1 +
 utils/Makefile.am |   6 ++-
 utils/nf-log.c    | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+), 1 deletion(-)
 create mode 100644 utils/nf-log.c
utils/.gitignore +1 -0
index 64e4588..01fa66d 100644
@@ -1,2 +1,3 @@
 /nfulnl_test
 /ulog_test
+/nf-log
utils/Makefile.am +5 -1
index f961b6c..dfe5f34 100644
@@ -1,11 +1,15 @@
 include ${top_srcdir}/Make_global.am
 
-check_PROGRAMS = nfulnl_test
+check_PROGRAMS = nfulnl_test nf-log
 
 nfulnl_test_SOURCES = nfulnl_test.c
 nfulnl_test_LDADD = ../src/libnetfilter_log.la
 nfulnl_test_LDFLAGS = -dynamic
 
+nf_log_SOURCES = nf-log.c
+nf_log_LDADD = ../src/libnetfilter_log.la
+nf_log_LDFLAGS = -dynamic -lmnl
+
 if BUILD_IPULOG
 check_PROGRAMS += ulog_test
 
utils/nf-log.c new file +131 -0

131 lines not on this page · raw message

Signature · 1 line
2.1.4
#17 lore raw

Re: [lnf-log PATCHv2 2/3] utils: take a example from libmnl and use new functions

Also applied, thanks!

#18 lore raw

PATCH v2 3/3 nlmsg: add printf function in conjunction with libmnl

In reply to Ken-ichirou MATSUZAWA

To printf nflog netlink message in XML, nflog_snprintf_xml can be used after converting nflog nlattrs to nflog_data, but we should not have any code that mixes both new and old, which handles nflog_data, APIs. The idea is to deprecate libnfnetlink and any client of that library at some point, that will take quite time though since we'll have to mark those old interfaces as deprecated.

---
 include/internal.h                          |  9 +++++
 include/libnetfilter_log/libnetfilter_log.h |  8 +++++
 src/libnetfilter_log.c                      |  6 +---
 src/nlmsg.c                                 | 53 +++++++++++++++++++++++++++++
 utils/nf-log.c                              |  7 ++++
 5 files changed, 78 insertions(+), 5 deletions(-)
 create mode 100644 include/internal.h
include/internal.h new file +9 -0
new file mode 100644
index 0000000..7839fd8
@@ -0,0 +1,9 @@
+#ifndef _LIBNETFILTER_LOG_INTERNAL_H
+#define _LIBNETFILTER_LOG_INTERNAL_H
+
+struct nflog_data
+{
+	struct nfattr **nfa;
+};
+
+#endif
include/libnetfilter_log/libnetfilter_log.h +8 -0
index 5087f6f..46767eb 100644
@@ -89,4 +89,12 @@ extern int nflog_attr_put_cfg_mode(struct nlmsghdr *nlh, uint8_t mode, uint32_t
 extern int nflog_attr_put_cfg_cmd(struct nlmsghdr *nlh, uint8_t cmd);
 extern int nflog_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr);
 
+enum nflog_output_type {
+	NFLOG_OUTPUT_XML	= 0,
+};
+
+int nflog_nlmsg_snprintf(char *buf, size_t bufsiz, const struct nlmsghdr *nlh,
+			 struct nlattr **attr, enum nflog_output_type type,
+			 uint32_t flags);
+
 #endif	/* __LIBNETFILTER_LOG_H */
src/libnetfilter_log.c +1 -5
index e92576b..567049c 100644
@@ -26,6 +26,7 @@
 #include <errno.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
+#include "internal.h"
 
 #include <libnetfilter_log/linux_nfnetlink_log.h>
 
@@ -78,11 +79,6 @@ struct nflog_g_handle
 	void *data;
 };
 
-struct nflog_data
-{
-	struct nfattr **nfa;
-};
-
 int nflog_errno;
 
 /***********************************************************************
src/nlmsg.c +53 -0

64 lines not on this page · raw message

utils/nf-log.c +7 -0
index d575b77..5f2a192 100644
@@ -14,6 +14,7 @@ static int log_cb(const struct nlmsghdr *nlh, void *data)
 	struct nfulnl_msg_packet_hdr *ph = NULL;
 	const char *prefix = NULL;
 	uint32_t mark = 0;
+	char buf[4096];
 	int ret;
 
 	ret = nflog_nlmsg_parse(nlh, attrs);
@@ -31,6 +32,12 @@ static int log_cb(const struct nlmsghdr *nlh, void *data)
 		prefix ? prefix : "", ntohs(ph->hw_protocol), ph->hook,
 		mark);
 
+	ret = nflog_nlmsg_snprintf(buf, sizeof(buf), nlh, attrs,
+				   NFLOG_OUTPUT_XML, NFLOG_XML_ALL);
+	if (ret < 0)
+		return MNL_CB_ERROR;
+	printf("%s (ret=%d)\n", buf, ret);
+
 	return MNL_CB_OK;
 }
 
Signature · 1 line
2.1.4
#19 lore raw

Re: [lnf-log PATCHv2 3/3] nlmsg: add printf function in conjunction with libmnl

Quoted 6 lines · Ken-ichirou MATSUZAWA

To printf nflog netlink message in XML, nflog_snprintf_xml can be used after converting nflog nlattrs to nflog_data, but we should not have any code that mixes both new and old, which handles nflog_data, APIs. The idea is to deprecate libnfnetlink and any client of that library at some point, that will take quite time though since we'll have to mark those old interfaces as deprecated.

this one also applied, thanks.