17#include <linux/netfilter/nf_tables.h>
20#include <libmnl/libmnl.h>
21#include <libnftnl/expr.h>
22#include <libnftnl/rule.h>
27 enum nft_registers dreg;
31nftnl_expr_fib_set(
struct nftnl_expr *e, uint16_t result,
32 const void *data, uint32_t data_len)
37 case NFTNL_EXPR_FIB_RESULT:
38 memcpy(&fib->result, data,
sizeof(fib->result));
40 case NFTNL_EXPR_FIB_DREG:
41 memcpy(&fib->dreg, data,
sizeof(fib->dreg));
43 case NFTNL_EXPR_FIB_FLAGS:
44 memcpy(&fib->flags, data,
sizeof(fib->flags));
53nftnl_expr_fib_get(
const struct nftnl_expr *e, uint16_t result,
59 case NFTNL_EXPR_FIB_RESULT:
60 *data_len =
sizeof(fib->result);
62 case NFTNL_EXPR_FIB_DREG:
63 *data_len =
sizeof(fib->dreg);
65 case NFTNL_EXPR_FIB_FLAGS:
66 *data_len =
sizeof(fib->flags);
72static int nftnl_expr_fib_cb(
const struct nlattr *attr,
void *data)
74 const struct nlattr **tb = data;
75 int type = mnl_attr_get_type(attr);
77 if (mnl_attr_type_valid(attr, NFTA_FIB_MAX) < 0)
84 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
94nftnl_expr_fib_build(
struct nlmsghdr *nlh,
const struct nftnl_expr *e)
98 if (e->flags & (1 << NFTNL_EXPR_FIB_FLAGS))
99 mnl_attr_put_u32(nlh, NFTA_FIB_FLAGS, htonl(fib->flags));
100 if (e->flags & (1 << NFTNL_EXPR_FIB_RESULT))
101 mnl_attr_put_u32(nlh, NFTA_FIB_RESULT, htonl(fib->result));
102 if (e->flags & (1 << NFTNL_EXPR_FIB_DREG))
103 mnl_attr_put_u32(nlh, NFTA_FIB_DREG, htonl(fib->dreg));
107nftnl_expr_fib_parse(
struct nftnl_expr *e,
struct nlattr *attr)
110 struct nlattr *tb[NFTA_FIB_MAX+1] = {};
113 if (mnl_attr_parse_nested(attr, nftnl_expr_fib_cb, tb) < 0)
116 if (tb[NFTA_FIB_RESULT]) {
117 fib->result = ntohl(mnl_attr_get_u32(tb[NFTA_FIB_RESULT]));
118 e->flags |= (1 << NFTNL_EXPR_FIB_RESULT);
120 if (tb[NFTA_FIB_DREG]) {
121 fib->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_FIB_DREG]));
122 e->flags |= (1 << NFTNL_EXPR_FIB_DREG);
124 if (tb[NFTA_FIB_FLAGS]) {
125 fib->flags = ntohl(mnl_attr_get_u32(tb[NFTA_FIB_FLAGS]));
126 e->flags |= (1 << NFTNL_EXPR_FIB_FLAGS);
131static const char *fib_type[NFT_FIB_RESULT_MAX + 1] = {
132 [NFT_FIB_RESULT_OIF] =
"oif",
133 [NFT_FIB_RESULT_OIFNAME] =
"oifname",
134 [NFT_FIB_RESULT_ADDRTYPE] =
"type",
137static const char *fib_type_str(
enum nft_fib_result r)
139 if (r <= NFT_FIB_RESULT_MAX)
146nftnl_expr_fib_snprintf(
char *buf,
size_t remain,
147 uint32_t printflags,
const struct nftnl_expr *e)
150 uint32_t flags = fib->flags & ~NFTA_FIB_F_PRESENT;
151 uint32_t present_flag = fib->flags & NFTA_FIB_F_PRESENT;
152 int offset = 0, ret, i;
153 static const struct {
157 { NFTA_FIB_F_SADDR,
"saddr" },
158 { NFTA_FIB_F_DADDR,
"daddr" },
159 { NFTA_FIB_F_MARK,
"mark" },
160 { NFTA_FIB_F_IIF,
"iif" },
161 { NFTA_FIB_F_OIF,
"oif" },
164 for (i = 0; i < (
sizeof(tab) /
sizeof(tab[0])); i++) {
165 if (flags & tab[i].bit) {
166 ret = snprintf(buf + offset, remain,
"%s ",
168 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
170 flags &= ~tab[i].bit;
172 ret = snprintf(buf + offset, remain,
". ");
173 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
179 ret = snprintf(buf + offset, remain,
"unknown 0x%" PRIx32,
181 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
184 ret = snprintf(buf + offset, remain,
"%s%s => reg %d ",
185 fib_type_str(fib->result),
186 present_flag ?
" present" :
"",
188 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
193struct expr_ops expr_ops_fib = {
196 .max_attr = NFTA_FIB_MAX,
197 .set = nftnl_expr_fib_set,
198 .get = nftnl_expr_fib_get,
199 .parse = nftnl_expr_fib_parse,
200 .build = nftnl_expr_fib_build,
201 .output = nftnl_expr_fib_snprintf,