blob: 953978e8f0bac8a7a2b9fd40f758b2f2265b880a [file] [log] [blame]
Patrick McHardy20a69342013-10-11 12:06:22 +02001/*
2 * Copyright (c) 2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/list.h>
14#include <linux/rbtree.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nf_tables.h>
18#include <net/netfilter/nf_tables.h>
Rashika Kheriabd76ed32014-02-09 22:31:42 +053019#include <net/netfilter/nf_tables_core.h>
Patrick McHardy20a69342013-10-11 12:06:22 +020020
21struct nft_lookup {
22 struct nft_set *set;
23 enum nft_registers sreg:8;
24 enum nft_registers dreg:8;
25 struct nft_set_binding binding;
26};
27
28static void nft_lookup_eval(const struct nft_expr *expr,
29 struct nft_data data[NFT_REG_MAX + 1],
30 const struct nft_pktinfo *pkt)
31{
32 const struct nft_lookup *priv = nft_expr_priv(expr);
33 const struct nft_set *set = priv->set;
34
35 if (set->ops->lookup(set, &data[priv->sreg], &data[priv->dreg]))
36 return;
37 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
38}
39
40static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = {
41 [NFTA_LOOKUP_SET] = { .type = NLA_STRING },
42 [NFTA_LOOKUP_SREG] = { .type = NLA_U32 },
43 [NFTA_LOOKUP_DREG] = { .type = NLA_U32 },
44};
45
46static int nft_lookup_init(const struct nft_ctx *ctx,
47 const struct nft_expr *expr,
48 const struct nlattr * const tb[])
49{
50 struct nft_lookup *priv = nft_expr_priv(expr);
51 struct nft_set *set;
52 int err;
53
54 if (tb[NFTA_LOOKUP_SET] == NULL ||
55 tb[NFTA_LOOKUP_SREG] == NULL)
56 return -EINVAL;
57
58 set = nf_tables_set_lookup(ctx->table, tb[NFTA_LOOKUP_SET]);
59 if (IS_ERR(set))
60 return PTR_ERR(set);
61
62 priv->sreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_SREG]));
63 err = nft_validate_input_register(priv->sreg);
64 if (err < 0)
65 return err;
66
67 if (tb[NFTA_LOOKUP_DREG] != NULL) {
68 if (!(set->flags & NFT_SET_MAP))
69 return -EINVAL;
70
71 priv->dreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_DREG]));
72 err = nft_validate_output_register(priv->dreg);
73 if (err < 0)
74 return err;
75
76 if (priv->dreg == NFT_REG_VERDICT) {
77 if (set->dtype != NFT_DATA_VERDICT)
78 return -EINVAL;
79 } else if (set->dtype == NFT_DATA_VERDICT)
80 return -EINVAL;
81 } else if (set->flags & NFT_SET_MAP)
82 return -EINVAL;
83
84 err = nf_tables_bind_set(ctx, set, &priv->binding);
85 if (err < 0)
86 return err;
87
88 priv->set = set;
89 return 0;
90}
91
Patrick McHardy62472bc2014-03-07 19:08:30 +010092static void nft_lookup_destroy(const struct nft_ctx *ctx,
93 const struct nft_expr *expr)
Patrick McHardy20a69342013-10-11 12:06:22 +020094{
95 struct nft_lookup *priv = nft_expr_priv(expr);
96
97 nf_tables_unbind_set(NULL, priv->set, &priv->binding);
98}
99
100static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
101{
102 const struct nft_lookup *priv = nft_expr_priv(expr);
103
104 if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
105 goto nla_put_failure;
106 if (nla_put_be32(skb, NFTA_LOOKUP_SREG, htonl(priv->sreg)))
107 goto nla_put_failure;
108 if (priv->set->flags & NFT_SET_MAP)
109 if (nla_put_be32(skb, NFTA_LOOKUP_DREG, htonl(priv->dreg)))
110 goto nla_put_failure;
111 return 0;
112
113nla_put_failure:
114 return -1;
115}
116
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200117static struct nft_expr_type nft_lookup_type;
118static const struct nft_expr_ops nft_lookup_ops = {
119 .type = &nft_lookup_type,
Patrick McHardy20a69342013-10-11 12:06:22 +0200120 .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
Patrick McHardy20a69342013-10-11 12:06:22 +0200121 .eval = nft_lookup_eval,
122 .init = nft_lookup_init,
123 .destroy = nft_lookup_destroy,
124 .dump = nft_lookup_dump,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200125};
126
127static struct nft_expr_type nft_lookup_type __read_mostly = {
128 .name = "lookup",
129 .ops = &nft_lookup_ops,
Patrick McHardy20a69342013-10-11 12:06:22 +0200130 .policy = nft_lookup_policy,
131 .maxattr = NFTA_LOOKUP_MAX,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200132 .owner = THIS_MODULE,
Patrick McHardy20a69342013-10-11 12:06:22 +0200133};
134
135int __init nft_lookup_module_init(void)
136{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200137 return nft_register_expr(&nft_lookup_type);
Patrick McHardy20a69342013-10-11 12:06:22 +0200138}
139
140void nft_lookup_module_exit(void)
141{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200142 nft_unregister_expr(&nft_lookup_type);
Patrick McHardy20a69342013-10-11 12:06:22 +0200143}