Merge remote-tracking branches 'spi/topic/img-spfi', 'spi/topic/imx', 'spi/topic...
[deliverable/linux.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;
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]);
958bee14
PNA
59 if (IS_ERR(set)) {
60 if (tb[NFTA_LOOKUP_SET_ID]) {
61 set = nf_tables_set_lookup_byid(ctx->net,
62 tb[NFTA_LOOKUP_SET_ID]);
63 }
64 if (IS_ERR(set))
65 return PTR_ERR(set);
66 }
20a69341
PM
67
68 priv->sreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_SREG]));
69 err = nft_validate_input_register(priv->sreg);
70 if (err < 0)
71 return err;
72
73 if (tb[NFTA_LOOKUP_DREG] != NULL) {
74 if (!(set->flags & NFT_SET_MAP))
75 return -EINVAL;
76
77 priv->dreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_DREG]));
78 err = nft_validate_output_register(priv->dreg);
79 if (err < 0)
80 return err;
81
82 if (priv->dreg == NFT_REG_VERDICT) {
83 if (set->dtype != NFT_DATA_VERDICT)
84 return -EINVAL;
85 } else if (set->dtype == NFT_DATA_VERDICT)
86 return -EINVAL;
87 } else if (set->flags & NFT_SET_MAP)
88 return -EINVAL;
89
90 err = nf_tables_bind_set(ctx, set, &priv->binding);
91 if (err < 0)
92 return err;
93
94 priv->set = set;
95 return 0;
96}
97
62472bce
PM
98static void nft_lookup_destroy(const struct nft_ctx *ctx,
99 const struct nft_expr *expr)
20a69341
PM
100{
101 struct nft_lookup *priv = nft_expr_priv(expr);
102
ab9da5c1 103 nf_tables_unbind_set(ctx, priv->set, &priv->binding);
20a69341
PM
104}
105
106static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
107{
108 const struct nft_lookup *priv = nft_expr_priv(expr);
109
110 if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
111 goto nla_put_failure;
112 if (nla_put_be32(skb, NFTA_LOOKUP_SREG, htonl(priv->sreg)))
113 goto nla_put_failure;
114 if (priv->set->flags & NFT_SET_MAP)
115 if (nla_put_be32(skb, NFTA_LOOKUP_DREG, htonl(priv->dreg)))
116 goto nla_put_failure;
117 return 0;
118
119nla_put_failure:
120 return -1;
121}
122
ef1f7df9
PM
123static struct nft_expr_type nft_lookup_type;
124static const struct nft_expr_ops nft_lookup_ops = {
125 .type = &nft_lookup_type,
20a69341 126 .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
20a69341
PM
127 .eval = nft_lookup_eval,
128 .init = nft_lookup_init,
129 .destroy = nft_lookup_destroy,
130 .dump = nft_lookup_dump,
ef1f7df9
PM
131};
132
133static struct nft_expr_type nft_lookup_type __read_mostly = {
134 .name = "lookup",
135 .ops = &nft_lookup_ops,
20a69341
PM
136 .policy = nft_lookup_policy,
137 .maxattr = NFTA_LOOKUP_MAX,
ef1f7df9 138 .owner = THIS_MODULE,
20a69341
PM
139};
140
141int __init nft_lookup_module_init(void)
142{
ef1f7df9 143 return nft_register_expr(&nft_lookup_type);
20a69341
PM
144}
145
146void nft_lookup_module_exit(void)
147{
ef1f7df9 148 nft_unregister_expr(&nft_lookup_type);
20a69341 149}
This page took 0.118422 seconds and 5 git commands to generate.