libnftnl 1.3.2
cmp.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
4 *
5 * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
6 */
7
8#include "internal.h"
9
10#include <stdio.h>
11#include <stdint.h>
12#include <string.h>
13#include <arpa/inet.h>
14#include <errno.h>
15
16#include <libmnl/libmnl.h>
17#include <linux/netfilter/nf_tables.h>
18#include <libnftnl/expr.h>
19#include <libnftnl/rule.h>
20
22 union nftnl_data_reg data;
23 enum nft_registers sreg;
24 enum nft_cmp_ops op;
25};
26
27static int
28nftnl_expr_cmp_set(struct nftnl_expr *e, uint16_t type,
29 const void *data, uint32_t data_len, uint32_t byteorder)
30{
31 struct nftnl_expr_cmp *cmp = nftnl_expr_data(e);
32
33 switch(type) {
34 case NFTNL_EXPR_CMP_SREG:
35 memcpy(&cmp->sreg, data, data_len);
36 break;
37 case NFTNL_EXPR_CMP_OP:
38 memcpy(&cmp->op, data, data_len);
39 break;
40 case NFTNL_EXPR_CMP_DATA:
41 return nftnl_data_cpy(&cmp->data, data,
42 data_len, byteorder, NULL);
43 }
44 return 0;
45}
46
47static const void *
48nftnl_expr_cmp_get(const struct nftnl_expr *e, uint16_t type,
49 uint32_t *data_len)
50{
51 struct nftnl_expr_cmp *cmp = nftnl_expr_data(e);
52
53 switch(type) {
54 case NFTNL_EXPR_CMP_SREG:
55 *data_len = sizeof(cmp->sreg);
56 return &cmp->sreg;
57 case NFTNL_EXPR_CMP_OP:
58 *data_len = sizeof(cmp->op);
59 return &cmp->op;
60 case NFTNL_EXPR_CMP_DATA:
61 *data_len = cmp->data.len;
62 return &cmp->data.val;
63 }
64 return NULL;
65}
66
67static int nftnl_expr_cmp_cb(const struct nlattr *attr, void *data)
68{
69 const struct nlattr **tb = data;
70 int type = mnl_attr_get_type(attr);
71
72 if (mnl_attr_type_valid(attr, NFTA_CMP_MAX) < 0)
73 return MNL_CB_OK;
74
75 switch(type) {
76 case NFTA_CMP_SREG:
77 case NFTA_CMP_OP:
78 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
79 abi_breakage();
80 break;
81 case NFTA_CMP_DATA:
82 if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0)
83 abi_breakage();
84 break;
85 }
86
87 tb[type] = attr;
88 return MNL_CB_OK;
89}
90
91static void
92nftnl_expr_cmp_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
93{
94 struct nftnl_expr_cmp *cmp = nftnl_expr_data(e);
95
96 if (e->flags & (1 << NFTNL_EXPR_CMP_SREG))
97 mnl_attr_put_u32(nlh, NFTA_CMP_SREG, htonl(cmp->sreg));
98 if (e->flags & (1 << NFTNL_EXPR_CMP_OP))
99 mnl_attr_put_u32(nlh, NFTA_CMP_OP, htonl(cmp->op));
100 if (e->flags & (1 << NFTNL_EXPR_CMP_DATA)) {
101 struct nlattr *nest;
102
103 nest = mnl_attr_nest_start(nlh, NFTA_CMP_DATA);
104 mnl_attr_put(nlh, NFTA_DATA_VALUE, cmp->data.len, cmp->data.val);
105 mnl_attr_nest_end(nlh, nest);
106 }
107}
108
109static int
110nftnl_expr_cmp_parse(struct nftnl_expr *e, struct nlattr *attr)
111{
112 struct nftnl_expr_cmp *cmp = nftnl_expr_data(e);
113 struct nlattr *tb[NFTA_CMP_MAX+1] = {};
114 int ret = 0;
115
116 if (mnl_attr_parse_nested(attr, nftnl_expr_cmp_cb, tb) < 0)
117 return -1;
118
119 if (tb[NFTA_CMP_SREG]) {
120 cmp->sreg = ntohl(mnl_attr_get_u32(tb[NFTA_CMP_SREG]));
121 e->flags |= (1 << NFTA_CMP_SREG);
122 }
123 if (tb[NFTA_CMP_OP]) {
124 cmp->op = ntohl(mnl_attr_get_u32(tb[NFTA_CMP_OP]));
125 e->flags |= (1 << NFTA_CMP_OP);
126 }
127 if (tb[NFTA_CMP_DATA]) {
128 ret = nftnl_parse_data(&cmp->data, tb[NFTA_CMP_DATA], NULL);
129 e->flags |= (1 << NFTA_CMP_DATA);
130 }
131
132 return ret;
133}
134
135static const char *expr_cmp_str[] = {
136 [NFT_CMP_EQ] = "eq",
137 [NFT_CMP_NEQ] = "neq",
138 [NFT_CMP_LT] = "lt",
139 [NFT_CMP_LTE] = "lte",
140 [NFT_CMP_GT] = "gt",
141 [NFT_CMP_GTE] = "gte",
142};
143
144static const char *cmp2str(uint32_t op)
145{
146 if (op > NFT_CMP_GTE)
147 return "unknown";
148
149 return expr_cmp_str[op];
150}
151
152static int
153nftnl_expr_cmp_snprintf(char *buf, size_t remain,
154 uint32_t flags, const struct nftnl_expr *e)
155{
156 struct nftnl_expr_cmp *cmp = nftnl_expr_data(e);
157 int offset = 0, ret;
158
159 ret = snprintf(buf, remain, "%s reg %u ",
160 cmp2str(cmp->op), cmp->sreg);
161 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
162
163 ret = nftnl_data_reg_snprintf(buf + offset, remain, &cmp->data,
164 0, DATA_VALUE);
165 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
166
167 ret = snprintf(buf + offset, remain, " ");
168 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
169
170 return offset;
171}
172
173static struct attr_policy cmp_attr_policy[__NFTNL_EXPR_CMP_MAX] = {
174 [NFTNL_EXPR_CMP_SREG] = { .maxlen = sizeof(uint32_t) },
175 [NFTNL_EXPR_CMP_OP] = { .maxlen = sizeof(uint32_t) },
176 [NFTNL_EXPR_CMP_DATA] = { .maxlen = NFT_DATA_VALUE_MAXLEN }
177};
178
179struct expr_ops expr_ops_cmp = {
180 .name = "cmp",
181 .alloc_len = sizeof(struct nftnl_expr_cmp),
182 .nftnl_max_attr = __NFTNL_EXPR_CMP_MAX - 1,
183 .attr_policy = cmp_attr_policy,
184 .set = nftnl_expr_cmp_set,
185 .get = nftnl_expr_cmp_get,
186 .parse = nftnl_expr_cmp_parse,
187 .build = nftnl_expr_cmp_build,
188 .output = nftnl_expr_cmp_snprintf,
189};