13#include <linux/netfilter/nf_tables.h>
16#include <libmnl/libmnl.h>
17#include <libnftnl/expr.h>
18#include <libnftnl/rule.h>
22 enum nft_registers dreg;
23 enum nft_registers sreg;
27#define IP_CT_DIR_ORIGINAL 0
28#define IP_CT_DIR_REPLY 1
31nftnl_expr_ct_set(
struct nftnl_expr *e, uint16_t type,
32 const void *data, uint32_t data_len)
37 case NFTNL_EXPR_CT_KEY:
38 memcpy(&ct->key, data, data_len);
40 case NFTNL_EXPR_CT_DIR:
41 memcpy(&ct->dir, data, data_len);
43 case NFTNL_EXPR_CT_DREG:
44 memcpy(&ct->dreg, data, data_len);
46 case NFTNL_EXPR_CT_SREG:
47 memcpy(&ct->sreg, data, data_len);
54nftnl_expr_ct_get(
const struct nftnl_expr *e, uint16_t type,
60 case NFTNL_EXPR_CT_KEY:
61 *data_len =
sizeof(ct->key);
63 case NFTNL_EXPR_CT_DIR:
64 *data_len =
sizeof(ct->dir);
66 case NFTNL_EXPR_CT_DREG:
67 *data_len =
sizeof(ct->dreg);
69 case NFTNL_EXPR_CT_SREG:
70 *data_len =
sizeof(ct->sreg);
76static int nftnl_expr_ct_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_CT_MAX) < 0)
88 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
91 case NFTA_CT_DIRECTION:
92 if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
102nftnl_expr_ct_build(
struct nlmsghdr *nlh,
const struct nftnl_expr *e)
106 if (e->flags & (1 << NFTNL_EXPR_CT_KEY))
107 mnl_attr_put_u32(nlh, NFTA_CT_KEY, htonl(ct->key));
108 if (e->flags & (1 << NFTNL_EXPR_CT_DREG))
109 mnl_attr_put_u32(nlh, NFTA_CT_DREG, htonl(ct->dreg));
110 if (e->flags & (1 << NFTNL_EXPR_CT_DIR))
111 mnl_attr_put_u8(nlh, NFTA_CT_DIRECTION, ct->dir);
112 if (e->flags & (1 << NFTNL_EXPR_CT_SREG))
113 mnl_attr_put_u32(nlh, NFTA_CT_SREG, htonl(ct->sreg));
117nftnl_expr_ct_parse(
struct nftnl_expr *e,
struct nlattr *attr)
120 struct nlattr *tb[NFTA_CT_MAX+1] = {};
122 if (mnl_attr_parse_nested(attr, nftnl_expr_ct_cb, tb) < 0)
125 if (tb[NFTA_CT_KEY]) {
126 ct->key = ntohl(mnl_attr_get_u32(tb[NFTA_CT_KEY]));
127 e->flags |= (1 << NFTNL_EXPR_CT_KEY);
129 if (tb[NFTA_CT_DREG]) {
130 ct->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_CT_DREG]));
131 e->flags |= (1 << NFTNL_EXPR_CT_DREG);
133 if (tb[NFTA_CT_SREG]) {
134 ct->sreg = ntohl(mnl_attr_get_u32(tb[NFTA_CT_SREG]));
135 e->flags |= (1 << NFTNL_EXPR_CT_SREG);
137 if (tb[NFTA_CT_DIRECTION]) {
138 ct->dir = mnl_attr_get_u8(tb[NFTA_CT_DIRECTION]);
139 e->flags |= (1 << NFTNL_EXPR_CT_DIR);
145static const char *ctkey2str_array[NFT_CT_MAX + 1] = {
146 [NFT_CT_STATE] =
"state",
147 [NFT_CT_DIRECTION] =
"direction",
148 [NFT_CT_STATUS] =
"status",
149 [NFT_CT_MARK] =
"mark",
150 [NFT_CT_SECMARK] =
"secmark",
151 [NFT_CT_EXPIRATION] =
"expiration",
152 [NFT_CT_HELPER] =
"helper",
153 [NFT_CT_L3PROTOCOL] =
"l3protocol",
154 [NFT_CT_PROTOCOL] =
"protocol",
155 [NFT_CT_SRC] =
"src",
156 [NFT_CT_DST] =
"dst",
157 [NFT_CT_PROTO_SRC] =
"proto_src",
158 [NFT_CT_PROTO_DST] =
"proto_dst",
159 [NFT_CT_LABELS] =
"label",
160 [NFT_CT_PKTS] =
"packets",
161 [NFT_CT_BYTES] =
"bytes",
162 [NFT_CT_AVGPKT] =
"avgpkt",
163 [NFT_CT_ZONE] =
"zone",
164 [NFT_CT_EVENTMASK] =
"event",
165 [NFT_CT_SRC_IP] =
"src_ip",
166 [NFT_CT_DST_IP] =
"dst_ip",
167 [NFT_CT_SRC_IP6] =
"src_ip6",
168 [NFT_CT_DST_IP6] =
"dst_ip6",
172static const char *ctkey2str(uint32_t ctkey)
174 if (ctkey > NFT_CT_MAX)
177 return ctkey2str_array[ctkey];
180static const char *ctdir2str(uint8_t ctdir)
183 case IP_CT_DIR_ORIGINAL:
185 case IP_CT_DIR_REPLY:
193nftnl_expr_ct_snprintf(
char *buf,
size_t remain,
194 uint32_t flags,
const struct nftnl_expr *e)
199 if (e->flags & (1 << NFTNL_EXPR_CT_SREG)) {
200 ret = snprintf(buf, remain,
"set %s with reg %u ",
201 ctkey2str(ct->key), ct->sreg);
202 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
205 if (e->flags & (1 << NFTNL_EXPR_CT_DREG)) {
206 ret = snprintf(buf, remain,
"load %s => reg %u ",
207 ctkey2str(ct->key), ct->dreg);
208 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
211 if (nftnl_expr_is_set(e, NFTNL_EXPR_CT_DIR)) {
212 ret = snprintf(buf + offset, remain,
", dir %s ",
214 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
220static struct attr_policy ct_attr_policy[__NFTNL_EXPR_CT_MAX] = {
221 [NFTNL_EXPR_CT_DREG] = { .maxlen =
sizeof(uint32_t) },
222 [NFTNL_EXPR_CT_KEY] = { .maxlen =
sizeof(uint32_t) },
223 [NFTNL_EXPR_CT_DIR] = { .maxlen =
sizeof(uint8_t) },
224 [NFTNL_EXPR_CT_SREG] = { .maxlen =
sizeof(uint32_t) },
227struct expr_ops expr_ops_ct = {
230 .nftnl_max_attr = __NFTNL_EXPR_CT_MAX - 1,
231 .attr_policy = ct_attr_policy,
232 .set = nftnl_expr_ct_set,
233 .get = nftnl_expr_ct_get,
234 .parse = nftnl_expr_ct_parse,
235 .build = nftnl_expr_ct_build,
236 .output = nftnl_expr_ct_snprintf,