nft_lookup.c 5.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2009 Patrick McHardy <kaber@trash.net>
 *
 * Development of this code funded by Astaro AG (http://www.astaro.com/)
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/rbtree.h>
#include <linux/netlink.h>
#include <linux/netfilter.h>
#include <linux/netfilter/nf_tables.h>
#include <net/netfilter/nf_tables.h>
16
#include <net/netfilter/nf_tables_core.h>
17 18 19

struct nft_lookup {
	struct nft_set			*set;
20
	u8				sreg;
21
	u8				dreg;
22
	bool				invert;
23 24 25
	struct nft_set_binding		binding;
};

26 27 28
void nft_lookup_eval(const struct nft_expr *expr,
		     struct nft_regs *regs,
		     const struct nft_pktinfo *pkt)
29 30 31
{
	const struct nft_lookup *priv = nft_expr_priv(expr);
	const struct nft_set *set = priv->set;
32
	const struct nft_set_ext *ext = NULL;
33
	bool found;
34

35 36
	found = set->ops->lookup(nft_net(pkt), set, &regs->data[priv->sreg],
				 &ext) ^ priv->invert;
37 38
	if (!found) {
		regs->verdict.code = NFT_BREAK;
39
		return;
40
	}
41

42 43 44 45
	if (ext) {
		if (set->flags & NFT_SET_MAP)
			nft_data_copy(&regs->data[priv->dreg],
				      nft_set_ext_data(ext), set->dlen);
46

47 48
		nft_set_elem_update_expr(ext, regs, pkt);
	}
49 50 51
}

static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = {
52 53
	[NFTA_LOOKUP_SET]	= { .type = NLA_STRING,
				    .len = NFT_SET_MAXNAMELEN - 1 },
54
	[NFTA_LOOKUP_SET_ID]	= { .type = NLA_U32 },
55 56
	[NFTA_LOOKUP_SREG]	= { .type = NLA_U32 },
	[NFTA_LOOKUP_DREG]	= { .type = NLA_U32 },
57
	[NFTA_LOOKUP_FLAGS]	= { .type = NLA_U32 },
58 59 60 61 62 63 64
};

static int nft_lookup_init(const struct nft_ctx *ctx,
			   const struct nft_expr *expr,
			   const struct nlattr * const tb[])
{
	struct nft_lookup *priv = nft_expr_priv(expr);
65
	u8 genmask = nft_genmask_next(ctx->net);
66
	struct nft_set *set;
67
	u32 flags;
68 69 70 71 72 73
	int err;

	if (tb[NFTA_LOOKUP_SET] == NULL ||
	    tb[NFTA_LOOKUP_SREG] == NULL)
		return -EINVAL;

74 75
	set = nft_set_lookup_global(ctx->net, ctx->table, tb[NFTA_LOOKUP_SET],
				    tb[NFTA_LOOKUP_SET_ID], genmask);
76 77
	if (IS_ERR(set))
		return PTR_ERR(set);
78

79 80
	err = nft_parse_register_load(tb[NFTA_LOOKUP_SREG], &priv->sreg,
				      set->klen);
81 82 83
	if (err < 0)
		return err;

84 85 86 87 88 89 90 91 92 93 94 95 96
	if (tb[NFTA_LOOKUP_FLAGS]) {
		flags = ntohl(nla_get_be32(tb[NFTA_LOOKUP_FLAGS]));

		if (flags & ~NFT_LOOKUP_F_INV)
			return -EINVAL;

		if (flags & NFT_LOOKUP_F_INV) {
			if (set->flags & NFT_SET_MAP)
				return -EINVAL;
			priv->invert = true;
		}
	}

97
	if (tb[NFTA_LOOKUP_DREG] != NULL) {
98 99
		if (priv->invert)
			return -EINVAL;
100 101 102
		if (!(set->flags & NFT_SET_MAP))
			return -EINVAL;

103 104 105
		err = nft_parse_register_store(ctx, tb[NFTA_LOOKUP_DREG],
					       &priv->dreg, NULL, set->dtype,
					       set->dlen);
106 107
		if (err < 0)
			return err;
108 109 110
	} else if (set->flags & NFT_SET_MAP)
		return -EINVAL;

111 112
	priv->binding.flags = set->flags & NFT_SET_MAP;

113 114 115 116 117 118 119 120
	err = nf_tables_bind_set(ctx, set, &priv->binding);
	if (err < 0)
		return err;

	priv->set = set;
	return 0;
}

121
static void nft_lookup_deactivate(const struct nft_ctx *ctx,
122 123
				  const struct nft_expr *expr,
				  enum nft_trans_phase phase)
124 125 126
{
	struct nft_lookup *priv = nft_expr_priv(expr);

127 128 129 130 131 132 133
	nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
}

static void nft_lookup_activate(const struct nft_ctx *ctx,
				const struct nft_expr *expr)
{
	struct nft_lookup *priv = nft_expr_priv(expr);
134

135
	nf_tables_activate_set(ctx, priv->set);
136 137
}

138 139
static void nft_lookup_destroy(const struct nft_ctx *ctx,
			       const struct nft_expr *expr)
140 141 142
{
	struct nft_lookup *priv = nft_expr_priv(expr);

143
	nf_tables_destroy_set(ctx, priv->set);
144 145 146 147 148
}

static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
{
	const struct nft_lookup *priv = nft_expr_priv(expr);
149
	u32 flags = priv->invert ? NFT_LOOKUP_F_INV : 0;
150 151 152

	if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
		goto nla_put_failure;
153
	if (nft_dump_register(skb, NFTA_LOOKUP_SREG, priv->sreg))
154 155
		goto nla_put_failure;
	if (priv->set->flags & NFT_SET_MAP)
156
		if (nft_dump_register(skb, NFTA_LOOKUP_DREG, priv->dreg))
157
			goto nla_put_failure;
158 159
	if (nla_put_be32(skb, NFTA_LOOKUP_FLAGS, htonl(flags)))
		goto nla_put_failure;
160 161 162 163 164 165
	return 0;

nla_put_failure:
	return -1;
}

166 167 168 169 170 171
static int nft_lookup_validate_setelem(const struct nft_ctx *ctx,
				       struct nft_set *set,
				       const struct nft_set_iter *iter,
				       struct nft_set_elem *elem)
{
	const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
172
	struct nft_ctx *pctx = (struct nft_ctx *)ctx;
173
	const struct nft_data *data;
174
	int err;
175 176 177 178 179 180 181 182 183

	if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
	    *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
		return 0;

	data = nft_set_ext_data(ext);
	switch (data->verdict.code) {
	case NFT_JUMP:
	case NFT_GOTO:
184 185 186 187 188 189
		pctx->level++;
		err = nft_chain_validate(ctx, data->verdict.chain);
		if (err < 0)
			return err;
		pctx->level--;
		break;
190
	default:
191
		break;
192
	}
193 194

	return 0;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
}

static int nft_lookup_validate(const struct nft_ctx *ctx,
			       const struct nft_expr *expr,
			       const struct nft_data **d)
{
	const struct nft_lookup *priv = nft_expr_priv(expr);
	struct nft_set_iter iter;

	if (!(priv->set->flags & NFT_SET_MAP) ||
	    priv->set->dtype != NFT_DATA_VERDICT)
		return 0;

	iter.genmask	= nft_genmask_next(ctx->net);
	iter.skip	= 0;
	iter.count	= 0;
	iter.err	= 0;
	iter.fn		= nft_lookup_validate_setelem;

	priv->set->ops->walk(ctx, priv->set, &iter);
	if (iter.err < 0)
		return iter.err;

	return 0;
}

221 222
static const struct nft_expr_ops nft_lookup_ops = {
	.type		= &nft_lookup_type,
223 224 225
	.size		= NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
	.eval		= nft_lookup_eval,
	.init		= nft_lookup_init,
226
	.activate	= nft_lookup_activate,
227
	.deactivate	= nft_lookup_deactivate,
228 229
	.destroy	= nft_lookup_destroy,
	.dump		= nft_lookup_dump,
230
	.validate	= nft_lookup_validate,
231 232
};

233
struct nft_expr_type nft_lookup_type __read_mostly = {
234 235
	.name		= "lookup",
	.ops		= &nft_lookup_ops,
236 237
	.policy		= nft_lookup_policy,
	.maxattr	= NFTA_LOOKUP_MAX,
238
	.owner		= THIS_MODULE,
239
};