1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/netlink.h>
12 #include <linux/netfilter.h>
13 #include <linux/if_arp.h>
14 #include <linux/netfilter/nf_tables.h>
15 #include <net/netfilter/nf_tables_core.h>
16 #include <net/netfilter/nf_tables_offload.h>
17 #include <net/netfilter/nf_tables.h>
23 enum nft_cmp_ops op:8;
26 void nft_cmp_eval(const struct nft_expr *expr,
27 struct nft_regs *regs,
28 const struct nft_pktinfo *pkt)
30 const struct nft_cmp_expr *priv = nft_expr_priv(expr);
33 d = memcmp(®s->data[priv->sreg], &priv->data, priv->len);
63 regs->verdict.code = NFT_BREAK;
66 static const struct nla_policy nft_cmp_policy[NFTA_CMP_MAX + 1] = {
67 [NFTA_CMP_SREG] = { .type = NLA_U32 },
68 [NFTA_CMP_OP] = { .type = NLA_U32 },
69 [NFTA_CMP_DATA] = { .type = NLA_NESTED },
72 static int nft_cmp_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
73 const struct nlattr * const tb[])
75 struct nft_cmp_expr *priv = nft_expr_priv(expr);
76 struct nft_data_desc desc;
79 err = nft_data_init(NULL, &priv->data, sizeof(priv->data), &desc,
84 if (desc.type != NFT_DATA_VALUE) {
86 nft_data_release(&priv->data, desc.type);
90 err = nft_parse_register_load(tb[NFTA_CMP_SREG], &priv->sreg, desc.len);
94 priv->op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
99 static int nft_cmp_dump(struct sk_buff *skb, const struct nft_expr *expr)
101 const struct nft_cmp_expr *priv = nft_expr_priv(expr);
103 if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
104 goto nla_put_failure;
105 if (nla_put_be32(skb, NFTA_CMP_OP, htonl(priv->op)))
106 goto nla_put_failure;
108 if (nft_data_dump(skb, NFTA_CMP_DATA, &priv->data,
109 NFT_DATA_VALUE, priv->len) < 0)
110 goto nla_put_failure;
117 static int __nft_cmp_offload(struct nft_offload_ctx *ctx,
118 struct nft_flow_rule *flow,
119 const struct nft_cmp_expr *priv)
121 struct nft_offload_reg *reg = &ctx->regs[priv->sreg];
122 u8 *mask = (u8 *)&flow->match.mask;
123 u8 *key = (u8 *)&flow->match.key;
125 if (priv->op != NFT_CMP_EQ || priv->len > reg->len)
128 memcpy(key + reg->offset, &priv->data, reg->len);
129 memcpy(mask + reg->offset, ®->mask, reg->len);
131 flow->match.dissector.used_keys |= BIT(reg->key);
132 flow->match.dissector.offset[reg->key] = reg->base_offset;
134 if (reg->key == FLOW_DISSECTOR_KEY_META &&
135 reg->offset == offsetof(struct nft_flow_key, meta.ingress_iftype) &&
136 nft_reg_load16(priv->data.data) != ARPHRD_ETHER)
139 nft_offload_update_dependency(ctx, &priv->data, reg->len);
144 static int nft_cmp_offload(struct nft_offload_ctx *ctx,
145 struct nft_flow_rule *flow,
146 const struct nft_expr *expr)
148 const struct nft_cmp_expr *priv = nft_expr_priv(expr);
150 return __nft_cmp_offload(ctx, flow, priv);
153 static const struct nft_expr_ops nft_cmp_ops = {
154 .type = &nft_cmp_type,
155 .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp_expr)),
156 .eval = nft_cmp_eval,
157 .init = nft_cmp_init,
158 .dump = nft_cmp_dump,
159 .offload = nft_cmp_offload,
162 static int nft_cmp_fast_init(const struct nft_ctx *ctx,
163 const struct nft_expr *expr,
164 const struct nlattr * const tb[])
166 struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
167 struct nft_data_desc desc;
168 struct nft_data data;
171 err = nft_data_init(NULL, &data, sizeof(data), &desc,
176 err = nft_parse_register_load(tb[NFTA_CMP_SREG], &priv->sreg, desc.len);
180 desc.len *= BITS_PER_BYTE;
182 priv->mask = nft_cmp_fast_mask(desc.len);
183 priv->data = data.data[0] & priv->mask;
184 priv->len = desc.len;
185 priv->inv = ntohl(nla_get_be32(tb[NFTA_CMP_OP])) != NFT_CMP_EQ;
189 static int nft_cmp_fast_offload(struct nft_offload_ctx *ctx,
190 struct nft_flow_rule *flow,
191 const struct nft_expr *expr)
193 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
194 struct nft_cmp_expr cmp = {
201 .len = priv->len / BITS_PER_BYTE,
202 .op = priv->inv ? NFT_CMP_NEQ : NFT_CMP_EQ,
205 return __nft_cmp_offload(ctx, flow, &cmp);
208 static int nft_cmp_fast_dump(struct sk_buff *skb, const struct nft_expr *expr)
210 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
211 enum nft_cmp_ops op = priv->inv ? NFT_CMP_NEQ : NFT_CMP_EQ;
212 struct nft_data data;
214 if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
215 goto nla_put_failure;
216 if (nla_put_be32(skb, NFTA_CMP_OP, htonl(op)))
217 goto nla_put_failure;
219 data.data[0] = priv->data;
220 if (nft_data_dump(skb, NFTA_CMP_DATA, &data,
221 NFT_DATA_VALUE, priv->len / BITS_PER_BYTE) < 0)
222 goto nla_put_failure;
229 const struct nft_expr_ops nft_cmp_fast_ops = {
230 .type = &nft_cmp_type,
231 .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp_fast_expr)),
232 .eval = NULL, /* inlined */
233 .init = nft_cmp_fast_init,
234 .dump = nft_cmp_fast_dump,
235 .offload = nft_cmp_fast_offload,
238 static const struct nft_expr_ops *
239 nft_cmp_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
241 struct nft_data_desc desc;
242 struct nft_data data;
246 if (tb[NFTA_CMP_SREG] == NULL ||
247 tb[NFTA_CMP_OP] == NULL ||
248 tb[NFTA_CMP_DATA] == NULL)
249 return ERR_PTR(-EINVAL);
251 op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
261 return ERR_PTR(-EINVAL);
264 err = nft_data_init(NULL, &data, sizeof(data), &desc,
269 if (desc.type != NFT_DATA_VALUE)
272 if (desc.len <= sizeof(u32) && (op == NFT_CMP_EQ || op == NFT_CMP_NEQ))
273 return &nft_cmp_fast_ops;
277 nft_data_release(&data, desc.type);
278 return ERR_PTR(-EINVAL);
281 struct nft_expr_type nft_cmp_type __read_mostly = {
283 .select_ops = nft_cmp_select_ops,
284 .policy = nft_cmp_policy,
285 .maxattr = NFTA_CMP_MAX,
286 .owner = THIS_MODULE,