GNU Linux-libre 4.14.266-gnu1
[releases.git] / net / netfilter / nft_immediate.c
1 /*
2  * Copyright (c) 2008-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/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
20 struct nft_immediate_expr {
21         struct nft_data         data;
22         enum nft_registers      dreg:8;
23         u8                      dlen;
24 };
25
26 static void nft_immediate_eval(const struct nft_expr *expr,
27                                struct nft_regs *regs,
28                                const struct nft_pktinfo *pkt)
29 {
30         const struct nft_immediate_expr *priv = nft_expr_priv(expr);
31
32         nft_data_copy(&regs->data[priv->dreg], &priv->data, priv->dlen);
33 }
34
35 static 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
40 static 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         err = nft_data_init(ctx, &priv->data, sizeof(priv->data), &desc,
53                             tb[NFTA_IMMEDIATE_DATA]);
54         if (err < 0)
55                 return err;
56
57         priv->dlen = desc.len;
58
59         priv->dreg = nft_parse_register(tb[NFTA_IMMEDIATE_DREG]);
60         err = nft_validate_register_store(ctx, priv->dreg, &priv->data,
61                                           desc.type, desc.len);
62         if (err < 0)
63                 goto err1;
64
65         return 0;
66
67 err1:
68         nft_data_release(&priv->data, desc.type);
69         return err;
70 }
71
72 static void nft_immediate_activate(const struct nft_ctx *ctx,
73                                    const struct nft_expr *expr)
74 {
75         const struct nft_immediate_expr *priv = nft_expr_priv(expr);
76
77         return nft_data_hold(&priv->data, nft_dreg_to_type(priv->dreg));
78 }
79
80 static void nft_immediate_deactivate(const struct nft_ctx *ctx,
81                                      const struct nft_expr *expr)
82 {
83         const struct nft_immediate_expr *priv = nft_expr_priv(expr);
84
85         return nft_data_release(&priv->data, nft_dreg_to_type(priv->dreg));
86 }
87
88 static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr)
89 {
90         const struct nft_immediate_expr *priv = nft_expr_priv(expr);
91
92         if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg))
93                 goto nla_put_failure;
94
95         return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
96                              nft_dreg_to_type(priv->dreg), priv->dlen);
97
98 nla_put_failure:
99         return -1;
100 }
101
102 static int nft_immediate_validate(const struct nft_ctx *ctx,
103                                   const struct nft_expr *expr,
104                                   const struct nft_data **data)
105 {
106         const struct nft_immediate_expr *priv = nft_expr_priv(expr);
107
108         if (priv->dreg == NFT_REG_VERDICT)
109                 *data = &priv->data;
110
111         return 0;
112 }
113
114 static const struct nft_expr_ops nft_imm_ops = {
115         .type           = &nft_imm_type,
116         .size           = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)),
117         .eval           = nft_immediate_eval,
118         .init           = nft_immediate_init,
119         .activate       = nft_immediate_activate,
120         .deactivate     = nft_immediate_deactivate,
121         .dump           = nft_immediate_dump,
122         .validate       = nft_immediate_validate,
123 };
124
125 struct nft_expr_type nft_imm_type __read_mostly = {
126         .name           = "immediate",
127         .ops            = &nft_imm_ops,
128         .policy         = nft_immediate_policy,
129         .maxattr        = NFTA_IMMEDIATE_MAX,
130         .owner          = THIS_MODULE,
131 };