GNU Linux-libre 4.14.328-gnu1
[releases.git] / net / netfilter / nft_lookup.c
1 /*
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>
19 #include <net/netfilter/nf_tables_core.h>
20
21 struct nft_lookup {
22         struct nft_set                  *set;
23         u8                              sreg;
24         u8                              dreg;
25         bool                            invert;
26         struct nft_set_binding          binding;
27 };
28
29 static void nft_lookup_eval(const struct nft_expr *expr,
30                             struct nft_regs *regs,
31                             const struct nft_pktinfo *pkt)
32 {
33         const struct nft_lookup *priv = nft_expr_priv(expr);
34         const struct nft_set *set = priv->set;
35         const struct nft_set_ext *ext;
36         bool found;
37
38         found = set->ops->lookup(nft_net(pkt), set, &regs->data[priv->sreg],
39                                  &ext) ^ priv->invert;
40         if (!found) {
41                 regs->verdict.code = NFT_BREAK;
42                 return;
43         }
44
45         if (set->flags & NFT_SET_MAP)
46                 nft_data_copy(&regs->data[priv->dreg],
47                               nft_set_ext_data(ext), set->dlen);
48
49 }
50
51 static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = {
52         [NFTA_LOOKUP_SET]       = { .type = NLA_STRING,
53                                     .len = NFT_SET_MAXNAMELEN - 1 },
54         [NFTA_LOOKUP_SET_ID]    = { .type = NLA_U32 },
55         [NFTA_LOOKUP_SREG]      = { .type = NLA_U32 },
56         [NFTA_LOOKUP_DREG]      = { .type = NLA_U32 },
57         [NFTA_LOOKUP_FLAGS]     = { .type = NLA_U32 },
58 };
59
60 static int nft_lookup_init(const struct nft_ctx *ctx,
61                            const struct nft_expr *expr,
62                            const struct nlattr * const tb[])
63 {
64         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         int err;
69
70         if (tb[NFTA_LOOKUP_SET] == NULL ||
71             tb[NFTA_LOOKUP_SREG] == NULL)
72                 return -EINVAL;
73
74         set = nft_set_lookup(ctx->net, ctx->table, tb[NFTA_LOOKUP_SET],
75                              tb[NFTA_LOOKUP_SET_ID], genmask);
76         if (IS_ERR(set))
77                 return PTR_ERR(set);
78
79         err = nft_parse_register_load(tb[NFTA_LOOKUP_SREG], &priv->sreg,
80                                       set->klen);
81         if (err < 0)
82                 return err;
83
84         if (tb[NFTA_LOOKUP_FLAGS]) {
85                 flags = ntohl(nla_get_be32(tb[NFTA_LOOKUP_FLAGS]));
86
87                 if (flags & ~NFT_LOOKUP_F_INV)
88                         return -EINVAL;
89
90                 if (flags & NFT_LOOKUP_F_INV) {
91                         if (set->flags & NFT_SET_MAP)
92                                 return -EINVAL;
93                         priv->invert = true;
94                 }
95         }
96
97         if (tb[NFTA_LOOKUP_DREG] != NULL) {
98                 if (priv->invert)
99                         return -EINVAL;
100                 if (!(set->flags & NFT_SET_MAP))
101                         return -EINVAL;
102
103                 err = nft_parse_register_store(ctx, tb[NFTA_LOOKUP_DREG],
104                                                &priv->dreg, NULL, set->dtype,
105                                                set->dlen);
106                 if (err < 0)
107                         return err;
108         } else if (set->flags & NFT_SET_MAP)
109                 return -EINVAL;
110
111         priv->binding.flags = set->flags & NFT_SET_MAP;
112
113         err = nf_tables_bind_set(ctx, set, &priv->binding);
114         if (err < 0)
115                 return err;
116
117         priv->set = set;
118         return 0;
119 }
120
121 static void nft_lookup_deactivate(const struct nft_ctx *ctx,
122                                   const struct nft_expr *expr,
123                                   enum nft_trans_phase phase)
124 {
125         struct nft_lookup *priv = nft_expr_priv(expr);
126
127         nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
128 }
129
130 static void nft_lookup_activate(const struct nft_ctx *ctx,
131                                 const struct nft_expr *expr)
132 {
133         struct nft_lookup *priv = nft_expr_priv(expr);
134
135         nf_tables_activate_set(ctx, priv->set);
136 }
137
138 static void nft_lookup_destroy(const struct nft_ctx *ctx,
139                                const struct nft_expr *expr)
140 {
141         struct nft_lookup *priv = nft_expr_priv(expr);
142
143         nf_tables_destroy_set(ctx, priv->set);
144 }
145
146 static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
147 {
148         const struct nft_lookup *priv = nft_expr_priv(expr);
149         u32 flags = priv->invert ? NFT_LOOKUP_F_INV : 0;
150
151         if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
152                 goto nla_put_failure;
153         if (nft_dump_register(skb, NFTA_LOOKUP_SREG, priv->sreg))
154                 goto nla_put_failure;
155         if (priv->set->flags & NFT_SET_MAP)
156                 if (nft_dump_register(skb, NFTA_LOOKUP_DREG, priv->dreg))
157                         goto nla_put_failure;
158         if (nla_put_be32(skb, NFTA_LOOKUP_FLAGS, htonl(flags)))
159                 goto nla_put_failure;
160         return 0;
161
162 nla_put_failure:
163         return -1;
164 }
165
166 static const struct nft_expr_ops nft_lookup_ops = {
167         .type           = &nft_lookup_type,
168         .size           = NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
169         .eval           = nft_lookup_eval,
170         .init           = nft_lookup_init,
171         .activate       = nft_lookup_activate,
172         .deactivate     = nft_lookup_deactivate,
173         .destroy        = nft_lookup_destroy,
174         .dump           = nft_lookup_dump,
175 };
176
177 struct nft_expr_type nft_lookup_type __read_mostly = {
178         .name           = "lookup",
179         .ops            = &nft_lookup_ops,
180         .policy         = nft_lookup_policy,
181         .maxattr        = NFTA_LOOKUP_MAX,
182         .owner          = THIS_MODULE,
183 };