15 #include <arpa/inet.h>
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;
31 nftnl_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 fib->result = *((uint32_t *)data);
40 case NFTNL_EXPR_FIB_DREG:
41 fib->dreg = *((uint32_t *)data);
43 case NFTNL_EXPR_FIB_FLAGS:
44 fib->flags = *((uint32_t *)data);
53 nftnl_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);
72 static 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)
94 nftnl_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));
107 nftnl_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);
131 static int nftnl_expr_fib_json_parse(
struct nftnl_expr *e, json_t *root,
132 struct nftnl_parse_err *err)
135 uint32_t result, flags, dreg;
137 if (nftnl_jansson_parse_reg(root,
"result", NFTNL_TYPE_U32,
139 nftnl_expr_set_u32(e, NFTNL_EXPR_FIB_RESULT, result);
141 if (nftnl_jansson_parse_reg(root,
"dreg", NFTNL_TYPE_U32,
143 nftnl_expr_set_u32(e, NFTNL_EXPR_FIB_DREG, dreg);
145 if (nftnl_jansson_parse_val(root,
"flags", NFTNL_TYPE_U32,
147 nftnl_expr_set_u32(e, NFTNL_EXPR_FIB_FLAGS, flags);
156 static const char *fib_type[NFT_FIB_RESULT_MAX + 1] = {
157 [NFT_FIB_RESULT_OIF] =
"oif",
158 [NFT_FIB_RESULT_OIFNAME] =
"oifname",
159 [NFT_FIB_RESULT_ADDRTYPE] =
"type",
162 static const char *fib_type_str(
enum nft_fib_result r)
164 if (r <= NFT_FIB_RESULT_MAX)
171 nftnl_expr_fib_snprintf_default(
char *buf,
size_t size,
172 const struct nftnl_expr *e)
175 int remain = size, offset = 0, ret, i;
176 uint32_t flags = fib->flags & ~NFTA_FIB_F_PRESENT;
177 uint32_t present_flag = fib->flags & NFTA_FIB_F_PRESENT;
178 static const struct {
182 { NFTA_FIB_F_SADDR,
"saddr" },
183 { NFTA_FIB_F_DADDR,
"daddr" },
184 { NFTA_FIB_F_MARK,
"mark" },
185 { NFTA_FIB_F_IIF,
"iif" },
186 { NFTA_FIB_F_OIF,
"oif" },
189 for (i = 0; i < (
sizeof(tab) /
sizeof(tab[0])); i++) {
190 if (flags & tab[i].bit) {
191 ret = snprintf(buf + offset, remain,
"%s ",
193 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
195 flags &= ~tab[i].bit;
197 ret = snprintf(buf + offset, remain,
". ");
198 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
204 ret = snprintf(buf + offset, remain,
"unknown 0x%" PRIx32,
206 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
209 ret = snprintf(buf + offset, remain,
"%s%s => reg %d ",
210 fib_type_str(fib->result),
211 present_flag ?
" present" :
"",
213 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
218 static int nftnl_expr_fib_export(
char *buf,
size_t size,
219 const struct nftnl_expr *e,
int type)
223 NFTNL_BUF_INIT(b, buf, size);
225 if (e->flags & (1 << NFTNL_EXPR_FIB_RESULT))
226 nftnl_buf_u32(&b, type, fib->result, OP);
227 if (e->flags & (1 << NFTNL_EXPR_FIB_DREG))
228 nftnl_buf_u32(&b, type, fib->dreg, DREG);
229 if (e->flags & (1 << NFTNL_EXPR_FIB_FLAGS))
230 nftnl_buf_u32(&b, type, fib->flags, FLAGS);
232 return nftnl_buf_done(&b);
236 nftnl_expr_fib_snprintf(
char *buf,
size_t len, uint32_t type,
237 uint32_t flags,
const struct nftnl_expr *e)
240 case NFTNL_OUTPUT_DEFAULT:
241 return nftnl_expr_fib_snprintf_default(buf, len, e);
242 case NFTNL_OUTPUT_XML:
243 case NFTNL_OUTPUT_JSON:
244 return nftnl_expr_fib_export(buf, len, e, type);
251 static bool nftnl_expr_fib_cmp(
const struct nftnl_expr *e1,
252 const struct nftnl_expr *e2)
258 if (e1->flags & (1 << NFTNL_EXPR_FIB_RESULT))
259 eq &= (h1->result == h2->result);
260 if (e1->flags & (1 << NFTNL_EXPR_FIB_DREG))
261 eq &= (h1->dreg == h2->dreg);
262 if (e1->flags & (1 << NFTNL_EXPR_FIB_FLAGS))
263 eq &= (h1->flags == h2->flags);
268 struct expr_ops expr_ops_fib = {
271 .max_attr = NFTA_FIB_MAX,
272 .cmp = nftnl_expr_fib_cmp,
273 .set = nftnl_expr_fib_set,
274 .get = nftnl_expr_fib_get,
275 .parse = nftnl_expr_fib_parse,
276 .build = nftnl_expr_fib_build,
277 .snprintf = nftnl_expr_fib_snprintf,
278 .json_parse = nftnl_expr_fib_json_parse,