blob: 810385eb7249c7be39bcbc0e6c34abc1a6b1708d [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,
27 struct nft_data data[NFT_REG_MAX + 1],
28 const struct nft_pktinfo *pkt)
29{
30 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
31
32 nft_data_copy(&data[priv->dreg], &priv->data);
33}
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
52 priv->dreg = ntohl(nla_get_be32(tb[NFTA_IMMEDIATE_DREG]));
53 err = nft_validate_output_register(priv->dreg);
54 if (err < 0)
55 return err;
56
57 err = nft_data_init(ctx, &priv->data, &desc, tb[NFTA_IMMEDIATE_DATA]);
58 if (err < 0)
59 return err;
60 priv->dlen = desc.len;
61
62 err = nft_validate_data_load(ctx, priv->dreg, &priv->data, desc.type);
63 if (err < 0)
64 goto err1;
65
66 return 0;
67
68err1:
69 nft_data_uninit(&priv->data, desc.type);
70 return err;
71}
72
Patrick McHardy62472bc2014-03-07 19:08:30 +010073static void nft_immediate_destroy(const struct nft_ctx *ctx,
74 const struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +020075{
76 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
77 return nft_data_uninit(&priv->data, nft_dreg_to_type(priv->dreg));
78}
79
80static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr)
81{
82 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
83
84 if (nla_put_be32(skb, NFTA_IMMEDIATE_DREG, htonl(priv->dreg)))
85 goto nla_put_failure;
86
87 return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
88 nft_dreg_to_type(priv->dreg), priv->dlen);
89
90nla_put_failure:
91 return -1;
92}
93
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020094static int nft_immediate_validate(const struct nft_ctx *ctx,
95 const struct nft_expr *expr,
96 const struct nft_data **data)
Patrick McHardy20a69342013-10-11 12:06:22 +020097{
98 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
99
100 if (priv->dreg == NFT_REG_VERDICT)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200101 *data = &priv->data;
102
103 return 0;
Patrick McHardy20a69342013-10-11 12:06:22 +0200104}
105
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200106static struct nft_expr_type nft_imm_type;
107static const struct nft_expr_ops nft_imm_ops = {
108 .type = &nft_imm_type,
Patrick McHardy96518512013-10-14 11:00:02 +0200109 .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)),
Patrick McHardy96518512013-10-14 11:00:02 +0200110 .eval = nft_immediate_eval,
111 .init = nft_immediate_init,
112 .destroy = nft_immediate_destroy,
113 .dump = nft_immediate_dump,
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200114 .validate = nft_immediate_validate,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200115};
116
117static struct nft_expr_type nft_imm_type __read_mostly = {
118 .name = "immediate",
119 .ops = &nft_imm_ops,
Patrick McHardy96518512013-10-14 11:00:02 +0200120 .policy = nft_immediate_policy,
121 .maxattr = NFTA_IMMEDIATE_MAX,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200122 .owner = THIS_MODULE,
Patrick McHardy96518512013-10-14 11:00:02 +0200123};
124
125int __init nft_immediate_module_init(void)
126{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200127 return nft_register_expr(&nft_imm_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200128}
129
130void nft_immediate_module_exit(void)
131{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200132 nft_unregister_expr(&nft_imm_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200133}