[PATCH v3 2/4] tests: add check for NETLINK_NETFILTER parser
Chen Jingpiao
chenjingpiao at gmail.com
Wed Mar 7 04:34:45 UTC 2018
* tests/netlink_netfilter.c: Include <netinet/in.h>, <arpa/inet.h>
and <linux/netfilter/nf_tables.h>.
Replace "netlink.h" with "test_netlink.h".
(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWTABLE): New macros.
(test_nlmsg_done, test_nfgenmsg): New functions.
(main): Use them.
---
tests/netlink_netfilter.c | 121 +++++++++++++++++++++++++++++++++++++-
1 file changed, 119 insertions(+), 2 deletions(-)
diff --git a/tests/netlink_netfilter.c b/tests/netlink_netfilter.c
index db2622eb7..0047b0a38 100644
--- a/tests/netlink_netfilter.c
+++ b/tests/netlink_netfilter.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017 JingPiao Chen <chenjingpiao at gmail.com>
+ * Copyright (c) 2017, 2018 Chen Jingpiao <chenjingpiao at gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -32,9 +32,21 @@
# include <stdio.h>
# include <string.h>
# include <unistd.h>
+# include <netinet/in.h>
+# include <arpa/inet.h>
# include <sys/socket.h>
-# include "netlink.h"
+# include "test_netlink.h"
# include <linux/netfilter/nfnetlink.h>
+# ifdef HAVE_LINUX_NETFILTER_NF_TABLES_H
+# include <linux/netfilter/nf_tables.h>
+# endif
+
+# ifndef NFNL_SUBSYS_NFTABLES
+# define NFNL_SUBSYS_NFTABLES 10
+# endif
+# ifndef NFT_MSG_NEWTABLE
+# define NFT_MSG_NEWTABLE 0
+# endif
static void
test_nlmsg_type(const int fd)
@@ -70,6 +82,109 @@ test_nlmsg_type(const int fd)
fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
}
+static void
+test_nlmsg_done(const int fd)
+{
+ void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
+ const int num = 0xabcdefad;
+
+ TEST_NETLINK(fd, nlh0, NLMSG_DONE, NLM_F_REQUEST,
+ sizeof(num), &num, sizeof(num),
+ printf("%d", num));
+}
+
+static void
+test_nfgenmsg(const int fd)
+{
+ void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
+
+ struct nfgenmsg msg = {
+ .nfgen_family = AF_UNIX,
+ .version = 0,
+ .res_id = NFNL_SUBSYS_NFTABLES
+ };
+
+ TEST_NETLINK_OBJECT_EX_(fd, nlh0,
+ NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_NEWTABLE,
+ "NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_NEWTABLE",
+ NLM_F_REQUEST, "NLM_F_REQUEST",
+ msg, print_quoted_hex,
+ printf("{nfgen_family=AF_UNIX");
+# ifdef NFNETLINK_V0
+ printf(", version=NFNETLINK_V0");
+# else
+ printf(", version=%#x /* NFNETLINK_??? */",
+ msg.version);
+# endif
+ printf(", res_id=");
+# ifdef WORDS_BIGENDIAN
+ printf("htons(NFNL_SUBSYS_NFTABLES)");
+# else
+ printf("NFNL_SUBSYS_NFTABLES");
+# endif
+ );
+
+ msg.res_id = htons(NFNL_SUBSYS_NFTABLES);
+ TEST_NETLINK_(fd, nlh0,
+ NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_NEWTABLE,
+ "NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_NEWTABLE",
+ NLM_F_REQUEST, "NLM_F_REQUEST",
+ sizeof(msg), &msg, sizeof(msg),
+ printf("{nfgen_family=AF_UNIX");
+# ifdef NFNETLINK_V0
+ printf(", version=NFNETLINK_V0");
+# else
+ printf(", version=%#x /* NFNETLINK_??? */", msg.version);
+# endif
+ printf(", res_id=htons(NFNL_SUBSYS_NFTABLES)"));
+
+ msg.res_id = htons(0xabcd);
+ TEST_NETLINK_(fd, nlh0,
+ NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_NEWTABLE,
+ "NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_NEWTABLE",
+ NLM_F_REQUEST, "NLM_F_REQUEST",
+ sizeof(msg), &msg, sizeof(msg),
+ printf("{nfgen_family=AF_UNIX");
+# ifdef NFNETLINK_V0
+ printf(", version=NFNETLINK_V0");
+# else
+ printf(", version=%#x /* NFNETLINK_??? */", msg.version);
+# endif
+ printf(", res_id=htons(%d)", 0xabcd));
+
+# ifdef NFNL_MSG_BATCH_BEGIN
+ msg.res_id = htons(NFNL_SUBSYS_NFTABLES);
+ TEST_NETLINK(fd, nlh0,
+ NFNL_MSG_BATCH_BEGIN, NLM_F_REQUEST,
+ sizeof(msg), &msg, sizeof(msg),
+ printf("{nfgen_family=AF_UNIX");
+# ifdef NFNETLINK_V0
+ printf(", version=NFNETLINK_V0");
+# else
+ printf(", version=%#x /* NFNETLINK_??? */", msg.version);
+# endif
+ printf(", res_id=htons(%d)", NFNL_SUBSYS_NFTABLES));
+
+ char str_buf[NLMSG_ALIGN(sizeof(msg)) + 4];
+
+ msg.res_id = htons(0xabcd);
+ memcpy(str_buf, &msg, sizeof(msg));
+ memcpy(str_buf + NLMSG_ALIGN(sizeof(msg)), "1234", 4);
+
+ TEST_NETLINK(fd, nlh0,
+ NFNL_MSG_BATCH_BEGIN, NLM_F_REQUEST,
+ sizeof(str_buf), str_buf, sizeof(str_buf),
+ printf("{nfgen_family=AF_UNIX");
+# ifdef NFNETLINK_V0
+ printf(", version=NFNETLINK_V0");
+# else
+ printf(", version=%#x /* NFNETLINK_??? */", msg.version);
+# endif
+ printf(", res_id=htons(%d)"
+ ", \"\\x31\\x32\\x33\\x34\"", 0xabcd));
+# endif /* NFNL_MSG_BATCH_BEGIN */
+}
+
int main(void)
{
skip_if_unavailable("/proc/self/fd/");
@@ -77,6 +192,8 @@ int main(void)
int fd = create_nl_socket(NETLINK_NETFILTER);
test_nlmsg_type(fd);
+ test_nlmsg_done(fd);
+ test_nfgenmsg(fd);
printf("+++ exited with 0 +++\n");
--
2.16.2.347.gba1b3b449.dirty
More information about the Strace-devel
mailing list