blob: a5f30b8760eab5aa476f0afc13fe0e8686c9ff47 [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;
Patrick McHardyb2832dd2015-03-25 14:08:48 +000034 const struct nft_set_ext *ext;
Patrick McHardy20a69342013-10-11 12:06:22 +020035
Patrick McHardyb2832dd2015-03-25 14:08:48 +000036 if (set->ops->lookup(set, &data[priv->sreg], &ext)) {
37 if (set->flags & NFT_SET_MAP)
38 nft_data_copy(&data[priv->dreg], nft_set_ext_data(ext));
Patrick McHardy20a69342013-10-11 12:06:22 +020039 return;
Patrick McHardyb2832dd2015-03-25 14:08:48 +000040 }
Patrick McHardy20a69342013-10-11 12:06:22 +020041 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
42}
43
44static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = {
45 [NFTA_LOOKUP_SET] = { .type = NLA_STRING },
Patrick McHardy4c1017a2015-01-30 07:46:33 +000046 [NFTA_LOOKUP_SET_ID] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +020047 [NFTA_LOOKUP_SREG] = { .type = NLA_U32 },
48 [NFTA_LOOKUP_DREG] = { .type = NLA_U32 },
49};
50
51static int nft_lookup_init(const struct nft_ctx *ctx,
52 const struct nft_expr *expr,
53 const struct nlattr * const tb[])
54{
55 struct nft_lookup *priv = nft_expr_priv(expr);
56 struct nft_set *set;
57 int err;
58
59 if (tb[NFTA_LOOKUP_SET] == NULL ||
60 tb[NFTA_LOOKUP_SREG] == NULL)
61 return -EINVAL;
62
63 set = nf_tables_set_lookup(ctx->table, tb[NFTA_LOOKUP_SET]);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +020064 if (IS_ERR(set)) {
65 if (tb[NFTA_LOOKUP_SET_ID]) {
66 set = nf_tables_set_lookup_byid(ctx->net,
67 tb[NFTA_LOOKUP_SET_ID]);
68 }
69 if (IS_ERR(set))
70 return PTR_ERR(set);
71 }
Patrick McHardy20a69342013-10-11 12:06:22 +020072
73 priv->sreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_SREG]));
74 err = nft_validate_input_register(priv->sreg);
75 if (err < 0)
76 return err;
77
78 if (tb[NFTA_LOOKUP_DREG] != NULL) {
79 if (!(set->flags & NFT_SET_MAP))
80 return -EINVAL;
81
82 priv->dreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_DREG]));
83 err = nft_validate_output_register(priv->dreg);
84 if (err < 0)
85 return err;
86
87 if (priv->dreg == NFT_REG_VERDICT) {
88 if (set->dtype != NFT_DATA_VERDICT)
89 return -EINVAL;
90 } else if (set->dtype == NFT_DATA_VERDICT)
91 return -EINVAL;
92 } else if (set->flags & NFT_SET_MAP)
93 return -EINVAL;
94
95 err = nf_tables_bind_set(ctx, set, &priv->binding);
96 if (err < 0)
97 return err;
98
99 priv->set = set;
100 return 0;
101}
102
Patrick McHardy62472bc2014-03-07 19:08:30 +0100103static void nft_lookup_destroy(const struct nft_ctx *ctx,
104 const struct nft_expr *expr)
Patrick McHardy20a69342013-10-11 12:06:22 +0200105{
106 struct nft_lookup *priv = nft_expr_priv(expr);
107
Patrick McHardyab9da5c12014-03-07 19:08:31 +0100108 nf_tables_unbind_set(ctx, priv->set, &priv->binding);
Patrick McHardy20a69342013-10-11 12:06:22 +0200109}
110
111static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
112{
113 const struct nft_lookup *priv = nft_expr_priv(expr);
114
115 if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
116 goto nla_put_failure;
117 if (nla_put_be32(skb, NFTA_LOOKUP_SREG, htonl(priv->sreg)))
118 goto nla_put_failure;
119 if (priv->set->flags & NFT_SET_MAP)
120 if (nla_put_be32(skb, NFTA_LOOKUP_DREG, htonl(priv->dreg)))
121 goto nla_put_failure;
122 return 0;
123
124nla_put_failure:
125 return -1;
126}
127
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200128static struct nft_expr_type nft_lookup_type;
129static const struct nft_expr_ops nft_lookup_ops = {
130 .type = &nft_lookup_type,
Patrick McHardy20a69342013-10-11 12:06:22 +0200131 .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
Patrick McHardy20a69342013-10-11 12:06:22 +0200132 .eval = nft_lookup_eval,
133 .init = nft_lookup_init,
134 .destroy = nft_lookup_destroy,
135 .dump = nft_lookup_dump,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200136};
137
138static struct nft_expr_type nft_lookup_type __read_mostly = {
139 .name = "lookup",
140 .ops = &nft_lookup_ops,
Patrick McHardy20a69342013-10-11 12:06:22 +0200141 .policy = nft_lookup_policy,
142 .maxattr = NFTA_LOOKUP_MAX,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200143 .owner = THIS_MODULE,
Patrick McHardy20a69342013-10-11 12:06:22 +0200144};
145
146int __init nft_lookup_module_init(void)
147{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200148 return nft_register_expr(&nft_lookup_type);
Patrick McHardy20a69342013-10-11 12:06:22 +0200149}
150
151void nft_lookup_module_exit(void)
152{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200153 nft_unregister_expr(&nft_lookup_type);
Patrick McHardy20a69342013-10-11 12:06:22 +0200154}