blob: b3c31ef8015d2517f594dc60a027c52aac8d41fe [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,
Patrick McHardya55e22e2015-04-11 02:27:31 +010029 struct nft_regs *regs,
Patrick McHardy20a69342013-10-11 12:06:22 +020030 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 McHardya55e22e2015-04-11 02:27:31 +010036 if (set->ops->lookup(set, &regs->data[priv->sreg], &ext)) {
Patrick McHardyb2832dd2015-03-25 14:08:48 +000037 if (set->flags & NFT_SET_MAP)
Patrick McHardya55e22e2015-04-11 02:27:31 +010038 nft_data_copy(&regs->data[priv->dreg],
Patrick McHardy49499c32015-04-11 02:27:37 +010039 nft_set_ext_data(ext), set->dlen);
Patrick McHardy20a69342013-10-11 12:06:22 +020040 return;
Patrick McHardyb2832dd2015-03-25 14:08:48 +000041 }
Patrick McHardya55e22e2015-04-11 02:27:31 +010042 regs->verdict.code = NFT_BREAK;
Patrick McHardy20a69342013-10-11 12:06:22 +020043}
44
45static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = {
46 [NFTA_LOOKUP_SET] = { .type = NLA_STRING },
Patrick McHardy4c1017a2015-01-30 07:46:33 +000047 [NFTA_LOOKUP_SET_ID] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +020048 [NFTA_LOOKUP_SREG] = { .type = NLA_U32 },
49 [NFTA_LOOKUP_DREG] = { .type = NLA_U32 },
50};
51
52static int nft_lookup_init(const struct nft_ctx *ctx,
53 const struct nft_expr *expr,
54 const struct nlattr * const tb[])
55{
56 struct nft_lookup *priv = nft_expr_priv(expr);
57 struct nft_set *set;
58 int err;
59
60 if (tb[NFTA_LOOKUP_SET] == NULL ||
61 tb[NFTA_LOOKUP_SREG] == NULL)
62 return -EINVAL;
63
64 set = nf_tables_set_lookup(ctx->table, tb[NFTA_LOOKUP_SET]);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +020065 if (IS_ERR(set)) {
66 if (tb[NFTA_LOOKUP_SET_ID]) {
67 set = nf_tables_set_lookup_byid(ctx->net,
68 tb[NFTA_LOOKUP_SET_ID]);
69 }
70 if (IS_ERR(set))
71 return PTR_ERR(set);
72 }
Patrick McHardy20a69342013-10-11 12:06:22 +020073
Patrick McHardy7c6c6e92015-04-11 10:46:41 +010074 if (set->flags & NFT_SET_EVAL)
75 return -EOPNOTSUPP;
76
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010077 priv->sreg = nft_parse_register(tb[NFTA_LOOKUP_SREG]);
Patrick McHardyd07db982015-04-11 02:27:30 +010078 err = nft_validate_register_load(priv->sreg, set->klen);
Patrick McHardy20a69342013-10-11 12:06:22 +020079 if (err < 0)
80 return err;
81
82 if (tb[NFTA_LOOKUP_DREG] != NULL) {
83 if (!(set->flags & NFT_SET_MAP))
84 return -EINVAL;
85
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010086 priv->dreg = nft_parse_register(tb[NFTA_LOOKUP_DREG]);
Patrick McHardy58f40ab2015-04-11 02:27:28 +010087 err = nft_validate_register_store(ctx, priv->dreg, NULL,
88 set->dtype, set->dlen);
89 if (err < 0)
90 return err;
Patrick McHardy20a69342013-10-11 12:06:22 +020091 } else if (set->flags & NFT_SET_MAP)
92 return -EINVAL;
93
Patrick McHardy11113e12015-04-05 14:41:07 +020094 priv->binding.flags = set->flags & NFT_SET_MAP;
95
Patrick McHardy20a69342013-10-11 12:06:22 +020096 err = nf_tables_bind_set(ctx, set, &priv->binding);
97 if (err < 0)
98 return err;
99
100 priv->set = set;
101 return 0;
102}
103
Patrick McHardy62472bc2014-03-07 19:08:30 +0100104static void nft_lookup_destroy(const struct nft_ctx *ctx,
105 const struct nft_expr *expr)
Patrick McHardy20a69342013-10-11 12:06:22 +0200106{
107 struct nft_lookup *priv = nft_expr_priv(expr);
108
Patrick McHardyab9da5c12014-03-07 19:08:31 +0100109 nf_tables_unbind_set(ctx, priv->set, &priv->binding);
Patrick McHardy20a69342013-10-11 12:06:22 +0200110}
111
112static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
113{
114 const struct nft_lookup *priv = nft_expr_priv(expr);
115
116 if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
117 goto nla_put_failure;
Patrick McHardyb1c96ed2015-04-11 02:27:36 +0100118 if (nft_dump_register(skb, NFTA_LOOKUP_SREG, priv->sreg))
Patrick McHardy20a69342013-10-11 12:06:22 +0200119 goto nla_put_failure;
120 if (priv->set->flags & NFT_SET_MAP)
Patrick McHardyb1c96ed2015-04-11 02:27:36 +0100121 if (nft_dump_register(skb, NFTA_LOOKUP_DREG, priv->dreg))
Patrick McHardy20a69342013-10-11 12:06:22 +0200122 goto nla_put_failure;
123 return 0;
124
125nla_put_failure:
126 return -1;
127}
128
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200129static struct nft_expr_type nft_lookup_type;
130static const struct nft_expr_ops nft_lookup_ops = {
131 .type = &nft_lookup_type,
Patrick McHardy20a69342013-10-11 12:06:22 +0200132 .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
Patrick McHardy20a69342013-10-11 12:06:22 +0200133 .eval = nft_lookup_eval,
134 .init = nft_lookup_init,
135 .destroy = nft_lookup_destroy,
136 .dump = nft_lookup_dump,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200137};
138
139static struct nft_expr_type nft_lookup_type __read_mostly = {
140 .name = "lookup",
141 .ops = &nft_lookup_ops,
Patrick McHardy20a69342013-10-11 12:06:22 +0200142 .policy = nft_lookup_policy,
143 .maxattr = NFTA_LOOKUP_MAX,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200144 .owner = THIS_MODULE,
Patrick McHardy20a69342013-10-11 12:06:22 +0200145};
146
147int __init nft_lookup_module_init(void)
148{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200149 return nft_register_expr(&nft_lookup_type);
Patrick McHardy20a69342013-10-11 12:06:22 +0200150}
151
152void nft_lookup_module_exit(void)
153{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200154 nft_unregister_expr(&nft_lookup_type);
Patrick McHardy20a69342013-10-11 12:06:22 +0200155}