10#include <linux/netfilter/nf_tables.h>
13#include <libmnl/libmnl.h>
14#include <libnftnl/object.h>
18static int nftnl_obj_connlimit_set(
struct nftnl_obj *e, uint16_t type,
19 const void *data, uint32_t data_len)
21 struct nftnl_obj_connlimit *connlimit = nftnl_obj_data(e);
24 case NFTNL_OBJ_CONNLIMIT_COUNT:
25 memcpy(&connlimit->count, data, data_len);
27 case NFTNL_OBJ_CONNLIMIT_FLAGS:
28 memcpy(&connlimit->flags, data, data_len);
34static const void *nftnl_obj_connlimit_get(
const struct nftnl_obj *e,
35 uint16_t type, uint32_t *data_len)
37 struct nftnl_obj_connlimit *connlimit = nftnl_obj_data(e);
40 case NFTNL_OBJ_CONNLIMIT_COUNT:
41 *data_len =
sizeof(connlimit->count);
42 return &connlimit->count;
43 case NFTNL_OBJ_CONNLIMIT_FLAGS:
44 *data_len =
sizeof(connlimit->flags);
45 return &connlimit->flags;
50static int nftnl_obj_connlimit_cb(
const struct nlattr *attr,
void *data)
52 int type = mnl_attr_get_type(attr);
53 const struct nlattr **tb = data;
55 if (mnl_attr_type_valid(attr, NFTA_CONNLIMIT_MAX) < 0)
59 case NFTA_CONNLIMIT_COUNT:
60 case NFTA_CONNLIMIT_FLAGS:
61 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
70static void nftnl_obj_connlimit_build(
struct nlmsghdr *nlh,
71 const struct nftnl_obj *e)
73 struct nftnl_obj_connlimit *connlimit = nftnl_obj_data(e);
75 if (e->flags & (1 << NFTNL_OBJ_CONNLIMIT_COUNT))
76 mnl_attr_put_u32(nlh, NFTA_CONNLIMIT_COUNT,
77 htonl(connlimit->count));
78 if (e->flags & (1 << NFTNL_OBJ_CONNLIMIT_FLAGS))
79 mnl_attr_put_u32(nlh, NFTA_CONNLIMIT_FLAGS,
80 htonl(connlimit->flags));
83static int nftnl_obj_connlimit_parse(
struct nftnl_obj *e,
struct nlattr *attr)
85 struct nftnl_obj_connlimit *connlimit = nftnl_obj_data(e);
86 struct nlattr *tb[NFTA_CONNLIMIT_MAX + 1] = {};
88 if (mnl_attr_parse_nested(attr, nftnl_obj_connlimit_cb, tb) < 0)
91 if (tb[NFTA_CONNLIMIT_COUNT]) {
92 connlimit->count = ntohl(mnl_attr_get_u32(tb[NFTA_CONNLIMIT_COUNT]));
93 e->flags |= (1 << NFTNL_OBJ_CONNLIMIT_COUNT);
95 if (tb[NFTA_CONNLIMIT_FLAGS]) {
96 connlimit->flags = ntohl(mnl_attr_get_u32(tb[NFTA_CONNLIMIT_FLAGS]));
97 e->flags |= (1 << NFTNL_OBJ_CONNLIMIT_FLAGS);
103static int nftnl_obj_connlimit_snprintf(
char *buf,
size_t len,
105 const struct nftnl_obj *e)
107 struct nftnl_obj_connlimit *connlimit = nftnl_obj_data(e);
109 return snprintf(buf, len,
"count %u flags %x ",
110 connlimit->count, connlimit->flags);
113static struct attr_policy obj_connlimit_attr_policy[__NFTNL_OBJ_CONNLIMIT_MAX] = {
114 [NFTNL_OBJ_CONNLIMIT_COUNT] = { .maxlen =
sizeof(uint32_t) },
115 [NFTNL_OBJ_CONNLIMIT_FLAGS] = { .maxlen =
sizeof(uint32_t) },
118struct obj_ops obj_ops_connlimit = {
120 .type = NFT_OBJECT_CONNLIMIT,
121 .alloc_len =
sizeof(
struct nftnl_obj_connlimit),
122 .nftnl_max_attr = __NFTNL_OBJ_CONNLIMIT_MAX,
123 .attr_policy = obj_connlimit_attr_policy,
124 .set = nftnl_obj_connlimit_set,
125 .get = nftnl_obj_connlimit_get,
126 .parse = nftnl_obj_connlimit_parse,
127 .build = nftnl_obj_connlimit_build,
128 .output = nftnl_obj_connlimit_snprintf,