]>
Commit | Line | Data |
---|---|---|
96518518 | 1 | /* |
ef1f7df9 | 2 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> |
96518518 PM |
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, | |
a55e22e9 | 27 | struct nft_regs *regs, |
96518518 PM |
28 | const struct nft_pktinfo *pkt) |
29 | { | |
30 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
31 | ||
49499c3e | 32 | nft_data_copy(®s->data[priv->dreg], &priv->data, priv->dlen); |
96518518 PM |
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 | ||
d0a11fc3 PM |
52 | err = nft_data_init(ctx, &priv->data, sizeof(priv->data), &desc, |
53 | tb[NFTA_IMMEDIATE_DATA]); | |
96518518 PM |
54 | if (err < 0) |
55 | return err; | |
36b701fa LGL |
56 | |
57 | if (desc.len > U8_MAX) | |
58 | return -ERANGE; | |
59 | ||
96518518 PM |
60 | priv->dlen = desc.len; |
61 | ||
b1c96ed3 | 62 | priv->dreg = nft_parse_register(tb[NFTA_IMMEDIATE_DREG]); |
1ec10212 PM |
63 | err = nft_validate_register_store(ctx, priv->dreg, &priv->data, |
64 | desc.type, desc.len); | |
96518518 PM |
65 | if (err < 0) |
66 | goto err1; | |
67 | ||
68 | return 0; | |
69 | ||
70 | err1: | |
71 | nft_data_uninit(&priv->data, desc.type); | |
72 | return err; | |
73 | } | |
74 | ||
62472bce PM |
75 | static void nft_immediate_destroy(const struct nft_ctx *ctx, |
76 | const struct nft_expr *expr) | |
96518518 PM |
77 | { |
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 | ||
82 | static 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 | ||
b1c96ed3 | 86 | if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg)) |
96518518 PM |
87 | 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 | ||
92 | nla_put_failure: | |
93 | return -1; | |
94 | } | |
95 | ||
0ca743a5 PNA |
96 | static int nft_immediate_validate(const struct nft_ctx *ctx, |
97 | const struct nft_expr *expr, | |
98 | const struct nft_data **data) | |
20a69341 PM |
99 | { |
100 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
101 | ||
102 | if (priv->dreg == NFT_REG_VERDICT) | |
0ca743a5 PNA |
103 | *data = &priv->data; |
104 | ||
105 | return 0; | |
20a69341 PM |
106 | } |
107 | ||
ef1f7df9 PM |
108 | static struct nft_expr_type nft_imm_type; |
109 | static const struct nft_expr_ops nft_imm_ops = { | |
110 | .type = &nft_imm_type, | |
96518518 | 111 | .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)), |
96518518 PM |
112 | .eval = nft_immediate_eval, |
113 | .init = nft_immediate_init, | |
114 | .destroy = nft_immediate_destroy, | |
115 | .dump = nft_immediate_dump, | |
0ca743a5 | 116 | .validate = nft_immediate_validate, |
ef1f7df9 PM |
117 | }; |
118 | ||
119 | static struct nft_expr_type nft_imm_type __read_mostly = { | |
120 | .name = "immediate", | |
121 | .ops = &nft_imm_ops, | |
96518518 PM |
122 | .policy = nft_immediate_policy, |
123 | .maxattr = NFTA_IMMEDIATE_MAX, | |
ef1f7df9 | 124 | .owner = THIS_MODULE, |
96518518 PM |
125 | }; |
126 | ||
127 | int __init nft_immediate_module_init(void) | |
128 | { | |
ef1f7df9 | 129 | return nft_register_expr(&nft_imm_type); |
96518518 PM |
130 | } |
131 | ||
132 | void nft_immediate_module_exit(void) | |
133 | { | |
ef1f7df9 | 134 | nft_unregister_expr(&nft_imm_type); |
96518518 | 135 | } |