12 #include <arpa/inet.h>
16 #include <linux/netfilter/nf_tables.h>
19 #include <libmnl/libmnl.h>
20 #include <libnftnl/object.h>
24 static int nftnl_obj_secmark_set(
struct nftnl_obj *e, uint16_t type,
25 const void *data, uint32_t data_len)
27 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
30 case NFTNL_OBJ_SECMARK_CTX:
31 snprintf(secmark->ctx,
sizeof(secmark->ctx),
"%s", (
const char *)data);
39 static const void *nftnl_obj_secmark_get(
const struct nftnl_obj *e,
40 uint16_t type, uint32_t *data_len)
42 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
45 case NFTNL_OBJ_SECMARK_CTX:
46 *data_len = strlen(secmark->ctx);
52 static int nftnl_obj_secmark_cb(
const struct nlattr *attr,
void *data)
54 const struct nftnl_obj_secmark *secmark = NULL;
55 int type = mnl_attr_get_type(attr);
56 const struct nlattr **tb = data;
58 if (mnl_attr_type_valid(attr, NFTA_SECMARK_MAX) < 0)
62 case NFTA_SECMARK_CTX:
63 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
65 if (mnl_attr_get_payload_len(attr) >=
sizeof(secmark->ctx))
75 nftnl_obj_secmark_build(
struct nlmsghdr *nlh,
const struct nftnl_obj *e)
77 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
79 if (e->flags & (1 << NFTNL_OBJ_SECMARK_CTX))
80 mnl_attr_put_str(nlh, NFTA_SECMARK_CTX, secmark->ctx);
84 nftnl_obj_secmark_parse(
struct nftnl_obj *e,
struct nlattr *attr)
86 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
87 struct nlattr *tb[NFTA_SECMARK_MAX + 1] = {};
89 if (mnl_attr_parse_nested(attr, nftnl_obj_secmark_cb, tb) < 0)
92 if (tb[NFTA_SECMARK_CTX]) {
93 snprintf(secmark->ctx,
sizeof(secmark->ctx),
"%s",
94 mnl_attr_get_str(tb[NFTA_SECMARK_CTX]));
95 e->flags |= (1 << NFTNL_OBJ_SECMARK_CTX);
101 static int nftnl_obj_secmark_snprintf_default(
char *buf,
size_t len,
102 const struct nftnl_obj *e)
104 struct nftnl_obj_secmark *secmark = nftnl_obj_data(e);
106 return snprintf(buf, len,
"context %s ", secmark->ctx);
109 static int nftnl_obj_secmark_snprintf(
char *buf,
size_t len, uint32_t type,
111 const struct nftnl_obj *e)
117 case NFTNL_OUTPUT_DEFAULT:
118 return nftnl_obj_secmark_snprintf_default(buf, len, e);
119 case NFTNL_OUTPUT_XML:
120 case NFTNL_OUTPUT_JSON:
127 struct obj_ops obj_ops_secmark = {
129 .type = NFT_OBJECT_SECMARK,
130 .alloc_len =
sizeof(
struct nftnl_obj_secmark),
131 .max_attr = NFTA_SECMARK_MAX,
132 .set = nftnl_obj_secmark_set,
133 .get = nftnl_obj_secmark_get,
134 .parse = nftnl_obj_secmark_parse,
135 .build = nftnl_obj_secmark_build,
136 .snprintf = nftnl_obj_secmark_snprintf,