Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / net / netfilter / nft_bitwise.c
blobfff8073e2a5692c14037a77c5d8151cf0c1bbcb0
1 /*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
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.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
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>
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;
28 static void nft_bitwise_eval(const struct nft_expr *expr,
29 struct nft_regs *regs,
30 const struct nft_pktinfo *pkt)
32 const struct nft_bitwise *priv = nft_expr_priv(expr);
33 const u32 *src = &regs->data[priv->sreg];
34 u32 *dst = &regs->data[priv->dreg];
35 unsigned int i;
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];
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 },
49 static int nft_bitwise_init(const struct nft_ctx *ctx,
50 const struct nft_expr *expr,
51 const struct nlattr * const tb[])
53 struct nft_bitwise *priv = nft_expr_priv(expr);
54 struct nft_data_desc d1, d2;
55 u32 len;
56 int err;
58 if (tb[NFTA_BITWISE_SREG] == NULL ||
59 tb[NFTA_BITWISE_DREG] == NULL ||
60 tb[NFTA_BITWISE_LEN] == NULL ||
61 tb[NFTA_BITWISE_MASK] == NULL ||
62 tb[NFTA_BITWISE_XOR] == NULL)
63 return -EINVAL;
65 err = nft_parse_u32_check(tb[NFTA_BITWISE_LEN], U8_MAX, &len);
66 if (err < 0)
67 return err;
69 priv->len = len;
71 priv->sreg = nft_parse_register(tb[NFTA_BITWISE_SREG]);
72 err = nft_validate_register_load(priv->sreg, priv->len);
73 if (err < 0)
74 return err;
76 priv->dreg = nft_parse_register(tb[NFTA_BITWISE_DREG]);
77 err = nft_validate_register_store(ctx, priv->dreg, NULL,
78 NFT_DATA_VALUE, priv->len);
79 if (err < 0)
80 return err;
82 err = nft_data_init(NULL, &priv->mask, sizeof(priv->mask), &d1,
83 tb[NFTA_BITWISE_MASK]);
84 if (err < 0)
85 return err;
86 if (d1.len != priv->len) {
87 err = -EINVAL;
88 goto err1;
91 err = nft_data_init(NULL, &priv->xor, sizeof(priv->xor), &d2,
92 tb[NFTA_BITWISE_XOR]);
93 if (err < 0)
94 goto err1;
95 if (d2.len != priv->len) {
96 err = -EINVAL;
97 goto err2;
100 return 0;
101 err2:
102 nft_data_release(&priv->xor, d2.type);
103 err1:
104 nft_data_release(&priv->mask, d1.type);
105 return err;
108 static int nft_bitwise_dump(struct sk_buff *skb, const struct nft_expr *expr)
110 const struct nft_bitwise *priv = nft_expr_priv(expr);
112 if (nft_dump_register(skb, NFTA_BITWISE_SREG, priv->sreg))
113 goto nla_put_failure;
114 if (nft_dump_register(skb, NFTA_BITWISE_DREG, priv->dreg))
115 goto nla_put_failure;
116 if (nla_put_be32(skb, NFTA_BITWISE_LEN, htonl(priv->len)))
117 goto nla_put_failure;
119 if (nft_data_dump(skb, NFTA_BITWISE_MASK, &priv->mask,
120 NFT_DATA_VALUE, priv->len) < 0)
121 goto nla_put_failure;
123 if (nft_data_dump(skb, NFTA_BITWISE_XOR, &priv->xor,
124 NFT_DATA_VALUE, priv->len) < 0)
125 goto nla_put_failure;
127 return 0;
129 nla_put_failure:
130 return -1;
133 static const struct nft_expr_ops nft_bitwise_ops = {
134 .type = &nft_bitwise_type,
135 .size = NFT_EXPR_SIZE(sizeof(struct nft_bitwise)),
136 .eval = nft_bitwise_eval,
137 .init = nft_bitwise_init,
138 .dump = nft_bitwise_dump,
141 struct nft_expr_type nft_bitwise_type __read_mostly = {
142 .name = "bitwise",
143 .ops = &nft_bitwise_ops,
144 .policy = nft_bitwise_policy,
145 .maxattr = NFTA_BITWISE_MAX,
146 .owner = THIS_MODULE,