netfilter: nf_tables: convert expressions to u32 register pointers
[deliverable/linux.git] / net / netfilter / nft_bitwise.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_bitwise {
21 enum nft_registers sreg:8;
22 enum nft_registers dreg:8;
23 u8 len;
24 struct nft_data mask;
25 struct nft_data xor;
26 };
27
28 static void nft_bitwise_eval(const struct nft_expr *expr,
29 struct nft_regs *regs,
30 const struct nft_pktinfo *pkt)
31 {
32 const struct nft_bitwise *priv = nft_expr_priv(expr);
33 const u32 *src = &regs->data[priv->sreg].data[0];
34 u32 *dst = &regs->data[priv->dreg].data[0];
35 unsigned int i;
36
37 for (i = 0; i < DIV_ROUND_UP(priv->len, 4); i++)
38 dst[i] = (src[i] & priv->mask.data[i]) ^ priv->xor.data[i];
39 }
40
41 static const struct nla_policy nft_bitwise_policy[NFTA_BITWISE_MAX + 1] = {
42 [NFTA_BITWISE_SREG] = { .type = NLA_U32 },
43 [NFTA_BITWISE_DREG] = { .type = NLA_U32 },
44 [NFTA_BITWISE_LEN] = { .type = NLA_U32 },
45 [NFTA_BITWISE_MASK] = { .type = NLA_NESTED },
46 [NFTA_BITWISE_XOR] = { .type = NLA_NESTED },
47 };
48
49 static int nft_bitwise_init(const struct nft_ctx *ctx,
50 const struct nft_expr *expr,
51 const struct nlattr * const tb[])
52 {
53 struct nft_bitwise *priv = nft_expr_priv(expr);
54 struct nft_data_desc d1, d2;
55 int err;
56
57 if (tb[NFTA_BITWISE_SREG] == NULL ||
58 tb[NFTA_BITWISE_DREG] == NULL ||
59 tb[NFTA_BITWISE_LEN] == NULL ||
60 tb[NFTA_BITWISE_MASK] == NULL ||
61 tb[NFTA_BITWISE_XOR] == NULL)
62 return -EINVAL;
63
64 priv->len = ntohl(nla_get_be32(tb[NFTA_BITWISE_LEN]));
65 priv->sreg = ntohl(nla_get_be32(tb[NFTA_BITWISE_SREG]));
66 err = nft_validate_register_load(priv->sreg, priv->len);
67 if (err < 0)
68 return err;
69
70 priv->dreg = ntohl(nla_get_be32(tb[NFTA_BITWISE_DREG]));
71 err = nft_validate_register_store(ctx, priv->dreg, NULL,
72 NFT_DATA_VALUE, priv->len);
73 if (err < 0)
74 return err;
75
76 err = nft_data_init(NULL, &priv->mask, &d1, tb[NFTA_BITWISE_MASK]);
77 if (err < 0)
78 return err;
79 if (d1.len != priv->len)
80 return -EINVAL;
81
82 err = nft_data_init(NULL, &priv->xor, &d2, tb[NFTA_BITWISE_XOR]);
83 if (err < 0)
84 return err;
85 if (d2.len != priv->len)
86 return -EINVAL;
87
88 return 0;
89 }
90
91 static int nft_bitwise_dump(struct sk_buff *skb, const struct nft_expr *expr)
92 {
93 const struct nft_bitwise *priv = nft_expr_priv(expr);
94
95 if (nla_put_be32(skb, NFTA_BITWISE_SREG, htonl(priv->sreg)))
96 goto nla_put_failure;
97 if (nla_put_be32(skb, NFTA_BITWISE_DREG, htonl(priv->dreg)))
98 goto nla_put_failure;
99 if (nla_put_be32(skb, NFTA_BITWISE_LEN, htonl(priv->len)))
100 goto nla_put_failure;
101
102 if (nft_data_dump(skb, NFTA_BITWISE_MASK, &priv->mask,
103 NFT_DATA_VALUE, priv->len) < 0)
104 goto nla_put_failure;
105
106 if (nft_data_dump(skb, NFTA_BITWISE_XOR, &priv->xor,
107 NFT_DATA_VALUE, priv->len) < 0)
108 goto nla_put_failure;
109
110 return 0;
111
112 nla_put_failure:
113 return -1;
114 }
115
116 static struct nft_expr_type nft_bitwise_type;
117 static const struct nft_expr_ops nft_bitwise_ops = {
118 .type = &nft_bitwise_type,
119 .size = NFT_EXPR_SIZE(sizeof(struct nft_bitwise)),
120 .eval = nft_bitwise_eval,
121 .init = nft_bitwise_init,
122 .dump = nft_bitwise_dump,
123 };
124
125 static struct nft_expr_type nft_bitwise_type __read_mostly = {
126 .name = "bitwise",
127 .ops = &nft_bitwise_ops,
128 .policy = nft_bitwise_policy,
129 .maxattr = NFTA_BITWISE_MAX,
130 .owner = THIS_MODULE,
131 };
132
133 int __init nft_bitwise_module_init(void)
134 {
135 return nft_register_expr(&nft_bitwise_type);
136 }
137
138 void nft_bitwise_module_exit(void)
139 {
140 nft_unregister_expr(&nft_bitwise_type);
141 }
This page took 0.033991 seconds and 5 git commands to generate.