blob: afad27c700a1626bceded5ab1a635f97ab39f045 [file] [log] [blame]
Patrick McHardy96518512013-10-14 11:00:02 +02001/*
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
Patrick McHardy96518512013-10-14 11:00:02 +02003 *
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
20struct 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
28static void nft_bitwise_eval(const struct nft_expr *expr,
29 struct nft_data data[NFT_REG_MAX + 1],
30 const struct nft_pktinfo *pkt)
31{
32 const struct nft_bitwise *priv = nft_expr_priv(expr);
33 const struct nft_data *src = &data[priv->sreg];
34 struct nft_data *dst = &data[priv->dreg];
35 unsigned int i;
36
37 for (i = 0; i < DIV_ROUND_UP(priv->len, 4); i++) {
38 dst->data[i] = (src->data[i] & priv->mask.data[i]) ^
39 priv->xor.data[i];
40 }
41}
42
43static const struct nla_policy nft_bitwise_policy[NFTA_BITWISE_MAX + 1] = {
44 [NFTA_BITWISE_SREG] = { .type = NLA_U32 },
45 [NFTA_BITWISE_DREG] = { .type = NLA_U32 },
46 [NFTA_BITWISE_LEN] = { .type = NLA_U32 },
47 [NFTA_BITWISE_MASK] = { .type = NLA_NESTED },
48 [NFTA_BITWISE_XOR] = { .type = NLA_NESTED },
49};
50
51static int nft_bitwise_init(const struct nft_ctx *ctx,
52 const struct nft_expr *expr,
53 const struct nlattr * const tb[])
54{
55 struct nft_bitwise *priv = nft_expr_priv(expr);
56 struct nft_data_desc d1, d2;
57 int err;
58
59 if (tb[NFTA_BITWISE_SREG] == NULL ||
60 tb[NFTA_BITWISE_DREG] == NULL ||
61 tb[NFTA_BITWISE_LEN] == NULL ||
62 tb[NFTA_BITWISE_MASK] == NULL ||
63 tb[NFTA_BITWISE_XOR] == NULL)
64 return -EINVAL;
65
Patrick McHardy45d9bcd2015-04-11 02:27:26 +010066 priv->len = ntohl(nla_get_be32(tb[NFTA_BITWISE_LEN]));
67
Patrick McHardy96518512013-10-14 11:00:02 +020068 priv->sreg = ntohl(nla_get_be32(tb[NFTA_BITWISE_SREG]));
69 err = nft_validate_input_register(priv->sreg);
70 if (err < 0)
71 return err;
72
73 priv->dreg = ntohl(nla_get_be32(tb[NFTA_BITWISE_DREG]));
74 err = nft_validate_output_register(priv->dreg);
75 if (err < 0)
76 return err;
Patrick McHardy45d9bcd2015-04-11 02:27:26 +010077
Patrick McHardy1ec10212015-04-11 02:27:27 +010078 err = nft_validate_register_store(ctx, priv->dreg, NULL,
79 NFT_DATA_VALUE, priv->len);
Patrick McHardy96518512013-10-14 11:00:02 +020080 if (err < 0)
81 return err;
82
Patrick McHardy96518512013-10-14 11:00:02 +020083 err = nft_data_init(NULL, &priv->mask, &d1, tb[NFTA_BITWISE_MASK]);
84 if (err < 0)
85 return err;
86 if (d1.len != priv->len)
87 return -EINVAL;
88
89 err = nft_data_init(NULL, &priv->xor, &d2, tb[NFTA_BITWISE_XOR]);
90 if (err < 0)
91 return err;
92 if (d2.len != priv->len)
93 return -EINVAL;
94
95 return 0;
96}
97
98static int nft_bitwise_dump(struct sk_buff *skb, const struct nft_expr *expr)
99{
100 const struct nft_bitwise *priv = nft_expr_priv(expr);
101
102 if (nla_put_be32(skb, NFTA_BITWISE_SREG, htonl(priv->sreg)))
103 goto nla_put_failure;
104 if (nla_put_be32(skb, NFTA_BITWISE_DREG, htonl(priv->dreg)))
105 goto nla_put_failure;
106 if (nla_put_be32(skb, NFTA_BITWISE_LEN, htonl(priv->len)))
107 goto nla_put_failure;
108
109 if (nft_data_dump(skb, NFTA_BITWISE_MASK, &priv->mask,
110 NFT_DATA_VALUE, priv->len) < 0)
111 goto nla_put_failure;
112
113 if (nft_data_dump(skb, NFTA_BITWISE_XOR, &priv->xor,
114 NFT_DATA_VALUE, priv->len) < 0)
115 goto nla_put_failure;
116
117 return 0;
118
119nla_put_failure:
120 return -1;
121}
122
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200123static struct nft_expr_type nft_bitwise_type;
124static const struct nft_expr_ops nft_bitwise_ops = {
125 .type = &nft_bitwise_type,
Patrick McHardy96518512013-10-14 11:00:02 +0200126 .size = NFT_EXPR_SIZE(sizeof(struct nft_bitwise)),
Patrick McHardy96518512013-10-14 11:00:02 +0200127 .eval = nft_bitwise_eval,
128 .init = nft_bitwise_init,
129 .dump = nft_bitwise_dump,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200130};
131
132static struct nft_expr_type nft_bitwise_type __read_mostly = {
133 .name = "bitwise",
134 .ops = &nft_bitwise_ops,
Patrick McHardy96518512013-10-14 11:00:02 +0200135 .policy = nft_bitwise_policy,
136 .maxattr = NFTA_BITWISE_MAX,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200137 .owner = THIS_MODULE,
Patrick McHardy96518512013-10-14 11:00:02 +0200138};
139
140int __init nft_bitwise_module_init(void)
141{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200142 return nft_register_expr(&nft_bitwise_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200143}
144
145void nft_bitwise_module_exit(void)
146{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200147 nft_unregister_expr(&nft_bitwise_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200148}