Merge tag 'efi-urgent' of git://git.kernel.org/pub/scm/linux/kernel/git/mfleming...
[deliverable/linux.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 enum nft_registers sreg:8;
24 enum nft_registers dreg:8;
25 struct nft_set_binding binding;
26 };
27
28 static 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
40 static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = {
41 [NFTA_LOOKUP_SET] = { .type = NLA_STRING },
42 [NFTA_LOOKUP_SET_ID] = { .type = NLA_U32 },
43 [NFTA_LOOKUP_SREG] = { .type = NLA_U32 },
44 [NFTA_LOOKUP_DREG] = { .type = NLA_U32 },
45 };
46
47 static int nft_lookup_init(const struct nft_ctx *ctx,
48 const struct nft_expr *expr,
49 const struct nlattr * const tb[])
50 {
51 struct nft_lookup *priv = nft_expr_priv(expr);
52 struct nft_set *set;
53 int err;
54
55 if (tb[NFTA_LOOKUP_SET] == NULL ||
56 tb[NFTA_LOOKUP_SREG] == NULL)
57 return -EINVAL;
58
59 set = nf_tables_set_lookup(ctx->table, tb[NFTA_LOOKUP_SET]);
60 if (IS_ERR(set)) {
61 if (tb[NFTA_LOOKUP_SET_ID]) {
62 set = nf_tables_set_lookup_byid(ctx->net,
63 tb[NFTA_LOOKUP_SET_ID]);
64 }
65 if (IS_ERR(set))
66 return PTR_ERR(set);
67 }
68
69 priv->sreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_SREG]));
70 err = nft_validate_input_register(priv->sreg);
71 if (err < 0)
72 return err;
73
74 if (tb[NFTA_LOOKUP_DREG] != NULL) {
75 if (!(set->flags & NFT_SET_MAP))
76 return -EINVAL;
77
78 priv->dreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_DREG]));
79 err = nft_validate_output_register(priv->dreg);
80 if (err < 0)
81 return err;
82
83 if (priv->dreg == NFT_REG_VERDICT) {
84 if (set->dtype != NFT_DATA_VERDICT)
85 return -EINVAL;
86 } else if (set->dtype == NFT_DATA_VERDICT)
87 return -EINVAL;
88 } else if (set->flags & NFT_SET_MAP)
89 return -EINVAL;
90
91 err = nf_tables_bind_set(ctx, set, &priv->binding);
92 if (err < 0)
93 return err;
94
95 priv->set = set;
96 return 0;
97 }
98
99 static void nft_lookup_destroy(const struct nft_ctx *ctx,
100 const struct nft_expr *expr)
101 {
102 struct nft_lookup *priv = nft_expr_priv(expr);
103
104 nf_tables_unbind_set(ctx, priv->set, &priv->binding);
105 }
106
107 static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
108 {
109 const struct nft_lookup *priv = nft_expr_priv(expr);
110
111 if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
112 goto nla_put_failure;
113 if (nla_put_be32(skb, NFTA_LOOKUP_SREG, htonl(priv->sreg)))
114 goto nla_put_failure;
115 if (priv->set->flags & NFT_SET_MAP)
116 if (nla_put_be32(skb, NFTA_LOOKUP_DREG, htonl(priv->dreg)))
117 goto nla_put_failure;
118 return 0;
119
120 nla_put_failure:
121 return -1;
122 }
123
124 static struct nft_expr_type nft_lookup_type;
125 static const struct nft_expr_ops nft_lookup_ops = {
126 .type = &nft_lookup_type,
127 .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
128 .eval = nft_lookup_eval,
129 .init = nft_lookup_init,
130 .destroy = nft_lookup_destroy,
131 .dump = nft_lookup_dump,
132 };
133
134 static struct nft_expr_type nft_lookup_type __read_mostly = {
135 .name = "lookup",
136 .ops = &nft_lookup_ops,
137 .policy = nft_lookup_policy,
138 .maxattr = NFTA_LOOKUP_MAX,
139 .owner = THIS_MODULE,
140 };
141
142 int __init nft_lookup_module_init(void)
143 {
144 return nft_register_expr(&nft_lookup_type);
145 }
146
147 void nft_lookup_module_exit(void)
148 {
149 nft_unregister_expr(&nft_lookup_type);
150 }
This page took 0.036063 seconds and 6 git commands to generate.