[libnftables PATCH] examples: add nft-ruleset-get

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This example prints the ruleset, using the ruleset API of nftables.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@xxxxxxxxx>
---
 examples/Makefile.am       |    4 
 examples/nft-ruleset-get.c |  427 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 431 insertions(+)
 create mode 100644 examples/nft-ruleset-get.c

diff --git a/examples/Makefile.am b/examples/Makefile.am
index 9965387..b909684 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -25,6 +25,7 @@ check_PROGRAMS = nft-table-add		\
 		 nft-set-elem-add	\
 		 nft-set-elem-get	\
 		 nft-set-elem-del	\
+		 nft-ruleset-get	\
 		 nft-compat-get
 
 nft_table_add_SOURCES = nft-table-add.c
@@ -102,5 +103,8 @@ nft_set_elem_del_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
 nft_set_elem_get_SOURCES = nft-set-elem-get.c
 nft_set_elem_get_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
 
+nft_ruleset_get_SOURCES = nft-ruleset-get.c
+nft_ruleset_get_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
+
 nft_compat_get_SOURCES = nft-compat-get.c
 nft_compat_get_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
diff --git a/examples/nft-ruleset-get.c b/examples/nft-ruleset-get.c
new file mode 100644
index 0000000..ba2f17e
--- /dev/null
+++ b/examples/nft-ruleset-get.c
@@ -0,0 +1,427 @@
+/*
+ * Copyright (c) 2013 Arturo Borrero Gonzalez <arturo.borrero.glez@xxxxxxxxx>
+ *
+ * based on previous code from:
+ *
+ * Copyright (c) 2013 Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+#include <linux/netfilter.h>
+#include <linux/netfilter/nf_tables.h>
+
+#include <libmnl/libmnl.h>
+#include <libnftables/ruleset.h>
+#include <libnftables/table.h>
+#include <libnftables/chain.h>
+#include <libnftables/set.h>
+#include <libnftables/rule.h>
+
+static int seq;
+
+static void memory_allocation_error(void)
+{
+	perror("OOM");
+	exit(EXIT_FAILURE);
+}
+
+static int
+mnl_talk(struct mnl_socket *nf_sock, const void *data, unsigned int len,
+	 int (*cb)(const struct nlmsghdr *nlh, void *data), void *cb_data)
+{
+	char buf[MNL_SOCKET_BUFFER_SIZE];
+	uint32_t portid = mnl_socket_get_portid(nf_sock);
+	int ret;
+
+	if (mnl_socket_sendto(nf_sock, data, len) < 0)
+		return -1;
+
+	ret = mnl_socket_recvfrom(nf_sock, buf, sizeof(buf));
+	while (ret > 0) {
+		ret = mnl_cb_run(buf, ret, seq, portid, cb, cb_data);
+		if (ret <= 0)
+			goto out;
+
+		ret = mnl_socket_recvfrom(nf_sock, buf, sizeof(buf));
+	}
+out:
+	if (ret < 0 && errno == EAGAIN)
+		return 0;
+
+	return ret;
+}
+
+/*
+ * Rule
+ */
+static int rule_cb(const struct nlmsghdr *nlh, void *data)
+{
+	struct nft_rule_list *nlr_list = data;
+	struct nft_rule *r;
+
+	r = nft_rule_alloc();
+	if (r == NULL)
+		memory_allocation_error();
+
+	if (nft_rule_nlmsg_parse(nlh, r) < 0)
+		goto err_free;
+
+	nft_rule_list_add_tail(r, nlr_list);
+	return MNL_CB_OK;
+
+err_free:
+	nft_rule_free(r);
+	return MNL_CB_OK;
+}
+
+static struct nft_rule_list *mnl_rule_dump(struct mnl_socket *nf_sock,
+					   int family)
+{
+	char buf[MNL_SOCKET_BUFFER_SIZE];
+	struct nlmsghdr *nlh;
+	struct nft_rule_list *nlr_list;
+	int ret;
+
+	nlr_list = nft_rule_list_alloc();
+	if (nlr_list == NULL)
+		memory_allocation_error();
+
+	nlh = nft_rule_nlmsg_build_hdr(buf, NFT_MSG_GETRULE, family,
+				       NLM_F_DUMP, seq);
+
+	ret = mnl_talk(nf_sock, nlh, nlh->nlmsg_len, rule_cb, nlr_list);
+	if (ret < 0)
+		goto err;
+
+	return nlr_list;
+err:
+	nft_rule_list_free(nlr_list);
+	return NULL;
+}
+
+/*
+ * Chain
+ */
+static int chain_cb(const struct nlmsghdr *nlh, void *data)
+{
+	struct nft_chain_list *nlc_list = data;
+	struct nft_chain *c;
+
+	c = nft_chain_alloc();
+	if (c == NULL)
+		memory_allocation_error();
+
+	if (nft_chain_nlmsg_parse(nlh, c) < 0)
+		goto err_free;
+
+	nft_chain_list_add_tail(c, nlc_list);
+	return MNL_CB_OK;
+
+err_free:
+	nft_chain_free(c);
+	return MNL_CB_OK;
+}
+
+static struct nft_chain_list *mnl_chain_dump(struct mnl_socket *nf_sock,
+					     int family)
+{
+	char buf[MNL_SOCKET_BUFFER_SIZE];
+	struct nlmsghdr *nlh;
+	struct nft_chain_list *nlc_list;
+	int ret;
+
+	nlc_list = nft_chain_list_alloc();
+	if (nlc_list == NULL)
+		memory_allocation_error();
+
+	nlh = nft_chain_nlmsg_build_hdr(buf, NFT_MSG_GETCHAIN, family,
+					NLM_F_DUMP, seq);
+
+	ret = mnl_talk(nf_sock, nlh, nlh->nlmsg_len, chain_cb, nlc_list);
+	if (ret < 0)
+		goto err;
+
+	return nlc_list;
+err:
+	nft_chain_list_free(nlc_list);
+	return NULL;
+}
+
+/*
+ * Table
+ */
+static int table_cb(const struct nlmsghdr *nlh, void *data)
+{
+	struct nft_table_list *nlt_list = data;
+	struct nft_table *t;
+
+	t = nft_table_alloc();
+	if (t == NULL)
+		memory_allocation_error();
+
+	if (nft_table_nlmsg_parse(nlh, t) < 0)
+		goto err_free;
+
+	nft_table_list_add_tail(t, nlt_list);
+	return MNL_CB_OK;
+
+err_free:
+	nft_table_free(t);
+	return MNL_CB_OK;
+}
+
+static struct nft_table_list *mnl_table_dump(struct mnl_socket *nf_sock,
+					     int family)
+{
+	char buf[MNL_SOCKET_BUFFER_SIZE];
+	struct nlmsghdr *nlh;
+	struct nft_table_list *nlt_list;
+	int ret;
+
+	nlt_list = nft_table_list_alloc();
+	if (nlt_list == NULL)
+		memory_allocation_error();
+
+	nlh = nft_table_nlmsg_build_hdr(buf, NFT_MSG_GETTABLE, family,
+					NLM_F_DUMP, seq);
+
+	ret = mnl_talk(nf_sock, nlh, nlh->nlmsg_len, table_cb, nlt_list);
+	if (ret < 0)
+		goto err;
+
+	return nlt_list;
+err:
+	nft_table_list_free(nlt_list);
+	return NULL;
+}
+
+/*
+ * Set
+ */
+static int set_cb(const struct nlmsghdr *nlh, void *data)
+{
+	struct nft_set_list *nls_list = data;
+	struct nft_set *s;
+
+	s = nft_set_alloc();
+	if (s == NULL)
+		memory_allocation_error();
+
+	if (nft_set_nlmsg_parse(nlh, s) < 0)
+		goto err_free;
+
+	nft_set_list_add_tail(s, nls_list);
+	return MNL_CB_OK;
+
+err_free:
+	nft_set_free(s);
+	return MNL_CB_OK;
+}
+
+static struct nft_set_list *
+mnl_set_dump(struct mnl_socket *nf_sock, int family, const char *table)
+{
+	char buf[MNL_SOCKET_BUFFER_SIZE];
+	struct nlmsghdr *nlh;
+	struct nft_set *s;
+	struct nft_set_list *nls_list;
+	int ret;
+
+	s = nft_set_alloc();
+	if (s == NULL)
+		memory_allocation_error();
+
+	nlh = nft_set_nlmsg_build_hdr(buf, NFT_MSG_GETSET, family,
+				      NLM_F_DUMP|NLM_F_ACK, seq);
+	nft_set_attr_set(s, NFT_SET_ATTR_TABLE, table);
+	nft_set_nlmsg_build_payload(nlh, s);
+	nft_set_free(s);
+
+	nls_list = nft_set_list_alloc();
+	if (nls_list == NULL)
+		memory_allocation_error();
+
+	ret = mnl_talk(nf_sock, nlh, nlh->nlmsg_len, set_cb, nls_list);
+	if (ret < 0)
+		goto err;
+
+	return nls_list;
+err:
+	nft_set_list_free(nls_list);
+	return NULL;
+}
+
+/*
+ * Set elements
+ */
+static int set_elem_cb(const struct nlmsghdr *nlh, void *data)
+{
+	nft_set_elems_nlmsg_parse(nlh, data);
+	return MNL_CB_OK;
+}
+
+static int mnl_setelem_get(struct mnl_socket *nf_sock, struct nft_set *nls)
+{
+	char buf[MNL_SOCKET_BUFFER_SIZE];
+	struct nlmsghdr *nlh;
+	uint32_t family = nft_set_attr_get_u32(nls, NFT_SET_ATTR_FAMILY);
+
+	nlh = nft_set_nlmsg_build_hdr(buf, NFT_MSG_GETSETELEM, family,
+				      NLM_F_DUMP|NLM_F_ACK, seq);
+	nft_set_nlmsg_build_payload(nlh, nls);
+
+	return mnl_talk(nf_sock, nlh, nlh->nlmsg_len, set_elem_cb, nls);
+}
+
+static int set_add_elems_cb(struct nft_set *t, void *data)
+{
+	mnl_setelem_get(data, t);
+	return 0;
+}
+
+/*
+ * ruleset
+ */
+
+static void
+nft_set_list_merge(struct nft_set_list *dest, struct nft_set_list *orig)
+{
+	struct nft_set_list_iter *it;
+	struct nft_set *o;
+
+	it = nft_set_list_iter_create(orig);
+	if (it == NULL)
+		memory_allocation_error();
+
+	o = nft_set_list_iter_next(it);
+	while (o != NULL) {
+		nft_set_list_add_tail(o, dest);
+		o = nft_set_list_iter_next(it);
+	}
+
+	nft_set_list_iter_destroy(it);
+}
+
+static struct nft_ruleset *mnl_ruleset_dump(struct mnl_socket *nf_sock)
+{
+	struct nft_ruleset *rs;
+	struct nft_rule_list *r;
+	struct nft_chain_list *c;
+	struct nft_set_list *complete_set_list = NULL, *s;
+	struct nft_table_list *t;
+	struct nft_table_list_iter *it;
+	struct nft_table *o;
+	const char *table;
+	uint16_t family;
+
+	t = mnl_table_dump(nf_sock, NFPROTO_UNSPEC);
+	if (t == NULL)
+		return NULL;
+
+	rs = nft_ruleset_alloc();
+	if (rs == NULL)
+		memory_allocation_error();
+
+	nft_ruleset_attr_set(rs, NFT_RULESET_ATTR_TABLELIST, t);
+
+	c = mnl_chain_dump(nf_sock, NFPROTO_UNSPEC);
+	if (c != NULL)
+		nft_ruleset_attr_set(rs, NFT_RULESET_ATTR_CHAINLIST, c);
+
+	r = mnl_rule_dump(nf_sock, NFPROTO_UNSPEC);
+	if (r != NULL)
+		nft_ruleset_attr_set(rs, NFT_RULESET_ATTR_RULELIST, r);
+
+	it = nft_table_list_iter_create(t);
+	if (it == NULL)
+		memory_allocation_error();
+
+	o = nft_table_list_iter_next(it);
+	while (o != NULL) {
+		table = nft_table_attr_get_str(o, NFT_TABLE_ATTR_NAME);
+		family = nft_table_attr_get_u32(o, NFT_TABLE_ATTR_FAMILY);
+
+		s = mnl_set_dump(nf_sock, family, table);
+		if (s != NULL) {
+			nft_set_list_foreach(s, set_add_elems_cb, nf_sock);
+
+			if (complete_set_list == NULL) {
+				complete_set_list = nft_set_list_alloc();
+				if (complete_set_list == NULL)
+					memory_allocation_error();
+			}
+
+			nft_set_list_merge(complete_set_list, s);
+		}
+		o = nft_table_list_iter_next(it);
+	}
+	nft_table_list_iter_destroy(it);
+
+	if (complete_set_list != NULL)
+		nft_ruleset_attr_set(rs, NFT_RULESET_ATTR_SETLIST,
+				     complete_set_list);
+
+	return rs;
+}
+
+int main(int argc, char *argv[])
+{
+	struct mnl_socket *nl;
+	uint32_t type = NFT_RULESET_O_DEFAULT;
+	struct nft_ruleset *rs;
+	size_t len = 1024000;
+	char *buf;
+
+	if (argc > 2) {
+		fprintf(stderr, "%s [xml|json]\n",
+			argv[0]);
+		return EXIT_FAILURE;
+	}
+
+	if (argc == 2) {
+		if (strcmp(argv[1], "xml") == 0)
+			type = NFT_RULESET_O_XML;
+		else if (strcmp(argv[1], "json") == 0)
+			type = NFT_RULESET_O_JSON;
+		else {
+			fprintf(stderr, "Unknown type: [xml|json]\n");
+			exit(EXIT_FAILURE);
+		}
+	}
+
+	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);
+	}
+
+	seq = time(NULL);
+
+	rs = mnl_ruleset_dump(nl);
+	if (rs == NULL) {
+		perror("ruleset_dump");
+		exit(EXIT_FAILURE);
+	}
+
+	buf = malloc(len);
+	nft_ruleset_snprintf(buf, len, rs, type, 0);
+	printf("%s\n", buf);
+	free(buf);
+
+	exit(EXIT_SUCCESS);
+}

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Netfitler Users]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux