netfilter: nf_tables: add register parsing/dumping helpers
[deliverable/linux.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);
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, &desc, tb[NFTA_IMMEDIATE_DATA]);
53 if (err < 0)
54 return err;
55 priv->dlen = desc.len;
56
57 priv->dreg = nft_parse_register(tb[NFTA_IMMEDIATE_DREG]);
58 err = nft_validate_register_store(ctx, priv->dreg, &priv->data,
59 desc.type, desc.len);
60 if (err < 0)
61 goto err1;
62
63 return 0;
64
65 err1:
66 nft_data_uninit(&priv->data, desc.type);
67 return err;
68 }
69
70 static void nft_immediate_destroy(const struct nft_ctx *ctx,
71 const struct nft_expr *expr)
72 {
73 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
74 return nft_data_uninit(&priv->data, nft_dreg_to_type(priv->dreg));
75 }
76
77 static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr)
78 {
79 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
80
81 if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg))
82 goto nla_put_failure;
83
84 return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
85 nft_dreg_to_type(priv->dreg), priv->dlen);
86
87 nla_put_failure:
88 return -1;
89 }
90
91 static int nft_immediate_validate(const struct nft_ctx *ctx,
92 const struct nft_expr *expr,
93 const struct nft_data **data)
94 {
95 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
96
97 if (priv->dreg == NFT_REG_VERDICT)
98 *data = &priv->data;
99
100 return 0;
101 }
102
103 static struct nft_expr_type nft_imm_type;
104 static const struct nft_expr_ops nft_imm_ops = {
105 .type = &nft_imm_type,
106 .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)),
107 .eval = nft_immediate_eval,
108 .init = nft_immediate_init,
109 .destroy = nft_immediate_destroy,
110 .dump = nft_immediate_dump,
111 .validate = nft_immediate_validate,
112 };
113
114 static struct nft_expr_type nft_imm_type __read_mostly = {
115 .name = "immediate",
116 .ops = &nft_imm_ops,
117 .policy = nft_immediate_policy,
118 .maxattr = NFTA_IMMEDIATE_MAX,
119 .owner = THIS_MODULE,
120 };
121
122 int __init nft_immediate_module_init(void)
123 {
124 return nft_register_expr(&nft_imm_type);
125 }
126
127 void nft_immediate_module_exit(void)
128 {
129 nft_unregister_expr(&nft_imm_type);
130 }
This page took 0.034932 seconds and 6 git commands to generate.