6 #include <linux/netfilter/nf_tables.h>
9 #include <libmnl/libmnl.h>
10 #include <libnftnl/expr.h>
11 #include <libnftnl/rule.h>
13 #define OSF_GENRE_SIZE 32
16 enum nft_registers dreg;
20 static int nftnl_expr_osf_set(
struct nftnl_expr *e, uint16_t type,
21 const void *data, uint32_t data_len)
26 case NFTNL_EXPR_OSF_DREG:
27 memcpy(&osf->dreg, data,
sizeof(osf->dreg));
29 case NFTNL_EXPR_OSF_TTL:
30 memcpy(&osf->ttl, data,
sizeof(osf->ttl));
37 nftnl_expr_osf_get(
const struct nftnl_expr *e, uint16_t type,
43 case NFTNL_EXPR_OSF_DREG:
44 *data_len =
sizeof(osf->dreg);
46 case NFTNL_EXPR_OSF_TTL:
47 *data_len =
sizeof(osf->ttl);
53 static int nftnl_expr_osf_cb(
const struct nlattr *attr,
void *data)
55 const struct nlattr **tb = data;
56 int type = mnl_attr_get_type(attr);
58 if (mnl_attr_type_valid(attr, NFTA_OSF_MAX) < 0)
62 case NFTNL_EXPR_OSF_DREG:
63 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
67 case NFTNL_EXPR_OSF_TTL:
68 if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
78 nftnl_expr_osf_build(
struct nlmsghdr *nlh,
const struct nftnl_expr *e)
82 if (e->flags & (1 << NFTNL_EXPR_OSF_DREG))
83 mnl_attr_put_u32(nlh, NFTNL_EXPR_OSF_DREG, htonl(osf->dreg));
84 if (e->flags & (1 << NFTNL_EXPR_OSF_TTL))
85 mnl_attr_put_u8(nlh, NFTNL_EXPR_OSF_TTL, osf->ttl);
89 nftnl_expr_osf_parse(
struct nftnl_expr *e,
struct nlattr *attr)
92 struct nlattr *tb[NFTA_OSF_MAX + 1] = {};
94 if (mnl_attr_parse_nested(attr, nftnl_expr_osf_cb, tb) < 0)
97 if (tb[NFTA_OSF_DREG]) {
98 osf->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_OSF_DREG]));
99 e->flags |= (1 << NFTNL_EXPR_OSF_DREG);
102 if (tb[NFTA_OSF_TTL]) {
103 osf->ttl = mnl_attr_get_u8(tb[NFTA_OSF_TTL]);
104 e->flags |= (1 << NFTNL_EXPR_OSF_TTL);
110 static int nftnl_expr_osf_snprintf_default(
char *buf,
size_t size,
111 const struct nftnl_expr *e)
114 int ret, offset = 0, len = size;
116 if (e->flags & (1 << NFTNL_EXPR_OSF_DREG)) {
117 ret = snprintf(buf, len,
"dreg %u ", osf->dreg);
118 SNPRINTF_BUFFER_SIZE(ret, len, offset);
125 nftnl_expr_osf_snprintf(
char *buf,
size_t len, uint32_t type,
126 uint32_t flags,
const struct nftnl_expr *e)
129 case NFTNL_OUTPUT_DEFAULT:
130 return nftnl_expr_osf_snprintf_default(buf, len, e);
131 case NFTNL_OUTPUT_XML:
132 case NFTNL_OUTPUT_JSON:
139 struct expr_ops expr_ops_osf = {
142 .max_attr = NFTA_OSF_MAX,
143 .set = nftnl_expr_osf_set,
144 .get = nftnl_expr_osf_get,
145 .parse = nftnl_expr_osf_parse,
146 .build = nftnl_expr_osf_build,
147 .snprintf = nftnl_expr_osf_snprintf,