blob: d17018ff54e6e67accc7f60e22f3cbfea6adfa94 [file] [log] [blame]
Patrick McHardy96518512013-10-14 11:00:02 +02001/*
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
Patrick McHardy96518512013-10-14 11:00:02 +02003 *
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/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables_core.h>
18#include <net/netfilter/nf_tables.h>
19
20struct nft_immediate_expr {
21 struct nft_data data;
22 enum nft_registers dreg:8;
23 u8 dlen;
24};
25
26static void nft_immediate_eval(const struct nft_expr *expr,
Patrick McHardya55e22e2015-04-11 02:27:31 +010027 struct nft_regs *regs,
Patrick McHardy96518512013-10-14 11:00:02 +020028 const struct nft_pktinfo *pkt)
29{
30 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
31
Patrick McHardy49499c32015-04-11 02:27:37 +010032 nft_data_copy(&regs->data[priv->dreg], &priv->data, priv->dlen);
Patrick McHardy96518512013-10-14 11:00:02 +020033}
34
35static const struct nla_policy nft_immediate_policy[NFTA_IMMEDIATE_MAX + 1] = {
36 [NFTA_IMMEDIATE_DREG] = { .type = NLA_U32 },
37 [NFTA_IMMEDIATE_DATA] = { .type = NLA_NESTED },
38};
39
40static int nft_immediate_init(const struct nft_ctx *ctx,
41 const struct nft_expr *expr,
42 const struct nlattr * const tb[])
43{
44 struct nft_immediate_expr *priv = nft_expr_priv(expr);
45 struct nft_data_desc desc;
46 int err;
47
48 if (tb[NFTA_IMMEDIATE_DREG] == NULL ||
49 tb[NFTA_IMMEDIATE_DATA] == NULL)
50 return -EINVAL;
51
Patrick McHardyd0a11fc2015-04-11 02:27:38 +010052 err = nft_data_init(ctx, &priv->data, sizeof(priv->data), &desc,
53 tb[NFTA_IMMEDIATE_DATA]);
Patrick McHardy96518512013-10-14 11:00:02 +020054 if (err < 0)
55 return err;
Laura Garcia Liebana36b701f2016-09-14 15:00:02 +020056
57 if (desc.len > U8_MAX)
58 return -ERANGE;
59
Patrick McHardy96518512013-10-14 11:00:02 +020060 priv->dlen = desc.len;
61
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010062 priv->dreg = nft_parse_register(tb[NFTA_IMMEDIATE_DREG]);
Patrick McHardy1ec10212015-04-11 02:27:27 +010063 err = nft_validate_register_store(ctx, priv->dreg, &priv->data,
64 desc.type, desc.len);
Patrick McHardy96518512013-10-14 11:00:02 +020065 if (err < 0)
66 goto err1;
67
68 return 0;
69
70err1:
71 nft_data_uninit(&priv->data, desc.type);
72 return err;
73}
74
Patrick McHardy62472bc2014-03-07 19:08:30 +010075static void nft_immediate_destroy(const struct nft_ctx *ctx,
76 const struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +020077{
78 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
79 return nft_data_uninit(&priv->data, nft_dreg_to_type(priv->dreg));
80}
81
82static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr)
83{
84 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
85
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010086 if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg))
Patrick McHardy96518512013-10-14 11:00:02 +020087 goto nla_put_failure;
88
89 return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
90 nft_dreg_to_type(priv->dreg), priv->dlen);
91
92nla_put_failure:
93 return -1;
94}
95
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020096static int nft_immediate_validate(const struct nft_ctx *ctx,
97 const struct nft_expr *expr,
98 const struct nft_data **data)
Patrick McHardy20a69342013-10-11 12:06:22 +020099{
100 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
101
102 if (priv->dreg == NFT_REG_VERDICT)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200103 *data = &priv->data;
104
105 return 0;
Patrick McHardy20a69342013-10-11 12:06:22 +0200106}
107
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200108static struct nft_expr_type nft_imm_type;
109static const struct nft_expr_ops nft_imm_ops = {
110 .type = &nft_imm_type,
Patrick McHardy96518512013-10-14 11:00:02 +0200111 .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)),
Patrick McHardy96518512013-10-14 11:00:02 +0200112 .eval = nft_immediate_eval,
113 .init = nft_immediate_init,
114 .destroy = nft_immediate_destroy,
115 .dump = nft_immediate_dump,
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200116 .validate = nft_immediate_validate,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200117};
118
119static struct nft_expr_type nft_imm_type __read_mostly = {
120 .name = "immediate",
121 .ops = &nft_imm_ops,
Patrick McHardy96518512013-10-14 11:00:02 +0200122 .policy = nft_immediate_policy,
123 .maxattr = NFTA_IMMEDIATE_MAX,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200124 .owner = THIS_MODULE,
Patrick McHardy96518512013-10-14 11:00:02 +0200125};
126
127int __init nft_immediate_module_init(void)
128{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200129 return nft_register_expr(&nft_imm_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200130}
131
132void nft_immediate_module_exit(void)
133{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200134 nft_unregister_expr(&nft_imm_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200135}