summaryrefslogtreecommitdiffstats
path: root/src/obj
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/obj/counter.c131
-rw-r--r--src/obj/ct_expect.c200
-rw-r--r--src/obj/ct_helper.c154
-rw-r--r--src/obj/ct_timeout.c320
-rw-r--r--src/obj/limit.c172
-rw-r--r--src/obj/quota.c148
-rw-r--r--src/obj/secmark.c120
-rw-r--r--src/obj/synproxy.c147
-rw-r--r--src/obj/tunnel.c551
-rw-r--r--src/object.c562
10 files changed, 2505 insertions, 0 deletions
diff --git a/src/obj/counter.c b/src/obj/counter.c
new file mode 100644
index 0000000..ebf3e74
--- /dev/null
+++ b/src/obj/counter.c
@@ -0,0 +1,131 @@
+/*
+ * (C) 2012-2016 by Pablo Neira Ayuso <pablo@netfilter.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <inttypes.h>
+
+#include <linux/netfilter/nf_tables.h>
+
+#include <libmnl/libmnl.h>
+#include <libnftnl/object.h>
+
+#include "internal.h"
+#include "obj.h"
+
+static int
+nftnl_obj_counter_set(struct nftnl_obj *e, uint16_t type,
+ const void *data, uint32_t data_len)
+{
+ struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
+
+ switch(type) {
+ case NFTNL_OBJ_CTR_BYTES:
+ memcpy(&ctr->bytes, data, sizeof(ctr->bytes));
+ break;
+ case NFTNL_OBJ_CTR_PKTS:
+ memcpy(&ctr->pkts, data, sizeof(ctr->pkts));
+ break;
+ default:
+ return -1;
+ }
+ return 0;
+}
+
+static const void *
+nftnl_obj_counter_get(const struct nftnl_obj *e, uint16_t type,
+ uint32_t *data_len)
+{
+ struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
+
+ switch(type) {
+ case NFTNL_OBJ_CTR_BYTES:
+ *data_len = sizeof(ctr->bytes);
+ return &ctr->bytes;
+ case NFTNL_OBJ_CTR_PKTS:
+ *data_len = sizeof(ctr->pkts);
+ return &ctr->pkts;
+ }
+ return NULL;
+}
+
+static int nftnl_obj_counter_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ if (mnl_attr_type_valid(attr, NFTA_COUNTER_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch(type) {
+ case NFTA_COUNTER_BYTES:
+ case NFTA_COUNTER_PACKETS:
+ if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static void
+nftnl_obj_counter_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
+{
+ struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
+
+ if (e->flags & (1 << NFTNL_OBJ_CTR_BYTES))
+ mnl_attr_put_u64(nlh, NFTA_COUNTER_BYTES, htobe64(ctr->bytes));
+ if (e->flags & (1 << NFTNL_OBJ_CTR_PKTS))
+ mnl_attr_put_u64(nlh, NFTA_COUNTER_PACKETS, htobe64(ctr->pkts));
+}
+
+static int
+nftnl_obj_counter_parse(struct nftnl_obj *e, struct nlattr *attr)
+{
+ struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
+ struct nlattr *tb[NFTA_COUNTER_MAX+1] = {};
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_counter_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_COUNTER_BYTES]) {
+ ctr->bytes = be64toh(mnl_attr_get_u64(tb[NFTA_COUNTER_BYTES]));
+ e->flags |= (1 << NFTNL_OBJ_CTR_BYTES);
+ }
+ if (tb[NFTA_COUNTER_PACKETS]) {
+ ctr->pkts = be64toh(mnl_attr_get_u64(tb[NFTA_COUNTER_PACKETS]));
+ e->flags |= (1 << NFTNL_OBJ_CTR_PKTS);
+ }
+
+ return 0;
+}
+
+static int nftnl_obj_counter_snprintf(char *buf, size_t len, uint32_t flags,
+ const struct nftnl_obj *e)
+{
+ struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
+
+ return snprintf(buf, len, "pkts %"PRIu64" bytes %"PRIu64" ",
+ ctr->pkts, ctr->bytes);
+}
+
+struct obj_ops obj_ops_counter = {
+ .name = "counter",
+ .type = NFT_OBJECT_COUNTER,
+ .alloc_len = sizeof(struct nftnl_obj_counter),
+ .max_attr = NFTA_COUNTER_MAX,
+ .set = nftnl_obj_counter_set,
+ .get = nftnl_obj_counter_get,
+ .parse = nftnl_obj_counter_parse,
+ .build = nftnl_obj_counter_build,
+ .output = nftnl_obj_counter_snprintf,
+};
diff --git a/src/obj/ct_expect.c b/src/obj/ct_expect.c
new file mode 100644
index 0000000..810ba9a
--- /dev/null
+++ b/src/obj/ct_expect.c
@@ -0,0 +1,200 @@
+/*
+ * (C) 2019 by Stéphane Veyret <sveyret@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <arpa/inet.h>
+#include <errno.h>
+
+#include <libmnl/libmnl.h>
+
+#include "obj.h"
+
+static int nftnl_obj_ct_expect_set(struct nftnl_obj *e, uint16_t type,
+ const void *data, uint32_t data_len)
+{
+ struct nftnl_obj_ct_expect *exp = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_CT_EXPECT_L3PROTO:
+ memcpy(&exp->l3proto, data, sizeof(exp->l3proto));
+ break;
+ case NFTNL_OBJ_CT_EXPECT_L4PROTO:
+ memcpy(&exp->l4proto, data, sizeof(exp->l4proto));
+ break;
+ case NFTNL_OBJ_CT_EXPECT_DPORT:
+ memcpy(&exp->dport, data, sizeof(exp->dport));
+ break;
+ case NFTNL_OBJ_CT_EXPECT_TIMEOUT:
+ memcpy(&exp->timeout, data, sizeof(exp->timeout));
+ break;
+ case NFTNL_OBJ_CT_EXPECT_SIZE:
+ memcpy(&exp->size, data, sizeof(exp->size));
+ break;
+ default:
+ return -1;
+ }
+ return 0;
+}
+
+static const void *nftnl_obj_ct_expect_get(const struct nftnl_obj *e,
+ uint16_t type, uint32_t *data_len)
+{
+ struct nftnl_obj_ct_expect *exp = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_CT_EXPECT_L3PROTO:
+ *data_len = sizeof(exp->l3proto);
+ return &exp->l3proto;
+ case NFTNL_OBJ_CT_EXPECT_L4PROTO:
+ *data_len = sizeof(exp->l4proto);
+ return &exp->l4proto;
+ case NFTNL_OBJ_CT_EXPECT_DPORT:
+ *data_len = sizeof(exp->dport);
+ return &exp->dport;
+ case NFTNL_OBJ_CT_EXPECT_TIMEOUT:
+ *data_len = sizeof(exp->timeout);
+ return &exp->timeout;
+ case NFTNL_OBJ_CT_EXPECT_SIZE:
+ *data_len = sizeof(exp->size);
+ return &exp->size;
+ }
+ return NULL;
+}
+
+static int nftnl_obj_ct_expect_cb(const struct nlattr *attr, void *data)
+{
+ int type = mnl_attr_get_type(attr);
+ const struct nlattr **tb = data;
+
+ if (mnl_attr_type_valid(attr, NFTA_CT_EXPECT_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch (type) {
+ case NFTA_CT_EXPECT_L3PROTO:
+ if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
+ abi_breakage();
+ break;
+ case NFTA_CT_EXPECT_L4PROTO:
+ if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
+ abi_breakage();
+ break;
+ case NFTA_CT_EXPECT_DPORT:
+ if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
+ abi_breakage();
+ break;
+ case NFTA_CT_EXPECT_TIMEOUT:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ abi_breakage();
+ break;
+ case NFTA_CT_EXPECT_SIZE:
+ if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static void
+nftnl_obj_ct_expect_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
+{
+ struct nftnl_obj_ct_expect *exp = nftnl_obj_data(e);
+
+ if (e->flags & (1 << NFTNL_OBJ_CT_EXPECT_L3PROTO))
+ mnl_attr_put_u16(nlh, NFTA_CT_EXPECT_L3PROTO, htons(exp->l3proto));
+ if (e->flags & (1 << NFTNL_OBJ_CT_EXPECT_L4PROTO))
+ mnl_attr_put_u8(nlh, NFTA_CT_EXPECT_L4PROTO, exp->l4proto);
+ if (e->flags & (1 << NFTNL_OBJ_CT_EXPECT_DPORT))
+ mnl_attr_put_u16(nlh, NFTA_CT_EXPECT_DPORT, htons(exp->dport));
+ if (e->flags & (1 << NFTNL_OBJ_CT_EXPECT_TIMEOUT))
+ mnl_attr_put_u32(nlh, NFTA_CT_EXPECT_TIMEOUT, exp->timeout);
+ if (e->flags & (1 << NFTNL_OBJ_CT_EXPECT_SIZE))
+ mnl_attr_put_u8(nlh, NFTA_CT_EXPECT_SIZE, exp->size);
+}
+
+static int
+nftnl_obj_ct_expect_parse(struct nftnl_obj *e, struct nlattr *attr)
+{
+ struct nftnl_obj_ct_expect *exp = nftnl_obj_data(e);
+ struct nlattr *tb[NFTA_CT_EXPECT_MAX + 1] = {};
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_ct_expect_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_CT_EXPECT_L3PROTO]) {
+ exp->l3proto = ntohs(mnl_attr_get_u16(tb[NFTA_CT_EXPECT_L3PROTO]));
+ e->flags |= (1 << NFTNL_OBJ_CT_EXPECT_L3PROTO);
+ }
+ if (tb[NFTA_CT_EXPECT_L4PROTO]) {
+ exp->l4proto = mnl_attr_get_u8(tb[NFTA_CT_EXPECT_L4PROTO]);
+ e->flags |= (1 << NFTNL_OBJ_CT_EXPECT_L4PROTO);
+ }
+ if (tb[NFTA_CT_EXPECT_DPORT]) {
+ exp->dport = ntohs(mnl_attr_get_u16(tb[NFTA_CT_EXPECT_DPORT]));
+ e->flags |= (1 << NFTNL_OBJ_CT_EXPECT_DPORT);
+ }
+ if (tb[NFTA_CT_EXPECT_TIMEOUT]) {
+ exp->timeout = mnl_attr_get_u32(tb[NFTA_CT_EXPECT_TIMEOUT]);
+ e->flags |= (1 << NFTNL_OBJ_CT_EXPECT_TIMEOUT);
+ }
+ if (tb[NFTA_CT_EXPECT_SIZE]) {
+ exp->size = mnl_attr_get_u8(tb[NFTA_CT_EXPECT_SIZE]);
+ e->flags |= (1 << NFTNL_OBJ_CT_EXPECT_SIZE);
+ }
+
+ return 0;
+}
+
+static int nftnl_obj_ct_expect_snprintf(char *buf, size_t remain,
+ uint32_t flags,
+ const struct nftnl_obj *e)
+{
+ struct nftnl_obj_ct_expect *exp = nftnl_obj_data(e);
+ int ret = 0, offset = 0;
+
+ if (e->flags & (1 << NFTNL_OBJ_CT_EXPECT_L3PROTO)) {
+ ret = snprintf(buf + offset, remain,
+ "family %d ", exp->l3proto);
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ }
+ if (e->flags & (1 << NFTNL_OBJ_CT_EXPECT_L4PROTO)) {
+ ret = snprintf(buf + offset, remain,
+ "protocol %d ", exp->l4proto);
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ }
+ if (e->flags & (1 << NFTNL_OBJ_CT_EXPECT_DPORT)) {
+ ret = snprintf(buf + offset, remain,
+ "dport %d ", exp->dport);
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ }
+ if (e->flags & (1 << NFTNL_OBJ_CT_EXPECT_TIMEOUT)) {
+ ret = snprintf(buf + offset, remain,
+ "timeout %d ", exp->timeout);
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ }
+ if (e->flags & (1 << NFTNL_OBJ_CT_EXPECT_SIZE)) {
+ ret = snprintf(buf + offset, remain, "size %d ", exp->size);
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ }
+
+ buf[offset] = '\0';
+ return offset;
+}
+
+struct obj_ops obj_ops_ct_expect = {
+ .name = "ct_expect",
+ .type = NFT_OBJECT_CT_EXPECT,
+ .alloc_len = sizeof(struct nftnl_obj_ct_expect),
+ .max_attr = NFTA_CT_EXPECT_MAX,
+ .set = nftnl_obj_ct_expect_set,
+ .get = nftnl_obj_ct_expect_get,
+ .parse = nftnl_obj_ct_expect_parse,
+ .build = nftnl_obj_ct_expect_build,
+ .output = nftnl_obj_ct_expect_snprintf,
+};
diff --git a/src/obj/ct_helper.c b/src/obj/ct_helper.c
new file mode 100644
index 0000000..a31bd6f
--- /dev/null
+++ b/src/obj/ct_helper.c
@@ -0,0 +1,154 @@
+/*
+ * (C) 2017 Red Hat GmbH
+ * Author: Florian Westphal <fw@strlen.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <inttypes.h>
+
+#include <linux/netfilter/nf_tables.h>
+
+#include "internal.h"
+#include <libmnl/libmnl.h>
+#include <libnftnl/object.h>
+
+#include "obj.h"
+
+static int nftnl_obj_ct_helper_set(struct nftnl_obj *e, uint16_t type,
+ const void *data, uint32_t data_len)
+{
+ struct nftnl_obj_ct_helper *helper = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_CT_HELPER_NAME:
+ snprintf(helper->name, sizeof(helper->name), "%s", (const char *)data);
+ break;
+ case NFTNL_OBJ_CT_HELPER_L3PROTO:
+ memcpy(&helper->l3proto, data, sizeof(helper->l3proto));
+ break;
+ case NFTNL_OBJ_CT_HELPER_L4PROTO:
+ memcpy(&helper->l4proto, data, sizeof(helper->l4proto));
+ break;
+ default:
+ return -1;
+ }
+ return 0;
+}
+
+static const void *nftnl_obj_ct_helper_get(const struct nftnl_obj *e,
+ uint16_t type, uint32_t *data_len)
+{
+ struct nftnl_obj_ct_helper *helper = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_CT_HELPER_NAME:
+ *data_len = strlen(helper->name);
+ return helper->name;
+ case NFTNL_OBJ_CT_HELPER_L3PROTO:
+ *data_len = sizeof(helper->l3proto);
+ return &helper->l3proto;
+ case NFTNL_OBJ_CT_HELPER_L4PROTO:
+ *data_len = sizeof(helper->l4proto);
+ return &helper->l4proto;
+ }
+ return NULL;
+}
+
+static int nftnl_obj_ct_helper_cb(const struct nlattr *attr, void *data)
+{
+ const struct nftnl_obj_ct_helper *helper = NULL;
+ int type = mnl_attr_get_type(attr);
+ const struct nlattr **tb = data;
+
+ if (mnl_attr_type_valid(attr, NFTA_CT_HELPER_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch (type) {
+ case NFTA_CT_HELPER_NAME:
+ if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
+ abi_breakage();
+ if (mnl_attr_get_payload_len(attr) >= sizeof(helper->name))
+ abi_breakage();
+ break;
+ case NFTA_CT_HELPER_L3PROTO:
+ if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
+ abi_breakage();
+ break;
+ case NFTA_CT_HELPER_L4PROTO:
+ if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static void
+nftnl_obj_ct_helper_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
+{
+ struct nftnl_obj_ct_helper *helper = nftnl_obj_data(e);
+
+ if (e->flags & (1 << NFTNL_OBJ_CT_HELPER_NAME))
+ mnl_attr_put_str(nlh, NFTA_CT_HELPER_NAME, helper->name);
+ if (e->flags & (1 << NFTNL_OBJ_CT_HELPER_L3PROTO))
+ mnl_attr_put_u16(nlh, NFTA_CT_HELPER_L3PROTO, htons(helper->l3proto));
+ if (e->flags & (1 << NFTNL_OBJ_CT_HELPER_L4PROTO))
+ mnl_attr_put_u8(nlh, NFTA_CT_HELPER_L4PROTO, helper->l4proto);
+}
+
+static int
+nftnl_obj_ct_helper_parse(struct nftnl_obj *e, struct nlattr *attr)
+{
+ struct nftnl_obj_ct_helper *helper = nftnl_obj_data(e);
+ struct nlattr *tb[NFTA_CT_HELPER_MAX + 1] = {};
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_ct_helper_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_CT_HELPER_NAME]) {
+ snprintf(helper->name, sizeof(helper->name), "%s",
+ mnl_attr_get_str(tb[NFTA_CT_HELPER_NAME]));
+ e->flags |= (1 << NFTNL_OBJ_CT_HELPER_NAME);
+ }
+ if (tb[NFTA_CT_HELPER_L3PROTO]) {
+ helper->l3proto = ntohs(mnl_attr_get_u16(tb[NFTA_CT_HELPER_L3PROTO]));
+ e->flags |= (1 << NFTNL_OBJ_CT_HELPER_L3PROTO);
+ }
+ if (tb[NFTA_CT_HELPER_L4PROTO]) {
+ helper->l4proto = mnl_attr_get_u8(tb[NFTA_CT_HELPER_L4PROTO]);
+ e->flags |= (1 << NFTNL_OBJ_CT_HELPER_L4PROTO);
+ }
+
+ return 0;
+}
+
+static int nftnl_obj_ct_helper_snprintf(char *buf, size_t len,
+ uint32_t flags,
+ const struct nftnl_obj *e)
+{
+ struct nftnl_obj_ct_helper *helper = nftnl_obj_data(e);
+
+ return snprintf(buf, len, "name %s family %d protocol %d ",
+ helper->name, helper->l3proto, helper->l4proto);
+}
+
+struct obj_ops obj_ops_ct_helper = {
+ .name = "ct_helper",
+ .type = NFT_OBJECT_CT_HELPER,
+ .alloc_len = sizeof(struct nftnl_obj_ct_helper),
+ .max_attr = NFTA_CT_HELPER_MAX,
+ .set = nftnl_obj_ct_helper_set,
+ .get = nftnl_obj_ct_helper_get,
+ .parse = nftnl_obj_ct_helper_parse,
+ .build = nftnl_obj_ct_helper_build,
+ .output = nftnl_obj_ct_helper_snprintf,
+};
diff --git a/src/obj/ct_timeout.c b/src/obj/ct_timeout.c
new file mode 100644
index 0000000..65b48bd
--- /dev/null
+++ b/src/obj/ct_timeout.c
@@ -0,0 +1,320 @@
+/*
+ * (C) 2018 by Harsha Sharma <harshasharmaiitr@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <inttypes.h>
+
+#include <linux/netfilter/nf_tables.h>
+
+#include "internal.h"
+#include <libmnl/libmnl.h>
+#include <libnftnl/object.h>
+
+#include "obj.h"
+
+static const char *const tcp_state_to_name[] = {
+ [NFTNL_CTTIMEOUT_TCP_SYN_SENT] = "SYN_SENT",
+ [NFTNL_CTTIMEOUT_TCP_SYN_RECV] = "SYN_RECV",
+ [NFTNL_CTTIMEOUT_TCP_ESTABLISHED] = "ESTABLISHED",
+ [NFTNL_CTTIMEOUT_TCP_FIN_WAIT] = "FIN_WAIT",
+ [NFTNL_CTTIMEOUT_TCP_CLOSE_WAIT] = "CLOSE_WAIT",
+ [NFTNL_CTTIMEOUT_TCP_LAST_ACK] = "LAST_ACK",
+ [NFTNL_CTTIMEOUT_TCP_TIME_WAIT] = "TIME_WAIT",
+ [NFTNL_CTTIMEOUT_TCP_CLOSE] = "CLOSE",
+ [NFTNL_CTTIMEOUT_TCP_SYN_SENT2] = "SYN_SENT2",
+ [NFTNL_CTTIMEOUT_TCP_RETRANS] = "RETRANS",
+ [NFTNL_CTTIMEOUT_TCP_UNACK] = "UNACKNOWLEDGED",
+};
+
+static uint32_t tcp_dflt_timeout[] = {
+ [NFTNL_CTTIMEOUT_TCP_SYN_SENT] = 120,
+ [NFTNL_CTTIMEOUT_TCP_SYN_RECV] = 60,
+ [NFTNL_CTTIMEOUT_TCP_ESTABLISHED] = 432000,
+ [NFTNL_CTTIMEOUT_TCP_FIN_WAIT] = 120,
+ [NFTNL_CTTIMEOUT_TCP_CLOSE_WAIT] = 60,
+ [NFTNL_CTTIMEOUT_TCP_LAST_ACK] = 30,
+ [NFTNL_CTTIMEOUT_TCP_TIME_WAIT] = 120,
+ [NFTNL_CTTIMEOUT_TCP_CLOSE] = 10,
+ [NFTNL_CTTIMEOUT_TCP_SYN_SENT2] = 120,
+ [NFTNL_CTTIMEOUT_TCP_RETRANS] = 300,
+ [NFTNL_CTTIMEOUT_TCP_UNACK] = 300,
+};
+
+static const char *const udp_state_to_name[] = {
+ [NFTNL_CTTIMEOUT_UDP_UNREPLIED] = "UNREPLIED",
+ [NFTNL_CTTIMEOUT_UDP_REPLIED] = "REPLIED",
+};
+
+static uint32_t udp_dflt_timeout[] = {
+ [NFTNL_CTTIMEOUT_UDP_UNREPLIED] = 30,
+ [NFTNL_CTTIMEOUT_UDP_REPLIED] = 180,
+};
+
+static struct {
+ uint32_t attr_max;
+ const char *const *state_to_name;
+ uint32_t *dflt_timeout;
+} timeout_protocol[IPPROTO_MAX] = {
+ [IPPROTO_TCP] = {
+ .attr_max = NFTNL_CTTIMEOUT_TCP_MAX,
+ .state_to_name = tcp_state_to_name,
+ .dflt_timeout = tcp_dflt_timeout,
+ },
+ [IPPROTO_UDP] = {
+ .attr_max = NFTNL_CTTIMEOUT_UDP_MAX,
+ .state_to_name = udp_state_to_name,
+ .dflt_timeout = udp_dflt_timeout,
+ },
+};
+
+struct _container_policy_cb {
+ unsigned int nlattr_max;
+ void *tb;
+};
+
+static int
+nftnl_timeout_policy_attr_set_u32(struct nftnl_obj *e,
+ uint32_t type, uint32_t data)
+{
+ struct nftnl_obj_ct_timeout *t = nftnl_obj_data(e);
+
+ if (type >= NFTNL_CTTIMEOUT_ARRAY_MAX)
+ return -1;
+
+ t->timeout[type] = data;
+
+ if (!(e->flags & (1 << NFTNL_OBJ_CT_TIMEOUT_ARRAY)))
+ e->flags |= (1 << NFTNL_OBJ_CT_TIMEOUT_ARRAY);
+
+ return 0;
+}
+
+static int
+parse_timeout_attr_policy_cb(const struct nlattr *attr, void *data)
+{
+ struct _container_policy_cb *data_cb = data;
+ const struct nlattr **tb = data_cb->tb;
+ uint16_t type = mnl_attr_get_type(attr);
+
+ if (mnl_attr_type_valid(attr, data_cb->nlattr_max) < 0)
+ return MNL_CB_OK;
+
+ if (type > 0 && type <= data_cb->nlattr_max) {
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ abi_breakage();
+ tb[type - 1] = attr;
+ }
+ return MNL_CB_OK;
+}
+
+static int
+timeout_parse_attr_data(struct nftnl_obj *e,
+ const struct nlattr *nest)
+{
+ struct nftnl_obj_ct_timeout *t = nftnl_obj_data(e);
+ unsigned int attr_max = timeout_protocol[t->l4proto].attr_max;
+ struct nlattr *tb[attr_max];
+ struct _container_policy_cb cnt = {
+ .nlattr_max = attr_max,
+ .tb = tb,
+ };
+ unsigned int i;
+
+ memset(tb, 0, sizeof(struct nlattr *) * attr_max);
+
+ if (mnl_attr_parse_nested(nest, parse_timeout_attr_policy_cb, &cnt) < 0)
+ return -1;
+
+ for (i = 0; i < array_size(tb); i++) {
+ if (tb[i]) {
+ nftnl_timeout_policy_attr_set_u32(e, i,
+ ntohl(mnl_attr_get_u32(tb[i])));
+ }
+ }
+ return 0;
+}
+
+static int nftnl_obj_ct_timeout_set(struct nftnl_obj *e, uint16_t type,
+ const void *data, uint32_t data_len)
+{
+ struct nftnl_obj_ct_timeout *timeout = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_CT_TIMEOUT_L3PROTO:
+ memcpy(&timeout->l3proto, data, sizeof(timeout->l3proto));
+ break;
+ case NFTNL_OBJ_CT_TIMEOUT_L4PROTO:
+ memcpy(&timeout->l4proto, data, sizeof(timeout->l4proto));
+ break;
+ case NFTNL_OBJ_CT_TIMEOUT_ARRAY:
+ memcpy(timeout->timeout, data,
+ sizeof(uint32_t) * NFTNL_CTTIMEOUT_ARRAY_MAX);
+ break;
+ default:
+ return -1;
+ }
+ return 0;
+}
+
+static const void *nftnl_obj_ct_timeout_get(const struct nftnl_obj *e,
+ uint16_t type, uint32_t *data_len)
+{
+ struct nftnl_obj_ct_timeout *timeout = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_CT_TIMEOUT_L3PROTO:
+ *data_len = sizeof(timeout->l3proto);
+ return &timeout->l3proto;
+ case NFTNL_OBJ_CT_TIMEOUT_L4PROTO:
+ *data_len = sizeof(timeout->l4proto);
+ return &timeout->l4proto;
+ case NFTNL_OBJ_CT_TIMEOUT_ARRAY:
+ *data_len = sizeof(uint32_t) * NFTNL_CTTIMEOUT_ARRAY_MAX;
+ return timeout->timeout;
+ }
+ return NULL;
+}
+
+static int nftnl_obj_ct_timeout_cb(const struct nlattr *attr, void *data)
+{
+ int type = mnl_attr_get_type(attr);
+ const struct nlattr **tb = data;
+
+ if (mnl_attr_type_valid(attr, NFTA_CT_TIMEOUT_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch (type) {
+ case NFTA_CT_TIMEOUT_L3PROTO:
+ if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
+ abi_breakage();
+ break;
+ case NFTA_CT_TIMEOUT_L4PROTO:
+ if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
+ abi_breakage();
+ break;
+ case NFTA_CT_TIMEOUT_DATA:
+ if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static void
+nftnl_obj_ct_timeout_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
+{
+ struct nftnl_obj_ct_timeout *timeout = nftnl_obj_data(e);
+ struct nlattr *nest;
+
+ if (e->flags & (1 << NFTNL_OBJ_CT_TIMEOUT_L3PROTO))
+ mnl_attr_put_u16(nlh, NFTA_CT_TIMEOUT_L3PROTO, htons(timeout->l3proto));
+ if (e->flags & (1 << NFTNL_OBJ_CT_TIMEOUT_L4PROTO))
+ mnl_attr_put_u8(nlh, NFTA_CT_TIMEOUT_L4PROTO, timeout->l4proto);
+ if (e->flags & (1 << NFTNL_OBJ_CT_TIMEOUT_ARRAY)) {
+ int i;
+
+ nest = mnl_attr_nest_start(nlh, NFTA_CT_TIMEOUT_DATA);
+ for (i = 0; i < timeout_protocol[timeout->l4proto].attr_max; i++)
+ mnl_attr_put_u32(nlh, i+1, htonl(timeout->timeout[i]));
+
+ mnl_attr_nest_end(nlh, nest);
+ }
+}
+
+static int
+nftnl_obj_ct_timeout_parse(struct nftnl_obj *e, struct nlattr *attr)
+{
+ struct nftnl_obj_ct_timeout *timeout = nftnl_obj_data(e);
+ struct nlattr *tb[NFTA_CT_TIMEOUT_MAX + 1] = {};
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_ct_timeout_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_CT_TIMEOUT_L3PROTO]) {
+ timeout->l3proto = ntohs(mnl_attr_get_u16(tb[NFTA_CT_TIMEOUT_L3PROTO]));
+ e->flags |= (1 << NFTNL_OBJ_CT_TIMEOUT_L3PROTO);
+ }
+ if (tb[NFTA_CT_TIMEOUT_L4PROTO]) {
+ timeout->l4proto = mnl_attr_get_u8(tb[NFTA_CT_TIMEOUT_L4PROTO]);
+ e->flags |= (1 << NFTNL_OBJ_CT_TIMEOUT_L4PROTO);
+ }
+ if (tb[NFTA_CT_TIMEOUT_DATA]) {
+ if (timeout_parse_attr_data(e, tb[NFTA_CT_TIMEOUT_DATA]) < 0)
+ return -1;
+ e->flags |= (1 << NFTNL_OBJ_CT_TIMEOUT_ARRAY);
+ }
+ return 0;
+}
+
+static int nftnl_obj_ct_timeout_snprintf(char *buf, size_t remain,
+ uint32_t flags,
+ const struct nftnl_obj *e)
+{
+ int ret = 0, offset = 0;
+
+ struct nftnl_obj_ct_timeout *timeout = nftnl_obj_data(e);
+
+ if (e->flags & (1 << NFTNL_OBJ_CT_TIMEOUT_L3PROTO)) {
+ ret = snprintf(buf + offset, remain, "family %d ",
+ timeout->l3proto);
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ }
+ if (e->flags & (1 << NFTNL_OBJ_CT_TIMEOUT_L4PROTO)) {
+ ret = snprintf(buf + offset, remain, "protocol %d ",
+ timeout->l4proto);
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ }
+ if (e->flags & (1 << NFTNL_OBJ_CT_TIMEOUT_ARRAY)) {
+ uint8_t l4num = timeout->l4proto;
+ int i;
+
+ /* default to generic protocol tracker. */
+ if (timeout_protocol[timeout->l4proto].attr_max == 0)
+ l4num = IPPROTO_RAW;
+
+ ret = snprintf(buf + offset, remain, "policy = {");
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+
+ for (i = 0; i < timeout_protocol[l4num].attr_max; i++) {
+ const char *state_name =
+ timeout_protocol[l4num].state_to_name[i][0] ?
+ timeout_protocol[l4num].state_to_name[i] :
+ "UNKNOWN";
+
+ if (timeout->timeout[i] != timeout_protocol[l4num].dflt_timeout[i]) {
+ ret = snprintf(buf + offset, remain,
+ "%s = %u,", state_name, timeout->timeout[i]);
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ }
+ }
+
+ ret = snprintf(buf + offset, remain, "}");
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ }
+ buf[offset] = '\0';
+
+ return offset;
+}
+
+struct obj_ops obj_ops_ct_timeout = {
+ .name = "ct_timeout",
+ .type = NFT_OBJECT_CT_TIMEOUT,
+ .alloc_len = sizeof(struct nftnl_obj_ct_timeout),
+ .max_attr = NFTA_CT_TIMEOUT_MAX,
+ .set = nftnl_obj_ct_timeout_set,
+ .get = nftnl_obj_ct_timeout_get,
+ .parse = nftnl_obj_ct_timeout_parse,
+ .build = nftnl_obj_ct_timeout_build,
+ .output = nftnl_obj_ct_timeout_snprintf,
+};
diff --git a/src/obj/limit.c b/src/obj/limit.c
new file mode 100644
index 0000000..d7b1aed
--- /dev/null
+++ b/src/obj/limit.c
@@ -0,0 +1,172 @@
+/*
+ * Copyright (c) 2017 Pablo M. Bermudo Garay <pablombg@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <inttypes.h>
+
+#include <linux/netfilter/nf_tables.h>
+
+#include "internal.h"
+#include <libmnl/libmnl.h>
+#include <libnftnl/object.h>
+
+#include "obj.h"
+
+static int nftnl_obj_limit_set(struct nftnl_obj *e, uint16_t type,
+ const void *data, uint32_t data_len)
+{
+ struct nftnl_obj_limit *limit = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_LIMIT_RATE:
+ memcpy(&limit->rate, data, sizeof(limit->rate));
+ break;
+ case NFTNL_OBJ_LIMIT_UNIT:
+ memcpy(&limit->unit, data, sizeof(limit->unit));
+ break;
+ case NFTNL_OBJ_LIMIT_BURST:
+ memcpy(&limit->burst, data, sizeof(limit->burst));
+ break;
+ case NFTNL_OBJ_LIMIT_TYPE:
+ memcpy(&limit->type, data, sizeof(limit->type));
+ break;
+ case NFTNL_OBJ_LIMIT_FLAGS:
+ memcpy(&limit->flags, data, sizeof(limit->flags));
+ break;
+ default:
+ return -1;
+ }
+ return 0;
+}
+
+static const void *nftnl_obj_limit_get(const struct nftnl_obj *e,
+ uint16_t type, uint32_t *data_len)
+{
+ struct nftnl_obj_limit *limit = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_LIMIT_RATE:
+ *data_len = sizeof(limit->rate);
+ return &limit->rate;
+ case NFTNL_OBJ_LIMIT_UNIT:
+ *data_len = sizeof(limit->unit);
+ return &limit->unit;
+ case NFTNL_OBJ_LIMIT_BURST:
+ *data_len = sizeof(limit->burst);
+ return &limit->burst;
+ case NFTNL_OBJ_LIMIT_TYPE:
+ *data_len = sizeof(limit->type);
+ return &limit->type;
+ case NFTNL_OBJ_LIMIT_FLAGS:
+ *data_len = sizeof(limit->flags);
+ return &limit->flags;
+ }
+ return NULL;
+}
+
+static int nftnl_obj_limit_cb(const struct nlattr *attr, void *data)
+{
+ int type = mnl_attr_get_type(attr);
+ const struct nlattr **tb = data;
+
+ if (mnl_attr_type_valid(attr, NFTA_LIMIT_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch (type) {
+ case NFTA_LIMIT_RATE:
+ case NFTA_LIMIT_UNIT:
+ if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
+ abi_breakage();
+ break;
+ case NFTA_LIMIT_BURST:
+ case NFTA_LIMIT_TYPE:
+ case NFTA_LIMIT_FLAGS:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static void nftnl_obj_limit_build(struct nlmsghdr *nlh,
+ const struct nftnl_obj *e)
+{
+ struct nftnl_obj_limit *limit = nftnl_obj_data(e);
+
+ if (e->flags & (1 << NFTNL_OBJ_LIMIT_RATE))
+ mnl_attr_put_u64(nlh, NFTA_LIMIT_RATE, htobe64(limit->rate));
+ if (e->flags & (1 << NFTNL_OBJ_LIMIT_UNIT))
+ mnl_attr_put_u64(nlh, NFTA_LIMIT_UNIT, htobe64(limit->unit));
+ if (e->flags & (1 << NFTNL_OBJ_LIMIT_BURST))
+ mnl_attr_put_u32(nlh, NFTA_LIMIT_BURST, htonl(limit->burst));
+ if (e->flags & (1 << NFTNL_OBJ_LIMIT_TYPE))
+ mnl_attr_put_u32(nlh, NFTA_LIMIT_TYPE, htonl(limit->type));
+ if (e->flags & (1 << NFTNL_OBJ_LIMIT_FLAGS))
+ mnl_attr_put_u32(nlh, NFTA_LIMIT_FLAGS, htonl(limit->flags));
+}
+
+static int nftnl_obj_limit_parse(struct nftnl_obj *e, struct nlattr *attr)
+{
+ struct nftnl_obj_limit *limit = nftnl_obj_data(e);
+ struct nlattr *tb[NFTA_LIMIT_MAX + 1] = {};
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_limit_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_LIMIT_RATE]) {
+ limit->rate = be64toh(mnl_attr_get_u64(tb[NFTA_LIMIT_RATE]));
+ e->flags |= (1 << NFTNL_OBJ_LIMIT_RATE);
+ }
+ if (tb[NFTA_LIMIT_UNIT]) {
+ limit->unit = be64toh(mnl_attr_get_u64(tb[NFTA_LIMIT_UNIT]));
+ e->flags |= (1 << NFTNL_OBJ_LIMIT_UNIT);
+ }
+ if (tb[NFTA_LIMIT_BURST]) {
+ limit->burst = ntohl(mnl_attr_get_u32(tb[NFTA_LIMIT_BURST]));
+ e->flags |= (1 << NFTNL_OBJ_LIMIT_BURST);
+ }
+ if (tb[NFTA_LIMIT_TYPE]) {
+ limit->type = ntohl(mnl_attr_get_u32(tb[NFTA_LIMIT_TYPE]));
+ e->flags |= (1 << NFTNL_OBJ_LIMIT_TYPE);
+ }
+ if (tb[NFTA_LIMIT_FLAGS]) {
+ limit->flags = ntohl(mnl_attr_get_u32(tb[NFTA_LIMIT_FLAGS]));
+ e->flags |= (1 << NFTNL_OBJ_LIMIT_FLAGS);
+ }
+
+ return 0;
+}
+
+static int nftnl_obj_limit_snprintf(char *buf, size_t len,
+ uint32_t flags,
+ const struct nftnl_obj *e)
+{
+ struct nftnl_obj_limit *limit = nftnl_obj_data(e);
+
+ return snprintf(buf, len, "rate %"PRIu64" unit %"PRIu64" burst %u "
+ "type %u flags %u ", limit->rate, limit->unit,
+ limit->burst, limit->type, limit->flags);
+}
+
+struct obj_ops obj_ops_limit = {
+ .name = "limit",
+ .type = NFT_OBJECT_LIMIT,
+ .alloc_len = sizeof(struct nftnl_obj_limit),
+ .max_attr = NFTA_LIMIT_MAX,
+ .set = nftnl_obj_limit_set,
+ .get = nftnl_obj_limit_get,
+ .parse = nftnl_obj_limit_parse,
+ .build = nftnl_obj_limit_build,
+ .output = nftnl_obj_limit_snprintf,
+};
diff --git a/src/obj/quota.c b/src/obj/quota.c
new file mode 100644
index 0000000..6c7559a
--- /dev/null
+++ b/src/obj/quota.c
@@ -0,0 +1,148 @@
+/*
+ * (C) 2012-2016 by Pablo Neira Ayuso <pablo@netfilter.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <inttypes.h>
+
+#include <linux/netfilter/nf_tables.h>
+
+#include "internal.h"
+#include <libmnl/libmnl.h>
+#include <libnftnl/object.h>
+
+#include "obj.h"
+
+static int nftnl_obj_quota_set(struct nftnl_obj *e, uint16_t type,
+ const void *data, uint32_t data_len)
+{
+ struct nftnl_obj_quota *quota = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_QUOTA_BYTES:
+ memcpy(&quota->bytes, data, sizeof(quota->bytes));
+ break;
+ case NFTNL_OBJ_QUOTA_CONSUMED:
+ memcpy(&quota->consumed, data, sizeof(quota->consumed));
+ break;
+ case NFTNL_OBJ_QUOTA_FLAGS:
+ memcpy(&quota->flags, data, sizeof(quota->flags));
+ break;
+ default:
+ return -1;
+ }
+ return 0;
+}
+
+static const void *nftnl_obj_quota_get(const struct nftnl_obj *e,
+ uint16_t type, uint32_t *data_len)
+{
+ struct nftnl_obj_quota *quota = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_QUOTA_BYTES:
+ *data_len = sizeof(quota->bytes);
+ return &quota->bytes;
+ case NFTNL_OBJ_QUOTA_CONSUMED:
+ *data_len = sizeof(quota->consumed);
+ return &quota->consumed;
+ case NFTNL_OBJ_QUOTA_FLAGS:
+ *data_len = sizeof(quota->flags);
+ return &quota->flags;
+ }
+ return NULL;
+}
+
+static int nftnl_obj_quota_cb(const struct nlattr *attr, void *data)
+{
+ int type = mnl_attr_get_type(attr);
+ const struct nlattr **tb = data;
+
+ if (mnl_attr_type_valid(attr, NFTA_QUOTA_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch(type) {
+ case NFTA_QUOTA_BYTES:
+ case NFTA_QUOTA_CONSUMED:
+ if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
+ abi_breakage();
+ break;
+ case NFTA_QUOTA_FLAGS:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static void
+nftnl_obj_quota_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
+{
+ struct nftnl_obj_quota *quota = nftnl_obj_data(e);
+
+ if (e->flags & (1 << NFTNL_OBJ_QUOTA_BYTES))
+ mnl_attr_put_u64(nlh, NFTA_QUOTA_BYTES, htobe64(quota->bytes));
+ if (e->flags & (1 << NFTNL_OBJ_QUOTA_CONSUMED))
+ mnl_attr_put_u64(nlh, NFTA_QUOTA_CONSUMED,
+ htobe64(quota->consumed));
+ if (e->flags & (1 << NFTNL_OBJ_QUOTA_FLAGS))
+ mnl_attr_put_u32(nlh, NFTA_QUOTA_FLAGS, htonl(quota->flags));
+}
+
+static int
+nftnl_obj_quota_parse(struct nftnl_obj *e, struct nlattr *attr)
+{
+ struct nftnl_obj_quota *quota = nftnl_obj_data(e);
+ struct nlattr *tb[NFTA_QUOTA_MAX + 1] = {};
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_quota_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_QUOTA_BYTES]) {
+ quota->bytes = be64toh(mnl_attr_get_u64(tb[NFTA_QUOTA_BYTES]));
+ e->flags |= (1 << NFTNL_OBJ_QUOTA_BYTES);
+ }
+ if (tb[NFTA_QUOTA_CONSUMED]) {
+ quota->consumed =
+ be64toh(mnl_attr_get_u64(tb[NFTA_QUOTA_CONSUMED]));
+ e->flags |= (1 << NFTNL_OBJ_QUOTA_CONSUMED);
+ }
+ if (tb[NFTA_QUOTA_FLAGS]) {
+ quota->flags = ntohl(mnl_attr_get_u32(tb[NFTA_QUOTA_FLAGS]));
+ e->flags |= (1 << NFTNL_OBJ_QUOTA_FLAGS);
+ }
+
+ return 0;
+}
+
+static int nftnl_obj_quota_snprintf(char *buf, size_t len,
+ uint32_t flags,
+ const struct nftnl_obj *e)
+{
+ struct nftnl_obj_quota *quota = nftnl_obj_data(e);
+
+ return snprintf(buf, len, "bytes %"PRIu64" flags %u ",
+ quota->bytes, quota->flags);
+}
+
+struct obj_ops obj_ops_quota = {
+ .name = "quota",
+ .type = NFT_OBJECT_QUOTA,
+ .alloc_len = sizeof(struct nftnl_obj_quota),
+ .max_attr = NFTA_QUOTA_MAX,
+ .set = nftnl_obj_quota_set,
+ .get = nftnl_obj_quota_get,
+ .parse = nftnl_obj_quota_parse,
+ .build = nftnl_obj_quota_build,
+ .output = nftnl_obj_quota_snprintf,
+};
diff --git a/src/obj/secmark.c b/src/obj/secmark.c
new file mode 100644
index 0000000..e5c24b3
--- /dev/null
+++ b/src/obj/secmark.c
@@ -0,0 +1,120 @@
+/*
+ * (C) 2012-2016 by Pablo Neira Ayuso <pablo@netfilter.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <inttypes.h>
+
+#include <linux/netfilter/nf_tables.h>
+
+#include "internal.h"
+#include <libmnl/libmnl.h>
+#include <libnftnl/object.h>
+
+#include "obj.h"
+
+static int nftnl_obj_secmark_set(struct nftnl_obj *e, uint16_t type,
+ const void *data, uint32_t data_len)
+{
+ struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_SECMARK_CTX:
+ snprintf(secmark->ctx, sizeof(secmark->ctx), "%s", (const char *)data);
+ break;
+ default:
+ return -1;
+ }
+ return 0;
+}
+
+static const void *nftnl_obj_secmark_get(const struct nftnl_obj *e,
+ uint16_t type, uint32_t *data_len)
+{
+ struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_SECMARK_CTX:
+ *data_len = strlen(secmark->ctx);
+ return secmark->ctx;
+ }
+ return NULL;
+}
+
+static int nftnl_obj_secmark_cb(const struct nlattr *attr, void *data)
+{
+ const struct nftnl_obj_secmark *secmark = NULL;
+ int type = mnl_attr_get_type(attr);
+ const struct nlattr **tb = data;
+
+ if (mnl_attr_type_valid(attr, NFTA_SECMARK_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch(type) {
+ case NFTA_SECMARK_CTX:
+ if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
+ abi_breakage();
+ if (mnl_attr_get_payload_len(attr) >= sizeof(secmark->ctx))
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static void
+nftnl_obj_secmark_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
+{
+ struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
+
+ if (e->flags & (1 << NFTNL_OBJ_SECMARK_CTX))
+ mnl_attr_put_str(nlh, NFTA_SECMARK_CTX, secmark->ctx);
+}
+
+static int
+nftnl_obj_secmark_parse(struct nftnl_obj *e, struct nlattr *attr)
+{
+ struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
+ struct nlattr *tb[NFTA_SECMARK_MAX + 1] = {};
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_secmark_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_SECMARK_CTX]) {
+ snprintf(secmark->ctx, sizeof(secmark->ctx), "%s",
+ mnl_attr_get_str(tb[NFTA_SECMARK_CTX]));
+ e->flags |= (1 << NFTNL_OBJ_SECMARK_CTX);
+ }
+
+ return 0;
+}
+
+static int nftnl_obj_secmark_snprintf(char *buf, size_t len,
+ uint32_t flags,
+ const struct nftnl_obj *e)
+{
+ struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
+
+ return snprintf(buf, len, "context %s ", secmark->ctx);
+}
+
+struct obj_ops obj_ops_secmark = {
+ .name = "secmark",
+ .type = NFT_OBJECT_SECMARK,
+ .alloc_len = sizeof(struct nftnl_obj_secmark),
+ .max_attr = NFTA_SECMARK_MAX,
+ .set = nftnl_obj_secmark_set,
+ .get = nftnl_obj_secmark_get,
+ .parse = nftnl_obj_secmark_parse,
+ .build = nftnl_obj_secmark_build,
+ .output = nftnl_obj_secmark_snprintf,
+};
diff --git a/src/obj/synproxy.c b/src/obj/synproxy.c
new file mode 100644
index 0000000..baef5c2
--- /dev/null
+++ b/src/obj/synproxy.c
@@ -0,0 +1,147 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <inttypes.h>
+
+#include <linux/netfilter/nf_tables.h>
+
+#include "internal.h"
+#include <libmnl/libmnl.h>
+#include <libnftnl/object.h>
+
+#include "obj.h"
+
+static int nftnl_obj_synproxy_set(struct nftnl_obj *e, uint16_t type,
+ const void *data, uint32_t data_len)
+{
+ struct nftnl_obj_synproxy *synproxy = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_SYNPROXY_MSS:
+ synproxy->mss = *((uint16_t *)data);
+ break;
+ case NFTNL_OBJ_SYNPROXY_WSCALE:
+ synproxy->wscale = *((uint8_t *)data);
+ break;
+ case NFTNL_OBJ_SYNPROXY_FLAGS:
+ synproxy->flags = *((uint32_t *)data);
+ break;
+ default:
+ return -1;
+ }
+ return 0;
+}
+
+static const void *nftnl_obj_synproxy_get(const struct nftnl_obj *e,
+ uint16_t type, uint32_t *data_len)
+{
+ struct nftnl_obj_synproxy *synproxy = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_SYNPROXY_MSS:
+ *data_len = sizeof(synproxy->mss);
+ return &synproxy->mss;
+ case NFTNL_OBJ_SYNPROXY_WSCALE:
+ *data_len = sizeof(synproxy->wscale);
+ return &synproxy->wscale;
+ case NFTNL_OBJ_SYNPROXY_FLAGS:
+ *data_len = sizeof(synproxy->flags);
+ return &synproxy->flags;
+ }
+ return NULL;
+}
+
+static int nftnl_obj_synproxy_cb(const struct nlattr *attr, void *data)
+{
+ int type = mnl_attr_get_type(attr);
+ const struct nlattr **tb = data;
+
+ if (mnl_attr_type_valid(attr, NFTA_SYNPROXY_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch (type) {
+ case NFTA_SYNPROXY_MSS:
+ if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
+ abi_breakage();
+ break;
+ case NFTA_SYNPROXY_WSCALE:
+ if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
+ abi_breakage();
+ break;
+ case NFTA_SYNPROXY_FLAGS:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static void nftnl_obj_synproxy_build(struct nlmsghdr *nlh,
+ const struct nftnl_obj *e)
+{
+ struct nftnl_obj_synproxy *synproxy = nftnl_obj_data(e);
+
+ if (e->flags & (1 << NFTNL_OBJ_SYNPROXY_MSS))
+ mnl_attr_put_u16(nlh, NFTA_SYNPROXY_MSS, htons(synproxy->mss));
+ if (e->flags & (1 << NFTNL_OBJ_SYNPROXY_WSCALE))
+ mnl_attr_put_u8(nlh, NFTA_SYNPROXY_WSCALE, synproxy->wscale);
+ if (e->flags & (1 << NFTNL_OBJ_SYNPROXY_FLAGS))
+ mnl_attr_put_u32(nlh, NFTA_SYNPROXY_FLAGS,
+ htonl(synproxy->flags));
+}
+
+static int nftnl_obj_synproxy_parse(struct nftnl_obj *e, struct nlattr *attr)
+{
+ struct nftnl_obj_synproxy *synproxy = nftnl_obj_data(e);
+ struct nlattr *tb[NFTA_SYNPROXY_MAX + 1] = {};
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_synproxy_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_SYNPROXY_MSS]) {
+ synproxy->mss = ntohs(mnl_attr_get_u16(tb[NFTA_SYNPROXY_MSS]));
+ e->flags |= (1 << NFTNL_OBJ_SYNPROXY_MSS);
+ }
+ if (tb[NFTA_SYNPROXY_WSCALE]) {
+ synproxy->wscale = mnl_attr_get_u8(tb[NFTA_SYNPROXY_WSCALE]);
+ e->flags |= (1 << NFTNL_OBJ_SYNPROXY_WSCALE);
+ }
+ if (tb[NFTA_SYNPROXY_FLAGS]) {
+ synproxy->flags = ntohl(mnl_attr_get_u32(tb[NFTA_SYNPROXY_FLAGS]));
+ e->flags |= (1 << NFTNL_OBJ_SYNPROXY_FLAGS);
+ }
+
+ return 0;
+}
+
+static int nftnl_obj_synproxy_snprintf(char *buf, size_t len,
+ uint32_t flags,
+ const struct nftnl_obj *e)
+{
+ struct nftnl_obj_synproxy *synproxy = nftnl_obj_data(e);
+ int ret, offset = 0;
+
+ if (e->flags & (1 << NFTNL_OBJ_SYNPROXY_MSS) &&
+ e->flags & (1 << NFTNL_OBJ_SYNPROXY_WSCALE)) {
+ ret = snprintf(buf, len, "mss %u wscale %u ", synproxy->mss,
+ synproxy->wscale);
+ SNPRINTF_BUFFER_SIZE(ret, len, offset);
+ }
+
+ return offset;
+}
+
+struct obj_ops obj_ops_synproxy = {
+ .name = "synproxy",
+ .type = NFT_OBJECT_SYNPROXY,
+ .alloc_len = sizeof(struct nftnl_obj_synproxy),
+ .max_attr = NFTA_SYNPROXY_MAX,
+ .set = nftnl_obj_synproxy_set,
+ .get = nftnl_obj_synproxy_get,
+ .parse = nftnl_obj_synproxy_parse,
+ .build = nftnl_obj_synproxy_build,
+ .output = nftnl_obj_synproxy_snprintf,
+};
diff --git a/src/obj/tunnel.c b/src/obj/tunnel.c
new file mode 100644
index 0000000..d2503dc
--- /dev/null
+++ b/src/obj/tunnel.c
@@ -0,0 +1,551 @@
+/*
+ * (C) 2018 by Pablo Neira Ayuso <pablo@netfilter.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <inttypes.h>
+
+#include <linux/netfilter/nf_tables.h>
+
+#include <libmnl/libmnl.h>
+#include <libnftnl/object.h>
+
+#include "internal.h"
+#include "obj.h"
+
+static int
+nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type,
+ const void *data, uint32_t data_len)
+{
+ struct nftnl_obj_tunnel *tun = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_TUNNEL_ID:
+ memcpy(&tun->id, data, sizeof(tun->id));
+ break;
+ case NFTNL_OBJ_TUNNEL_IPV4_SRC:
+ memcpy(&tun->src_v4, data, sizeof(tun->src_v4));
+ break;
+ case NFTNL_OBJ_TUNNEL_IPV4_DST:
+ memcpy(&tun->dst_v4, data, sizeof(tun->dst_v4));
+ break;
+ case NFTNL_OBJ_TUNNEL_IPV6_SRC:
+ memcpy(&tun->src_v6, data, sizeof(struct in6_addr));
+ break;
+ case NFTNL_OBJ_TUNNEL_IPV6_DST:
+ memcpy(&tun->dst_v6, data, sizeof(struct in6_addr));
+ break;
+ case NFTNL_OBJ_TUNNEL_IPV6_FLOWLABEL:
+ memcpy(&tun->flowlabel, data, sizeof(tun->flowlabel));
+ break;
+ case NFTNL_OBJ_TUNNEL_SPORT:
+ memcpy(&tun->sport, data, sizeof(tun->sport));
+ break;
+ case NFTNL_OBJ_TUNNEL_DPORT:
+ memcpy(&tun->dport, data, sizeof(tun->dport));
+ break;
+ case NFTNL_OBJ_TUNNEL_FLAGS:
+ memcpy(&tun->tun_flags, data, sizeof(tun->tun_flags));
+ break;
+ case NFTNL_OBJ_TUNNEL_TOS:
+ memcpy(&tun->tun_tos, data, sizeof(tun->tun_tos));
+ break;
+ case NFTNL_OBJ_TUNNEL_TTL:
+ memcpy(&tun->tun_ttl, data, sizeof(tun->tun_ttl));
+ break;
+ case NFTNL_OBJ_TUNNEL_VXLAN_GBP:
+ memcpy(&tun->u.tun_vxlan.gbp, data, sizeof(tun->u.tun_vxlan.gbp));
+ break;
+ case NFTNL_OBJ_TUNNEL_ERSPAN_VERSION:
+ memcpy(&tun->u.tun_erspan.version, data, sizeof(tun->u.tun_erspan.version));
+ break;
+ case NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX:
+ memcpy(&tun->u.tun_erspan.u.v1_index, data, sizeof(tun->u.tun_erspan.u.v1_index));
+ break;
+ case NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID:
+ memcpy(&tun->u.tun_erspan.u.v2.hwid, data, sizeof(tun->u.tun_erspan.u.v2.hwid));
+ break;
+ case NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR:
+ memcpy(&tun->u.tun_erspan.u.v2.dir, data, sizeof(tun->u.tun_erspan.u.v2.dir));
+ break;
+ default:
+ return -1;
+ }
+ return 0;
+}
+
+static const void *
+nftnl_obj_tunnel_get(const struct nftnl_obj *e, uint16_t type,
+ uint32_t *data_len)
+{
+ struct nftnl_obj_tunnel *tun = nftnl_obj_data(e);
+
+ switch (type) {
+ case NFTNL_OBJ_TUNNEL_ID:
+ *data_len = sizeof(tun->id);
+ return &tun->id;
+ case NFTNL_OBJ_TUNNEL_IPV4_SRC:
+ *data_len = sizeof(tun->src_v4);
+ return &tun->src_v4;
+ case NFTNL_OBJ_TUNNEL_IPV4_DST:
+ *data_len = sizeof(tun->dst_v4);
+ return &tun->dst_v4;
+ case NFTNL_OBJ_TUNNEL_IPV6_SRC:
+ *data_len = sizeof(tun->src_v6);
+ return &tun->src_v6;
+ case NFTNL_OBJ_TUNNEL_IPV6_DST:
+ *data_len = sizeof(tun->dst_v6);
+ return &tun->dst_v6;
+ case NFTNL_OBJ_TUNNEL_IPV6_FLOWLABEL:
+ *data_len = sizeof(tun->flowlabel);
+ return &tun->flowlabel;
+ case NFTNL_OBJ_TUNNEL_SPORT:
+ *data_len = sizeof(tun->sport);
+ return &tun->sport;
+ case NFTNL_OBJ_TUNNEL_DPORT:
+ *data_len = sizeof(tun->dport);
+ return &tun->dport;
+ case NFTNL_OBJ_TUNNEL_FLAGS:
+ *data_len = sizeof(tun->tun_flags);
+ return &tun->tun_flags;
+ case NFTNL_OBJ_TUNNEL_TOS:
+ *data_len = sizeof(tun->tun_tos);
+ return &tun->tun_tos;
+ case NFTNL_OBJ_TUNNEL_TTL:
+ *data_len = sizeof(tun->tun_ttl);
+ return &tun->tun_ttl;
+ case NFTNL_OBJ_TUNNEL_VXLAN_GBP:
+ *data_len = sizeof(tun->u.tun_vxlan.gbp);
+ return &tun->u.tun_vxlan.gbp;
+ case NFTNL_OBJ_TUNNEL_ERSPAN_VERSION:
+ *data_len = sizeof(tun->u.tun_erspan.version);
+ return &tun->u.tun_erspan.version;
+ case NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX:
+ *data_len = sizeof(tun->u.tun_erspan.u.v1_index);
+ return &tun->u.tun_erspan.u.v1_index;
+ case NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID:
+ *data_len = sizeof(tun->u.tun_erspan.u.v2.hwid);
+ return &tun->u.tun_erspan.u.v2.hwid;
+ case NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR:
+ *data_len = sizeof(tun->u.tun_erspan.u.v2.dir);
+ return &tun->u.tun_erspan.u.v2.dir;
+ }
+ return NULL;
+}
+
+static int nftnl_obj_tunnel_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ if (mnl_attr_type_valid(attr, NFTA_TUNNEL_KEY_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch(type) {
+ case NFTA_TUNNEL_KEY_ID:
+ case NFTA_TUNNEL_KEY_FLAGS:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ abi_breakage();
+ break;
+ case NFTA_TUNNEL_KEY_IP:
+ case NFTA_TUNNEL_KEY_IP6:
+ case NFTA_TUNNEL_KEY_OPTS:
+ if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
+ abi_breakage();
+ break;
+ case NFTA_TUNNEL_KEY_SPORT:
+ case NFTA_TUNNEL_KEY_DPORT:
+ if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
+ abi_breakage();
+ break;
+ case NFTA_TUNNEL_KEY_TOS:
+ case NFTA_TUNNEL_KEY_TTL:
+ if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static void
+nftnl_obj_tunnel_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
+{
+ struct nftnl_obj_tunnel *tun = nftnl_obj_data(e);
+ struct nlattr *nest;
+
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_ID))
+ mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_ID, htonl(tun->id));
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV4_SRC) ||
+ e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV4_DST)) {
+ nest = mnl_attr_nest_start(nlh, NFTA_TUNNEL_KEY_IP);
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV4_SRC))
+ mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_IP_SRC, tun->src_v4);
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV4_DST))
+ mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_IP_DST, tun->dst_v4);
+ mnl_attr_nest_end(nlh, nest);
+ }
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV6_SRC) ||
+ e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV6_DST)) {
+ nest = mnl_attr_nest_start(nlh, NFTA_TUNNEL_KEY_IP6);
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV6_SRC))
+ mnl_attr_put(nlh, NFTA_TUNNEL_KEY_IP6_SRC,
+ sizeof(tun->src_v6), &tun->src_v6);
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV6_DST))
+ mnl_attr_put(nlh, NFTA_TUNNEL_KEY_IP6_DST,
+ sizeof(tun->dst_v6), &tun->dst_v6);
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_IPV6_FLOWLABEL))
+ mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_IP6_FLOWLABEL,
+ htonl(tun->flowlabel));
+ mnl_attr_nest_end(nlh, nest);
+ }
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_SPORT))
+ mnl_attr_put_u16(nlh, NFTA_TUNNEL_KEY_SPORT, htons(tun->sport));
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_DPORT))
+ mnl_attr_put_u16(nlh, NFTA_TUNNEL_KEY_DPORT, htons(tun->dport));
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_TOS))
+ mnl_attr_put_u8(nlh, NFTA_TUNNEL_KEY_TOS, tun->tun_tos);
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_TTL))
+ mnl_attr_put_u8(nlh, NFTA_TUNNEL_KEY_TTL, tun->tun_ttl);
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_FLAGS))
+ mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_FLAGS, htonl(tun->tun_flags));
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_VXLAN_GBP)) {
+ nest = mnl_attr_nest_start(nlh, NFTA_TUNNEL_KEY_OPTS);
+ mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_VXLAN_GBP,
+ htonl(tun->u.tun_vxlan.gbp));
+ mnl_attr_nest_end(nlh, nest);
+ }
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_ERSPAN_VERSION) &&
+ (e->flags & (1 << NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX) ||
+ (e->flags & (1 << NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID) &&
+ e->flags & (1u << NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR)))) {
+ struct nlattr *nest_inner;
+
+ nest = mnl_attr_nest_start(nlh, NFTA_TUNNEL_KEY_OPTS);
+ nest_inner = mnl_attr_nest_start(nlh, NFTA_TUNNEL_KEY_OPTS_ERSPAN);
+ mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_ERSPAN_VERSION,
+ htonl(tun->u.tun_erspan.version));
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX))
+ mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX,
+ htonl(tun->u.tun_erspan.u.v1_index));
+ if (e->flags & (1 << NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID))
+ mnl_attr_put_u8(nlh, NFTA_TUNNEL_KEY_ERSPAN_V2_HWID,
+ tun->u.tun_erspan.u.v2.hwid);
+ if (e->flags & (1u << NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR))
+ mnl_attr_put_u8(nlh, NFTA_TUNNEL_KEY_ERSPAN_V2_DIR,
+ tun->u.tun_erspan.u.v2.dir);
+ mnl_attr_nest_end(nlh, nest_inner);
+ mnl_attr_nest_end(nlh, nest);
+ }
+}
+
+static int nftnl_obj_tunnel_ip_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ if (mnl_attr_type_valid(attr, NFTA_TUNNEL_KEY_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch (type) {
+ case NFTA_TUNNEL_KEY_IP_SRC:
+ case NFTA_TUNNEL_KEY_IP_DST:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static int nftnl_obj_tunnel_parse_ip(struct nftnl_obj *e, struct nlattr *attr,
+ struct nftnl_obj_tunnel *tun)
+{
+ struct nlattr *tb[NFTA_TUNNEL_KEY_IP_MAX + 1] = {};
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_tunnel_ip_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_TUNNEL_KEY_IP_SRC]) {
+ tun->src_v4 = mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_IP_SRC]);
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_IPV4_SRC);
+ }
+ if (tb[NFTA_TUNNEL_KEY_IP_DST]) {
+ tun->dst_v4 = mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_IP_DST]);
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_IPV4_DST);
+ }
+
+ return 0;
+}
+
+static int nftnl_obj_tunnel_ip6_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ if (mnl_attr_type_valid(attr, NFTA_TUNNEL_KEY_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch(type) {
+ case NFTA_TUNNEL_KEY_IP6_SRC:
+ case NFTA_TUNNEL_KEY_IP6_DST:
+ if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0)
+ abi_breakage();
+ break;
+ case NFTA_TUNNEL_KEY_IP6_FLOWLABEL:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static int nftnl_obj_tunnel_parse_ip6(struct nftnl_obj *e, struct nlattr *attr,
+ struct nftnl_obj_tunnel *tun)
+{
+ struct nlattr *tb[NFTA_TUNNEL_KEY_IP6_MAX + 1] = {};
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_tunnel_ip6_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_TUNNEL_KEY_IP6_SRC]) {
+ memcpy(&tun->src_v6,
+ mnl_attr_get_payload(tb[NFTA_TUNNEL_KEY_IP6_SRC]),
+ sizeof(struct in6_addr));
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_IPV6_SRC);
+ }
+ if (tb[NFTA_TUNNEL_KEY_IP6_DST]) {
+ memcpy(&tun->dst_v6,
+ mnl_attr_get_payload(tb[NFTA_TUNNEL_KEY_IP6_DST]),
+ sizeof(struct in6_addr));
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_IPV6_DST);
+ }
+ if (tb[NFTA_TUNNEL_KEY_IP6_FLOWLABEL]) {
+ tun->flowlabel =
+ ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_IP6_FLOWLABEL]));
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_IPV6_FLOWLABEL);
+ }
+
+ return 0;
+}
+
+static int nftnl_obj_tunnel_vxlan_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ if (mnl_attr_type_valid(attr, NFTA_TUNNEL_KEY_VXLAN_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch (type) {
+ case NFTA_TUNNEL_KEY_VXLAN_GBP:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static int
+nftnl_obj_tunnel_parse_vxlan(struct nftnl_obj *e, struct nlattr *attr,
+ struct nftnl_obj_tunnel *tun)
+{
+ struct nlattr *tb[NFTA_TUNNEL_KEY_VXLAN_MAX + 1] = {};
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_tunnel_vxlan_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_TUNNEL_KEY_VXLAN_GBP]) {
+ tun->u.tun_vxlan.gbp =
+ ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_VXLAN_GBP]));
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_VXLAN_GBP);
+ }
+
+ return 0;
+}
+
+static int nftnl_obj_tunnel_erspan_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ if (mnl_attr_type_valid(attr, NFTA_TUNNEL_KEY_ERSPAN_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch (type) {
+ case NFTA_TUNNEL_KEY_ERSPAN_VERSION:
+ case NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ abi_breakage();
+ break;
+ case NFTA_TUNNEL_KEY_ERSPAN_V2_HWID:
+ case NFTA_TUNNEL_KEY_ERSPAN_V2_DIR:
+ if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static int
+nftnl_obj_tunnel_parse_erspan(struct nftnl_obj *e, struct nlattr *attr,
+ struct nftnl_obj_tunnel *tun)
+{
+ struct nlattr *tb[NFTA_TUNNEL_KEY_ERSPAN_MAX + 1] = {};
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_tunnel_erspan_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_TUNNEL_KEY_ERSPAN_VERSION]) {
+ tun->u.tun_erspan.version =
+ ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_ERSPAN_VERSION]));
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_ERSPAN_VERSION);
+ }
+ if (tb[NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX]) {
+ tun->u.tun_erspan.u.v1_index =
+ ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX]));
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX);
+ }
+ if (tb[NFTA_TUNNEL_KEY_ERSPAN_V2_HWID]) {
+ tun->u.tun_erspan.u.v2.hwid =
+ mnl_attr_get_u8(tb[NFTA_TUNNEL_KEY_ERSPAN_V2_HWID]);
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID);
+ }
+ if (tb[NFTA_TUNNEL_KEY_ERSPAN_V2_DIR]) {
+ tun->u.tun_erspan.u.v2.dir =
+ mnl_attr_get_u8(tb[NFTA_TUNNEL_KEY_ERSPAN_V2_DIR]);
+ e->flags |= (1u << NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR);
+ }
+
+ return 0;
+}
+
+static int nftnl_obj_tunnel_opts_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ if (mnl_attr_type_valid(attr, NFTA_TUNNEL_KEY_OPTS_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch (type) {
+ case NFTA_TUNNEL_KEY_OPTS_VXLAN:
+ case NFTA_TUNNEL_KEY_OPTS_ERSPAN:
+ if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+static int
+nftnl_obj_tunnel_parse_opts(struct nftnl_obj *e, struct nlattr *attr,
+ struct nftnl_obj_tunnel *tun)
+{
+ struct nlattr *tb[NFTA_TUNNEL_KEY_OPTS_MAX + 1] = {};
+ int err = 0;
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_tunnel_opts_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_TUNNEL_KEY_OPTS_VXLAN]) {
+ err = nftnl_obj_tunnel_parse_vxlan(e, tb[NFTA_TUNNEL_KEY_OPTS_VXLAN],
+ tun);
+ } else if (tb[NFTA_TUNNEL_KEY_OPTS_ERSPAN]) {
+ err = nftnl_obj_tunnel_parse_erspan(e, tb[NFTA_TUNNEL_KEY_OPTS_ERSPAN],
+ tun);
+ }
+
+ return err;
+}
+
+static int
+nftnl_obj_tunnel_parse(struct nftnl_obj *e, struct nlattr *attr)
+{
+ struct nftnl_obj_tunnel *tun = nftnl_obj_data(e);
+ struct nlattr *tb[NFTA_TUNNEL_KEY_MAX + 1] = {};
+ int err;
+
+ if (mnl_attr_parse_nested(attr, nftnl_obj_tunnel_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_TUNNEL_KEY_ID]) {
+ tun->id = ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY_ID]));
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_ID);
+ }
+ if (tb[NFTA_TUNNEL_KEY_IP]) {
+ err = nftnl_obj_tunnel_parse_ip(e, tb[NFTA_TUNNEL_KEY_IP], tun);
+ if (err < 0)
+ return err;
+ } else if (tb[NFTA_TUNNEL_KEY_IP6]) {
+ err = nftnl_obj_tunnel_parse_ip6(e, tb[NFTA_TUNNEL_KEY_IP6], tun);
+ if (err < 0)
+ return err;
+ }
+
+ if (tb[NFTA_TUNNEL_KEY_SPORT]) {
+ tun->sport = ntohs(mnl_attr_get_u16(tb[NFTA_TUNNEL_KEY_SPORT]));
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_SPORT);
+ }
+ if (tb[NFTA_TUNNEL_KEY_DPORT]) {
+ tun->dport = ntohs(mnl_attr_get_u16(tb[NFTA_TUNNEL_KEY_DPORT]));
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_DPORT);
+ }
+ if (tb[NFTA_TUNNEL_KEY_TOS]) {
+ tun->tun_tos = mnl_attr_get_u8(tb[NFTA_TUNNEL_KEY_TOS]);
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_TOS);
+ }
+ if (tb[NFTA_TUNNEL_KEY_TTL]) {
+ tun->tun_ttl = mnl_attr_get_u8(tb[NFTA_TUNNEL_KEY_TTL]);
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_TTL);
+ }
+ if (tb[NFTA_TUNNEL_KEY_FLAGS]) {
+ tun->tun_flags = mnl_attr_get_u8(tb[NFTA_TUNNEL_KEY_FLAGS]);
+ e->flags |= (1 << NFTNL_OBJ_TUNNEL_FLAGS);
+ }
+ if (tb[NFTA_TUNNEL_KEY_OPTS]) {
+ err = nftnl_obj_tunnel_parse_opts(e, tb[NFTA_TUNNEL_KEY_OPTS], tun);
+ if (err < 0)
+ return err;
+ }
+
+ return 0;
+}
+
+static int nftnl_obj_tunnel_snprintf(char *buf, size_t len,
+ uint32_t flags, const struct nftnl_obj *e)
+{
+ struct nftnl_obj_tunnel *tun = nftnl_obj_data(e);
+
+ return snprintf(buf, len, "id %u ", tun->id);
+}
+
+struct obj_ops obj_ops_tunnel = {
+ .name = "tunnel",
+ .type = NFT_OBJECT_TUNNEL,
+ .alloc_len = sizeof(struct nftnl_obj_tunnel),
+ .max_attr = NFTA_TUNNEL_KEY_MAX,
+ .set = nftnl_obj_tunnel_set,
+ .get = nftnl_obj_tunnel_get,
+ .parse = nftnl_obj_tunnel_parse,
+ .build = nftnl_obj_tunnel_build,
+ .output = nftnl_obj_tunnel_snprintf,
+};
diff --git a/src/object.c b/src/object.c
new file mode 100644
index 0000000..232b97a
--- /dev/null
+++ b/src/object.c
@@ -0,0 +1,562 @@
+/*
+ * (C) 2012-2016 by Pablo Neira Ayuso <pablo@netfilter.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+#include "internal.h"
+
+#include <time.h>
+#include <endian.h>
+#include <stdint.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+#include <libmnl/libmnl.h>
+#include <linux/netfilter/nfnetlink.h>
+#include <linux/netfilter/nf_tables.h>
+
+#include <libnftnl/object.h>
+#include "obj.h"
+
+static struct obj_ops *obj_ops[__NFT_OBJECT_MAX] = {
+ [NFT_OBJECT_COUNTER] = &obj_ops_counter,
+ [NFT_OBJECT_QUOTA] = &obj_ops_quota,
+ [NFT_OBJECT_CT_HELPER] = &obj_ops_ct_helper,
+ [NFT_OBJECT_LIMIT] = &obj_ops_limit,
+ [NFT_OBJECT_TUNNEL] = &obj_ops_tunnel,
+ [NFT_OBJECT_CT_TIMEOUT] = &obj_ops_ct_timeout,
+ [NFT_OBJECT_SECMARK] = &obj_ops_secmark,
+ [NFT_OBJECT_CT_EXPECT] = &obj_ops_ct_expect,
+ [NFT_OBJECT_SYNPROXY] = &obj_ops_synproxy,
+};
+
+static struct obj_ops *nftnl_obj_ops_lookup(uint32_t type)
+{
+ if (type > NFT_OBJECT_MAX)
+ return NULL;
+
+ return obj_ops[type];
+}
+
+EXPORT_SYMBOL(nftnl_obj_alloc);
+struct nftnl_obj *nftnl_obj_alloc(void)
+{
+ return calloc(1, sizeof(struct nftnl_obj));
+}
+
+EXPORT_SYMBOL(nftnl_obj_free);
+void nftnl_obj_free(const struct nftnl_obj *obj)
+{
+ if (obj->flags & (1 << NFTNL_OBJ_TABLE))
+ xfree(obj->table);
+ if (obj->flags & (1 << NFTNL_OBJ_NAME))
+ xfree(obj->name);
+ if (obj->flags & (1 << NFTNL_OBJ_USERDATA))
+ xfree(obj->user.data);
+
+ xfree(obj);
+}
+
+EXPORT_SYMBOL(nftnl_obj_is_set);
+bool nftnl_obj_is_set(const struct nftnl_obj *obj, uint16_t attr)
+{
+ return obj->flags & (1 << attr);
+}
+
+static uint32_t nftnl_obj_validate[NFTNL_OBJ_MAX + 1] = {
+ [NFTNL_OBJ_FAMILY] = sizeof(uint32_t),
+ [NFTNL_OBJ_USE] = sizeof(uint32_t),
+ [NFTNL_OBJ_HANDLE] = sizeof(uint64_t),
+};
+
+EXPORT_SYMBOL(nftnl_obj_set_data);
+void nftnl_obj_set_data(struct nftnl_obj *obj, uint16_t attr,
+ const void *data, uint32_t data_len)
+{
+ if (attr < NFTNL_OBJ_MAX)
+ nftnl_assert_validate(data, nftnl_obj_validate, attr, data_len);
+
+ switch (attr) {
+ case NFTNL_OBJ_TABLE:
+ xfree(obj->table);
+ obj->table = strdup(data);
+ break;
+ case NFTNL_OBJ_NAME:
+ xfree(obj->name);
+ obj->name = strdup(data);
+ break;
+ case NFTNL_OBJ_TYPE:
+ obj->ops = nftnl_obj_ops_lookup(*((uint32_t *)data));
+ if (!obj->ops)
+ return;
+ break;
+ case NFTNL_OBJ_FAMILY:
+ memcpy(&obj->family, data, sizeof(obj->family));
+ break;
+ case NFTNL_OBJ_USE:
+ memcpy(&obj->use, data, sizeof(obj->use));
+ break;
+ case NFTNL_OBJ_HANDLE:
+ memcpy(&obj->handle, data, sizeof(obj->handle));
+ break;
+ case NFTNL_OBJ_USERDATA:
+ if (obj->flags & (1 << NFTNL_OBJ_USERDATA))
+ xfree(obj->user.data);
+
+ obj->user.data = malloc(data_len);
+ if (!obj->user.data)
+ return;
+ memcpy(obj->user.data, data, data_len);
+ obj->user.len = data_len;
+ break;
+ default:
+ if (obj->ops)
+ obj->ops->set(obj, attr, data, data_len);
+ break;
+ }
+ obj->flags |= (1 << attr);
+}
+
+void nftnl_obj_set(struct nftnl_obj *obj, uint16_t attr, const void *data) __visible;
+void nftnl_obj_set(struct nftnl_obj *obj, uint16_t attr, const void *data)
+{
+ nftnl_obj_set_data(obj, attr, data, nftnl_obj_validate[attr]);
+}
+
+EXPORT_SYMBOL(nftnl_obj_set_u8);
+void nftnl_obj_set_u8(struct nftnl_obj *obj, uint16_t attr, uint8_t val)
+{
+ nftnl_obj_set_data(obj, attr, &val, sizeof(uint8_t));
+}
+
+EXPORT_SYMBOL(nftnl_obj_set_u16);
+void nftnl_obj_set_u16(struct nftnl_obj *obj, uint16_t attr, uint16_t val)
+{
+ nftnl_obj_set_data(obj, attr, &val, sizeof(uint16_t));
+}
+
+EXPORT_SYMBOL(nftnl_obj_set_u32);
+void nftnl_obj_set_u32(struct nftnl_obj *obj, uint16_t attr, uint32_t val)
+{
+ nftnl_obj_set_data(obj, attr, &val, sizeof(uint32_t));
+}
+
+EXPORT_SYMBOL(nftnl_obj_set_u64);
+void nftnl_obj_set_u64(struct nftnl_obj *obj, uint16_t attr, uint64_t val)
+{
+ nftnl_obj_set_data(obj, attr, &val, sizeof(uint64_t));
+}
+
+EXPORT_SYMBOL(nftnl_obj_set_str);
+void nftnl_obj_set_str(struct nftnl_obj *obj, uint16_t attr, const char *str)
+{
+ nftnl_obj_set_data(obj, attr, str, 0);
+}
+
+EXPORT_SYMBOL(nftnl_obj_get_data);
+const void *nftnl_obj_get_data(struct nftnl_obj *obj, uint16_t attr,
+ uint32_t *data_len)
+{
+ if (!(obj->flags & (1 << attr)))
+ return NULL;
+
+ switch(attr) {
+ case NFTNL_OBJ_TABLE:
+ return obj->table;
+ case NFTNL_OBJ_NAME:
+ return obj->name;
+ case NFTNL_OBJ_TYPE:
+ if (!obj->ops)
+ return NULL;
+
+ *data_len = sizeof(uint32_t);
+ return &obj->ops->type;
+ case NFTNL_OBJ_FAMILY:
+ *data_len = sizeof(uint32_t);
+ return &obj->family;
+ case NFTNL_OBJ_USE:
+ *data_len = sizeof(uint32_t);
+ return &obj->use;
+ case NFTNL_OBJ_HANDLE:
+ *data_len = sizeof(uint64_t);
+ return &obj->handle;
+ case NFTNL_OBJ_USERDATA:
+ *data_len = obj->user.len;
+ return obj->user.data;
+ default:
+ if (obj->ops)
+ return obj->ops->get(obj, attr, data_len);
+ break;
+ }
+ return NULL;
+}
+
+EXPORT_SYMBOL(nftnl_obj_get);
+const void *nftnl_obj_get(struct nftnl_obj *obj, uint16_t attr)
+{
+ uint32_t data_len;
+ return nftnl_obj_get_data(obj, attr, &data_len);
+}
+
+EXPORT_SYMBOL(nftnl_obj_get_u8);
+uint8_t nftnl_obj_get_u8(struct nftnl_obj *obj, uint16_t attr)
+{
+ const void *ret = nftnl_obj_get(obj, attr);
+ return ret == NULL ? 0 : *((uint8_t *)ret);
+}
+
+EXPORT_SYMBOL(nftnl_obj_get_u16);
+uint16_t nftnl_obj_get_u16(struct nftnl_obj *obj, uint16_t attr)
+{
+ const void *ret = nftnl_obj_get(obj, attr);
+ return ret == NULL ? 0 : *((uint16_t *)ret);
+}
+
+EXPORT_SYMBOL(nftnl_obj_get_u32);
+uint32_t nftnl_obj_get_u32(struct nftnl_obj *obj, uint16_t attr)
+{
+ const void *ret = nftnl_obj_get(obj, attr);
+ return ret == NULL ? 0 : *((uint32_t *)ret);
+}
+
+EXPORT_SYMBOL(nftnl_obj_get_u64);
+uint64_t nftnl_obj_get_u64(struct nftnl_obj *obj, uint16_t attr)
+{
+ const void *ret = nftnl_obj_get(obj, attr);
+ return ret == NULL ? 0 : *((uint64_t *)ret);
+}
+
+EXPORT_SYMBOL(nftnl_obj_get_str);
+const char *nftnl_obj_get_str(struct nftnl_obj *obj, uint16_t attr)
+{
+ return nftnl_obj_get(obj, attr);
+}
+
+EXPORT_SYMBOL(nftnl_obj_nlmsg_build_payload);
+void nftnl_obj_nlmsg_build_payload(struct nlmsghdr *nlh,
+ const struct nftnl_obj *obj)
+{
+ if (obj->flags & (1 << NFTNL_OBJ_TABLE))
+ mnl_attr_put_strz(nlh, NFTA_OBJ_TABLE, obj->table);
+ if (obj->flags & (1 << NFTNL_OBJ_NAME))
+ mnl_attr_put_strz(nlh, NFTA_OBJ_NAME, obj->name);
+ if (obj->flags & (1 << NFTNL_OBJ_TYPE))
+ mnl_attr_put_u32(nlh, NFTA_OBJ_TYPE, htonl(obj->ops->type));
+ if (obj->flags & (1 << NFTNL_OBJ_HANDLE))
+ mnl_attr_put_u64(nlh, NFTA_OBJ_HANDLE, htobe64(obj->handle));
+ if (obj->flags & (1 << NFTNL_OBJ_USERDATA))
+ mnl_attr_put(nlh, NFTA_OBJ_USERDATA, obj->user.len, obj->user.data);
+ if (obj->ops) {
+ struct nlattr *nest = mnl_attr_nest_start(nlh, NFTA_OBJ_DATA);
+
+ obj->ops->build(nlh, obj);
+ mnl_attr_nest_end(nlh, nest);
+ }
+}
+
+static int nftnl_obj_parse_attr_cb(const struct nlattr *attr, void *data)
+{
+ const struct nlattr **tb = data;
+ int type = mnl_attr_get_type(attr);
+
+ if (mnl_attr_type_valid(attr, NFTA_OBJ_MAX) < 0)
+ return MNL_CB_OK;
+
+ switch(type) {
+ case NFTA_OBJ_TABLE:
+ case NFTA_OBJ_NAME:
+ if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
+ abi_breakage();
+ break;
+ case NFTA_OBJ_HANDLE:
+ if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
+ abi_breakage();
+ break;
+ case NFTA_OBJ_DATA:
+ if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
+ abi_breakage();
+ break;
+ case NFTA_OBJ_USE:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ abi_breakage();
+ break;
+ case NFTA_OBJ_USERDATA:
+ if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0)
+ abi_breakage();
+ break;
+ }
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+EXPORT_SYMBOL(nftnl_obj_nlmsg_parse);
+int nftnl_obj_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_obj *obj)
+{
+ struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh);
+ struct nlattr *tb[NFTA_OBJ_MAX + 1] = {};
+ int err;
+
+ if (mnl_attr_parse(nlh, sizeof(*nfg), nftnl_obj_parse_attr_cb, tb) < 0)
+ return -1;
+
+ if (tb[NFTA_OBJ_TABLE]) {
+ obj->table = strdup(mnl_attr_get_str(tb[NFTA_OBJ_TABLE]));
+ obj->flags |= (1 << NFTNL_OBJ_TABLE);
+ }
+ if (tb[NFTA_OBJ_NAME]) {
+ obj->name = strdup(mnl_attr_get_str(tb[NFTA_OBJ_NAME]));
+ obj->flags |= (1 << NFTNL_OBJ_NAME);
+ }
+ if (tb[NFTA_OBJ_TYPE]) {
+ uint32_t type = ntohl(mnl_attr_get_u32(tb[NFTA_OBJ_TYPE]));
+
+ obj->ops = nftnl_obj_ops_lookup(type);
+ if (obj->ops)
+ obj->flags |= (1 << NFTNL_OBJ_TYPE);
+ }
+ if (tb[NFTA_OBJ_DATA]) {
+ if (obj->ops) {
+ err = obj->ops->parse(obj, tb[NFTA_OBJ_DATA]);
+ if (err < 0)
+ return err;
+ }
+ }
+ if (tb[NFTA_OBJ_USE]) {
+ obj->use = ntohl(mnl_attr_get_u32(tb[NFTA_OBJ_USE]));
+ obj->flags |= (1 << NFTNL_OBJ_USE);
+ }
+ if (tb[NFTA_OBJ_HANDLE]) {
+ obj->handle = be64toh(mnl_attr_get_u64(tb[NFTA_OBJ_HANDLE]));
+ obj->flags |= (1 << NFTNL_OBJ_HANDLE);
+ }
+ if (tb[NFTA_OBJ_USERDATA]) {
+ nftnl_obj_set_data(obj, NFTNL_OBJ_USERDATA,
+ mnl_attr_get_payload(tb[NFTA_OBJ_USERDATA]),
+ mnl_attr_get_payload_len(tb[NFTA_OBJ_USERDATA]));
+ }
+
+ obj->family = nfg->nfgen_family;
+ obj->flags |= (1 << NFTNL_OBJ_FAMILY);
+
+ return 0;
+}
+
+static int nftnl_obj_do_parse(struct nftnl_obj *obj, enum nftnl_parse_type type,
+ const void *data, struct nftnl_parse_err *err,
+ enum nftnl_parse_input input)
+{
+ struct nftnl_parse_err perr = {};
+ int ret;
+
+ switch (type) {
+ case NFTNL_PARSE_JSON:
+ case NFTNL_PARSE_XML:
+ default:
+ ret = -1;
+ errno = EOPNOTSUPP;
+ break;
+ }
+
+ if (err != NULL)
+ *err = perr;
+
+ return ret;
+}
+
+EXPORT_SYMBOL(nftnl_obj_parse);
+int nftnl_obj_parse(struct nftnl_obj *obj, enum nftnl_parse_type type,
+ const char *data, struct nftnl_parse_err *err)
+{
+ return nftnl_obj_do_parse(obj, type, data, err, NFTNL_PARSE_BUFFER);
+}
+
+EXPORT_SYMBOL(nftnl_obj_parse_file);
+int nftnl_obj_parse_file(struct nftnl_obj *obj, enum nftnl_parse_type type,
+ FILE *fp, struct nftnl_parse_err *err)
+{
+ return nftnl_obj_do_parse(obj, type, fp, err, NFTNL_PARSE_FILE);
+}
+
+static int nftnl_obj_snprintf_dflt(char *buf, size_t remain,
+ const struct nftnl_obj *obj,
+ uint32_t type, uint32_t flags)
+{
+ const char *name = obj->ops ? obj->ops->name : "(unknown)";
+ int ret, offset = 0;
+
+ ret = snprintf(buf, remain, "table %s name %s use %u [ %s ",
+ obj->table, obj->name, obj->use, name);
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+
+ if (obj->ops) {
+ ret = obj->ops->output(buf + offset, remain, flags, obj);
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ }
+ ret = snprintf(buf + offset, remain, "]");
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+
+ return offset;
+}
+
+static int nftnl_obj_cmd_snprintf(char *buf, size_t remain,
+ const struct nftnl_obj *obj, uint32_t cmd,
+ uint32_t type, uint32_t flags)
+{
+ int ret, offset = 0;
+
+ if (type != NFTNL_OUTPUT_DEFAULT)
+ return -1;
+
+ ret = nftnl_obj_snprintf_dflt(buf + offset, remain, obj, type, flags);
+ SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+ return offset;
+}
+
+EXPORT_SYMBOL(nftnl_obj_snprintf);
+int nftnl_obj_snprintf(char *buf, size_t size, const struct nftnl_obj *obj,
+ uint32_t type, uint32_t flags)
+{
+ if (size)
+ buf[0] = '\0';
+
+ return nftnl_obj_cmd_snprintf(buf, size, obj, nftnl_flag2cmd(flags),
+ type, flags);
+}
+
+static int nftnl_obj_do_snprintf(char *buf, size_t size, const void *obj,
+ uint32_t cmd, uint32_t type, uint32_t flags)
+{
+ return nftnl_obj_snprintf(buf, size, obj, type, flags);
+}
+
+EXPORT_SYMBOL(nftnl_obj_fprintf);
+int nftnl_obj_fprintf(FILE *fp, const struct nftnl_obj *obj, uint32_t type,
+ uint32_t flags)
+{
+ return nftnl_fprintf(fp, obj, NFTNL_CMD_UNSPEC, type, flags,
+ nftnl_obj_do_snprintf);
+}
+
+struct nftnl_obj_list {
+ struct list_head list;
+};
+
+EXPORT_SYMBOL(nftnl_obj_list_alloc);
+struct nftnl_obj_list *nftnl_obj_list_alloc(void)
+{
+ struct nftnl_obj_list *list;
+
+ list = calloc(1, sizeof(struct nftnl_obj_list));
+ if (list == NULL)
+ return NULL;
+
+ INIT_LIST_HEAD(&list->list);
+
+ return list;
+}
+
+EXPORT_SYMBOL(nftnl_obj_list_free);
+void nftnl_obj_list_free(struct nftnl_obj_list *list)
+{
+ struct nftnl_obj *r, *tmp;
+
+ list_for_each_entry_safe(r, tmp, &list->list, head) {
+ list_del(&r->head);
+ nftnl_obj_free(r);
+ }
+ xfree(list);
+}
+
+EXPORT_SYMBOL(nftnl_obj_list_is_empty);
+int nftnl_obj_list_is_empty(struct nftnl_obj_list *list)
+{
+ return list_empty(&list->list);
+}
+
+EXPORT_SYMBOL(nftnl_obj_list_add);
+void nftnl_obj_list_add(struct nftnl_obj *r, struct nftnl_obj_list *list)
+{
+ list_add(&r->head, &list->list);
+}
+
+EXPORT_SYMBOL(nftnl_obj_list_add_tail);
+void nftnl_obj_list_add_tail(struct nftnl_obj *r,
+ struct nftnl_obj_list *list)
+{
+ list_add_tail(&r->head, &list->list);
+}
+
+EXPORT_SYMBOL(nftnl_obj_list_del);
+void nftnl_obj_list_del(struct nftnl_obj *t)
+{
+ list_del(&t->head);
+}
+
+EXPORT_SYMBOL(nftnl_obj_list_foreach);
+int nftnl_obj_list_foreach(struct nftnl_obj_list *table_list,
+ int (*cb)(struct nftnl_obj *t, void *data),
+ void *data)
+{
+ struct nftnl_obj *cur, *tmp;
+ int ret;
+
+ list_for_each_entry_safe(cur, tmp, &table_list->list, head) {
+ ret = cb(cur, data);
+ if (ret < 0)
+ return ret;
+ }
+ return 0;
+}
+
+struct nftnl_obj_list_iter {
+ struct nftnl_obj_list *list;
+ struct nftnl_obj *cur;
+};
+
+EXPORT_SYMBOL(nftnl_obj_list_iter_create);
+struct nftnl_obj_list_iter *
+nftnl_obj_list_iter_create(struct nftnl_obj_list *l)
+{
+ struct nftnl_obj_list_iter *iter;
+
+ iter = calloc(1, sizeof(struct nftnl_obj_list_iter));
+ if (iter == NULL)
+ return NULL;
+
+ iter->list = l;
+ if (nftnl_obj_list_is_empty(l))
+ iter->cur = NULL;
+ else
+ iter->cur = list_entry(l->list.next, struct nftnl_obj, head);
+
+ return iter;
+}
+
+EXPORT_SYMBOL(nftnl_obj_list_iter_next);
+struct nftnl_obj *nftnl_obj_list_iter_next(struct nftnl_obj_list_iter *iter)
+{
+ struct nftnl_obj *r = iter->cur;
+
+ if (r == NULL)
+ return NULL;
+
+ /* get next table, if any */
+ iter->cur = list_entry(iter->cur->head.next, struct nftnl_obj, head);
+ if (&iter->cur->head == iter->list->list.next)
+ return NULL;
+
+ return r;
+}
+
+EXPORT_SYMBOL(nftnl_obj_list_iter_destroy);
+void nftnl_obj_list_iter_destroy(struct nftnl_obj_list_iter *iter)
+{
+ xfree(iter);
+}