From: Ken-ichirou MATSUZAWA <chamaken@gmail.com>
To: The netfilter developer mailinglist <netfilter-devel@vger.kernel.org>
Subject: [lnf-log RFC PATCH 2/2] utils: take a example from libmnl and use nflog_nlmsg_parse
Date: Mon, 10 Aug 2015 17:17:53 +0900 [thread overview]
Message-ID: <20150810081752.GD25169@gmail.com> (raw)
In-Reply-To: <20150810081342.GB25169@gmail.com>
Signed-off-by: Ken-ichirou MATSUZAWA <chamas@h4.dion.ne.jp>
---
utils/Makefile.am | 6 +-
utils/nf-log.c | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 183 insertions(+), 1 deletion(-)
create mode 100644 utils/nf-log.c
diff --git a/utils/Makefile.am b/utils/Makefile.am
index f961b6c..dfe5f34 100644
--- a/utils/Makefile.am
+++ b/utils/Makefile.am
@@ -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
diff --git a/utils/nf-log.c b/utils/nf-log.c
new file mode 100644
index 0000000..ab9ac70
--- /dev/null
+++ b/utils/nf-log.c
@@ -0,0 +1,178 @@
+/* This example is placed in the public domain. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <time.h>
+#include <arpa/inet.h>
+
+#include <linux/netfilter.h>
+#include <linux/netfilter/nfnetlink.h>
+#include <linux/netfilter/nfnetlink_log.h>
+
+#include <libmnl/libmnl.h>
+#include <libnetfilter_log/libnetfilter_log.h>
+
+static int log_cb(const struct nlmsghdr *nlh, void *data)
+{
+ struct nflog_data *nfad;
+ struct nfulnl_msg_packet_hdr *ph = NULL;
+ const char *prefix = NULL;
+ uint32_t mark = 0;
+ int ret;
+
+ nfad = nflog_data_alloc();
+ if (nfad == NULL)
+ return MNL_CB_ERROR;
+ ret = nflog_nlmsg_parse(nlh, nfad);
+ if (ret != MNL_CB_OK)
+ return ret;
+
+ ph = nflog_get_msg_packet_hdr(nfad);
+ prefix = nflog_get_prefix(nfad);
+ mark = nflog_get_nfmark(nfad);
+
+ printf("log received (prefix=\"%s\" hw=0x%04x hook=%u mark=%u)\n",
+ prefix ? prefix : "", ntohs(ph->hw_protocol), ph->hook,
+ mark);
+
+ return MNL_CB_OK;
+}
+
+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));
+ 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;
+}
+
+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), ¶ms);
+
+ return nlh;
+}
+
+int main(int argc, char *argv[])
+{
+ struct mnl_socket *nl;
+ char buf[MNL_SOCKET_BUFFER_SIZE];
+ struct nlmsghdr *nlh;
+ int ret;
+ unsigned int portid, qnum;
+
+ if (argc != 2) {
+ printf("Usage: %s [queue_num]\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ qnum = atoi(argv[1]);
+
+ nl = mnl_socket_open(NETLINK_NETFILTER);
+ if (nl == NULL) {
+ perror("mnl_socket_open");
+ exit(EXIT_FAILURE);
+ }
+
+ if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
+ perror("mnl_socket_bind");
+ exit(EXIT_FAILURE);
+ }
+ portid = mnl_socket_get_portid(nl);
+
+ nlh = nflog_build_cfg_pf_request(buf, NFULNL_CFG_CMD_PF_UNBIND);
+
+ if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
+ perror("mnl_socket_sendto");
+ exit(EXIT_FAILURE);
+ }
+
+ nlh = nflog_build_cfg_pf_request(buf, NFULNL_CFG_CMD_PF_BIND);
+
+ if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
+ perror("mnl_socket_sendto");
+ exit(EXIT_FAILURE);
+ }
+
+ nlh = nflog_build_cfg_request(buf, NFULNL_CFG_CMD_BIND, qnum);
+
+ if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
+ perror("mnl_socket_sendto");
+ exit(EXIT_FAILURE);
+ }
+
+ nlh = nflog_build_cfg_params(buf, NFULNL_COPY_PACKET, 0xFFFF, qnum);
+
+ if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
+ perror("mnl_socket_sendto");
+ exit(EXIT_FAILURE);
+ }
+
+ ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+ if (ret == -1) {
+ perror("mnl_socket_recvfrom");
+ exit(EXIT_FAILURE);
+ }
+ while (ret > 0) {
+ ret = mnl_cb_run(buf, ret, 0, portid, log_cb, NULL);
+ if (ret < 0){
+ perror("mnl_cb_run");
+ exit(EXIT_FAILURE);
+ }
+
+ ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+ if (ret == -1) {
+ perror("mnl_socket_recvfrom");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ mnl_socket_close(nl);
+
+ return 0;
+}
--
2.1.4
next prev parent reply other threads:[~2015-08-10 8:17 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-10 8:13 [lnf-log RFC PATCH 0/2] introduce new functions to use without nflog_handle Ken-ichirou MATSUZAWA
2015-08-10 8:15 ` [lnf-log RFC PATCH 1/2] " Ken-ichirou MATSUZAWA
2015-08-18 5:48 ` Pablo Neira Ayuso
2015-08-10 8:17 ` Ken-ichirou MATSUZAWA [this message]
2015-08-18 6:04 ` [lnf-log RFC PATCH 2/2] utils: take a example from libmnl and use nflog_nlmsg_parse Pablo Neira Ayuso
2015-08-19 7:11 ` Ken-ichirou MATSUZAWA
2015-08-19 7:13 ` [lnf-log RFC PATCH v2 1/2] introduce new functions independent from libnfnetlink Ken-ichirou MATSUZAWA
2015-08-19 7:15 ` [lnf-log RFC PATCH v2 2/2] utils: take a example from libmnl and use new functions Ken-ichirou MATSUZAWA
2015-08-19 22:04 ` [lnf-log RFC PATCH 2/2] utils: take a example from libmnl and use nflog_nlmsg_parse Pablo Neira Ayuso
2015-08-20 7:26 ` Ken-ichirou MATSUZAWA
2015-08-20 7:29 ` [lnf-log PATCH 1/2] introduce new functions independent from libnfnetlink Ken-ichirou MATSUZAWA
2015-08-20 7:31 ` [lnf-log PATCH 2/2] utils: take a example from libmnl and use new functions Ken-ichirou MATSUZAWA
2015-08-20 18:16 ` [lnf-log RFC PATCH 2/2] utils: take a example from libmnl and use nflog_nlmsg_parse Pablo Neira Ayuso
2015-08-21 0:23 ` Ken-ichirou MATSUZAWA
2015-08-21 0:26 ` [lnf-log PATCHv2 1/3] introduce new functions independent from libnfnetlink Ken-ichirou MATSUZAWA
2015-08-26 19:01 ` Pablo Neira Ayuso
2015-08-21 0:27 ` [lnf-log PATCHv2 2/3] utils: take a example from libmnl and use new functions Ken-ichirou MATSUZAWA
2015-08-26 19:01 ` Pablo Neira Ayuso
2015-08-21 0:30 ` [lnf-log PATCHv2 3/3] nlmsg: add printf function in conjunction with libmnl Ken-ichirou MATSUZAWA
2015-08-26 19:01 ` Pablo Neira Ayuso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150810081752.GD25169@gmail.com \
--to=chamaken@gmail.com \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).