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>
25 #define NFT_META_MAX (NFT_META_CGROUP + 1)
29 enum nft_meta_keys key;
30 enum nft_registers dreg;
31 enum nft_registers sreg;
35 nftnl_expr_meta_set(
struct nftnl_expr *e, uint16_t type,
36 const void *data, uint32_t data_len)
41 case NFTNL_EXPR_META_KEY:
42 meta->key = *((uint32_t *)data);
44 case NFTNL_EXPR_META_DREG:
45 meta->dreg = *((uint32_t *)data);
47 case NFTNL_EXPR_META_SREG:
48 meta->sreg = *((uint32_t *)data);
57 nftnl_expr_meta_get(
const struct nftnl_expr *e, uint16_t type,
63 case NFTNL_EXPR_META_KEY:
64 *data_len =
sizeof(meta->key);
66 case NFTNL_EXPR_META_DREG:
67 *data_len =
sizeof(meta->dreg);
69 case NFTNL_EXPR_META_SREG:
70 *data_len =
sizeof(meta->sreg);
76 static int nftnl_expr_meta_cb(
const struct nlattr *attr,
void *data)
78 const struct nlattr **tb = data;
79 int type = mnl_attr_get_type(attr);
81 if (mnl_attr_type_valid(attr, NFTA_META_MAX) < 0)
88 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
98 nftnl_expr_meta_build(
struct nlmsghdr *nlh,
struct nftnl_expr *e)
102 if (e->flags & (1 << NFTNL_EXPR_META_KEY))
103 mnl_attr_put_u32(nlh, NFTA_META_KEY, htonl(meta->key));
104 if (e->flags & (1 << NFTNL_EXPR_META_DREG))
105 mnl_attr_put_u32(nlh, NFTA_META_DREG, htonl(meta->dreg));
106 if (e->flags & (1 << NFTNL_EXPR_META_SREG))
107 mnl_attr_put_u32(nlh, NFTA_META_SREG, htonl(meta->sreg));
111 nftnl_expr_meta_parse(
struct nftnl_expr *e,
struct nlattr *attr)
114 struct nlattr *tb[NFTA_META_MAX+1] = {};
116 if (mnl_attr_parse_nested(attr, nftnl_expr_meta_cb, tb) < 0)
119 if (tb[NFTA_META_KEY]) {
120 meta->key = ntohl(mnl_attr_get_u32(tb[NFTA_META_KEY]));
121 e->flags |= (1 << NFTNL_EXPR_META_KEY);
123 if (tb[NFTA_META_DREG]) {
124 meta->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_META_DREG]));
125 e->flags |= (1 << NFTNL_EXPR_META_DREG);
127 if (tb[NFTA_META_SREG]) {
128 meta->sreg = ntohl(mnl_attr_get_u32(tb[NFTA_META_SREG]));
129 e->flags |= (1 << NFTNL_EXPR_META_SREG);
135 static const char *meta_key2str_array[NFT_META_MAX] = {
136 [NFT_META_LEN] =
"len",
137 [NFT_META_PROTOCOL] =
"protocol",
138 [NFT_META_NFPROTO] =
"nfproto",
139 [NFT_META_L4PROTO] =
"l4proto",
140 [NFT_META_PRIORITY] =
"priority",
141 [NFT_META_MARK] =
"mark",
142 [NFT_META_IIF] =
"iif",
143 [NFT_META_OIF] =
"oif",
144 [NFT_META_IIFNAME] =
"iifname",
145 [NFT_META_OIFNAME] =
"oifname",
146 [NFT_META_IIFTYPE] =
"iiftype",
147 [NFT_META_OIFTYPE] =
"oiftype",
148 [NFT_META_SKUID] =
"skuid",
149 [NFT_META_SKGID] =
"skgid",
150 [NFT_META_NFTRACE] =
"nftrace",
151 [NFT_META_RTCLASSID] =
"rtclassid",
152 [NFT_META_SECMARK] =
"secmark",
153 [NFT_META_BRI_IIFNAME] =
"bri_iifname",
154 [NFT_META_BRI_OIFNAME] =
"bri_oifname",
155 [NFT_META_PKTTYPE] =
"pkttype",
156 [NFT_META_CPU] =
"cpu",
157 [NFT_META_IIFGROUP] =
"iifgroup",
158 [NFT_META_OIFGROUP] =
"oifgroup",
159 [NFT_META_CGROUP] =
"cgroup",
162 static const char *meta_key2str(uint8_t key)
164 if (key < NFT_META_MAX)
165 return meta_key2str_array[key];
170 static inline int str2meta_key(
const char *str)
174 for (i = 0; i < NFT_META_MAX; i++) {
175 if (strcmp(str, meta_key2str_array[i]) == 0)
183 static int nftnl_expr_meta_json_parse(
struct nftnl_expr *e, json_t *root,
184 struct nftnl_parse_err *err)
191 key_str = nftnl_jansson_parse_str(root,
"key", err);
192 if (key_str != NULL) {
193 key = str2meta_key(key_str);
195 nftnl_expr_set_u32(e, NFTNL_EXPR_META_KEY, key);
198 if (nftnl_jansson_node_exist(root,
"dreg")) {
199 if (nftnl_jansson_parse_reg(root,
"dreg", NFTNL_TYPE_U32, ®,
201 nftnl_expr_set_u32(e, NFTNL_EXPR_META_DREG, reg);
204 if (nftnl_jansson_node_exist(root,
"sreg")) {
205 if (nftnl_jansson_parse_reg(root,
"sreg", NFTNL_TYPE_U32, ®,
207 nftnl_expr_set_u32(e, NFTNL_EXPR_META_SREG, reg);
218 static int nftnl_expr_meta_xml_parse(
struct nftnl_expr *e, mxml_node_t *tree,
219 struct nftnl_parse_err *err)
226 key_str = nftnl_mxml_str_parse(tree,
"key", MXML_DESCEND_FIRST,
227 NFTNL_XML_MAND, err);
228 if (key_str != NULL) {
229 key = str2meta_key(key_str);
231 nftnl_expr_set_u32(e, NFTNL_EXPR_META_KEY, key);
234 if (nftnl_mxml_reg_parse(tree,
"dreg", &dreg, MXML_DESCEND_FIRST,
235 NFTNL_XML_OPT, err) == 0)
236 nftnl_expr_set_u32(e, NFTNL_EXPR_META_DREG, dreg);
238 if (nftnl_mxml_reg_parse(tree,
"sreg", &sreg, MXML_DESCEND_FIRST,
239 NFTNL_XML_OPT, err) == 0)
240 nftnl_expr_set_u32(e, NFTNL_EXPR_META_SREG, sreg);
250 nftnl_expr_meta_snprintf_default(
char *buf,
size_t len,
251 struct nftnl_expr *e)
255 if (e->flags & (1 << NFTNL_EXPR_META_SREG)) {
256 return snprintf(buf, len,
"set %s with reg %u ",
257 meta_key2str(meta->key), meta->sreg);
259 if (e->flags & (1 << NFTNL_EXPR_META_DREG)) {
260 return snprintf(buf, len,
"load %s => reg %u ",
261 meta_key2str(meta->key), meta->dreg);
266 static int nftnl_expr_meta_export(
char *buf,
size_t size,
267 struct nftnl_expr *e,
int type)
270 NFTNL_BUF_INIT(b, buf, size);
272 if (e->flags & (1 << NFTNL_EXPR_META_DREG))
273 nftnl_buf_u32(&b, type, meta->dreg, DREG);
274 if (e->flags & (1 << NFTNL_EXPR_META_KEY))
275 nftnl_buf_str(&b, type, meta_key2str(meta->key), KEY);
276 if (e->flags & (1 << NFTNL_EXPR_META_SREG))
277 nftnl_buf_u32(&b, type, meta->sreg, SREG);
279 return nftnl_buf_done(&b);
283 nftnl_expr_meta_snprintf(
char *buf,
size_t len, uint32_t type,
284 uint32_t flags,
struct nftnl_expr *e)
287 case NFTNL_OUTPUT_DEFAULT:
288 return nftnl_expr_meta_snprintf_default(buf, len, e);
289 case NFTNL_OUTPUT_XML:
290 case NFTNL_OUTPUT_JSON:
291 return nftnl_expr_meta_export(buf, len, e, type);
298 struct expr_ops expr_ops_meta = {
301 .max_attr = NFTA_META_MAX,
302 .set = nftnl_expr_meta_set,
303 .get = nftnl_expr_meta_get,
304 .parse = nftnl_expr_meta_parse,
305 .build = nftnl_expr_meta_build,
306 .snprintf = nftnl_expr_meta_snprintf,
307 .xml_parse = nftnl_expr_meta_xml_parse,
308 .json_parse = nftnl_expr_meta_json_parse,