]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/netfilter/nft_lookup.c
Merge branch 'for-linus-4.10' of git://git.kernel.org/pub/scm/linux/kernel/git/mason...
[mirror_ubuntu-zesty-kernel.git] / net / netfilter / nft_lookup.c
CommitLineData
20a69341
PM
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>
bd76ed36 19#include <net/netfilter/nf_tables_core.h>
20a69341
PM
20
21struct nft_lookup {
22 struct nft_set *set;
23 enum nft_registers sreg:8;
24 enum nft_registers dreg:8;
0071e184 25 bool invert;
20a69341
PM
26 struct nft_set_binding binding;
27};
28
29static void nft_lookup_eval(const struct nft_expr *expr,
a55e22e9 30 struct nft_regs *regs,
20a69341
PM
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;
b2832dd6 35 const struct nft_set_ext *ext;
0071e184 36 bool found;
20a69341 37
0e5a1c7e
PNA
38 found = set->ops->lookup(nft_net(pkt), set, &regs->data[priv->sreg],
39 &ext) ^ priv->invert;
0071e184
AB
40 if (!found) {
41 regs->verdict.code = NFT_BREAK;
20a69341 42 return;
b2832dd6 43 }
0071e184 44
4004d5c3 45 if (set->flags & NFT_SET_MAP)
0071e184
AB
46 nft_data_copy(&regs->data[priv->dreg],
47 nft_set_ext_data(ext), set->dlen);
48
20a69341
PM
49}
50
51static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = {
52 [NFTA_LOOKUP_SET] = { .type = NLA_STRING },
4c1017aa 53 [NFTA_LOOKUP_SET_ID] = { .type = NLA_U32 },
20a69341
PM
54 [NFTA_LOOKUP_SREG] = { .type = NLA_U32 },
55 [NFTA_LOOKUP_DREG] = { .type = NLA_U32 },
0071e184 56 [NFTA_LOOKUP_FLAGS] = { .type = NLA_U32 },
20a69341
PM
57};
58
59static int nft_lookup_init(const struct nft_ctx *ctx,
60 const struct nft_expr *expr,
61 const struct nlattr * const tb[])
62{
63 struct nft_lookup *priv = nft_expr_priv(expr);
37a9cc52 64 u8 genmask = nft_genmask_next(ctx->net);
20a69341 65 struct nft_set *set;
0071e184 66 u32 flags;
20a69341
PM
67 int err;
68
69 if (tb[NFTA_LOOKUP_SET] == NULL ||
70 tb[NFTA_LOOKUP_SREG] == NULL)
71 return -EINVAL;
72
37a9cc52 73 set = nf_tables_set_lookup(ctx->table, tb[NFTA_LOOKUP_SET], genmask);
958bee14
PNA
74 if (IS_ERR(set)) {
75 if (tb[NFTA_LOOKUP_SET_ID]) {
76 set = nf_tables_set_lookup_byid(ctx->net,
37a9cc52
PNA
77 tb[NFTA_LOOKUP_SET_ID],
78 genmask);
958bee14
PNA
79 }
80 if (IS_ERR(set))
81 return PTR_ERR(set);
82 }
20a69341 83
7c6c6e95
PM
84 if (set->flags & NFT_SET_EVAL)
85 return -EOPNOTSUPP;
86
b1c96ed3 87 priv->sreg = nft_parse_register(tb[NFTA_LOOKUP_SREG]);
d07db988 88 err = nft_validate_register_load(priv->sreg, set->klen);
20a69341
PM
89 if (err < 0)
90 return err;
91
0071e184
AB
92 if (tb[NFTA_LOOKUP_FLAGS]) {
93 flags = ntohl(nla_get_be32(tb[NFTA_LOOKUP_FLAGS]));
94
95 if (flags & ~NFT_LOOKUP_F_INV)
96 return -EINVAL;
97
98 if (flags & NFT_LOOKUP_F_INV) {
99 if (set->flags & NFT_SET_MAP)
100 return -EINVAL;
101 priv->invert = true;
102 }
103 }
104
20a69341 105 if (tb[NFTA_LOOKUP_DREG] != NULL) {
0071e184
AB
106 if (priv->invert)
107 return -EINVAL;
20a69341
PM
108 if (!(set->flags & NFT_SET_MAP))
109 return -EINVAL;
110
b1c96ed3 111 priv->dreg = nft_parse_register(tb[NFTA_LOOKUP_DREG]);
58f40ab6
PM
112 err = nft_validate_register_store(ctx, priv->dreg, NULL,
113 set->dtype, set->dlen);
114 if (err < 0)
115 return err;
20a69341
PM
116 } else if (set->flags & NFT_SET_MAP)
117 return -EINVAL;
118
11113e19
PM
119 priv->binding.flags = set->flags & NFT_SET_MAP;
120
20a69341
PM
121 err = nf_tables_bind_set(ctx, set, &priv->binding);
122 if (err < 0)
123 return err;
124
125 priv->set = set;
126 return 0;
127}
128
62472bce
PM
129static void nft_lookup_destroy(const struct nft_ctx *ctx,
130 const struct nft_expr *expr)
20a69341
PM
131{
132 struct nft_lookup *priv = nft_expr_priv(expr);
133
ab9da5c1 134 nf_tables_unbind_set(ctx, priv->set, &priv->binding);
20a69341
PM
135}
136
137static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
138{
139 const struct nft_lookup *priv = nft_expr_priv(expr);
0071e184 140 u32 flags = priv->invert ? NFT_LOOKUP_F_INV : 0;
20a69341
PM
141
142 if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
143 goto nla_put_failure;
b1c96ed3 144 if (nft_dump_register(skb, NFTA_LOOKUP_SREG, priv->sreg))
20a69341
PM
145 goto nla_put_failure;
146 if (priv->set->flags & NFT_SET_MAP)
b1c96ed3 147 if (nft_dump_register(skb, NFTA_LOOKUP_DREG, priv->dreg))
20a69341 148 goto nla_put_failure;
0071e184
AB
149 if (nla_put_be32(skb, NFTA_LOOKUP_FLAGS, htonl(flags)))
150 goto nla_put_failure;
20a69341
PM
151 return 0;
152
153nla_put_failure:
154 return -1;
155}
156
ef1f7df9
PM
157static const struct nft_expr_ops nft_lookup_ops = {
158 .type = &nft_lookup_type,
20a69341 159 .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
20a69341
PM
160 .eval = nft_lookup_eval,
161 .init = nft_lookup_init,
162 .destroy = nft_lookup_destroy,
163 .dump = nft_lookup_dump,
ef1f7df9
PM
164};
165
4e24877e 166struct nft_expr_type nft_lookup_type __read_mostly = {
ef1f7df9
PM
167 .name = "lookup",
168 .ops = &nft_lookup_ops,
20a69341
PM
169 .policy = nft_lookup_policy,
170 .maxattr = NFTA_LOOKUP_MAX,
ef1f7df9 171 .owner = THIS_MODULE,
20a69341 172};