GNU Linux-libre 4.14.328-gnu1
[releases.git] / net / netfilter / nft_hash.c
1 /*
2  * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
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  */
9
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/netlink.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter/nf_tables.h>
16 #include <net/netfilter/nf_tables.h>
17 #include <net/netfilter/nf_tables_core.h>
18 #include <linux/jhash.h>
19
20 struct nft_jhash {
21         u8                      sreg;
22         u8                      dreg;
23         u8                      len;
24         bool                    autogen_seed:1;
25         u32                     modulus;
26         u32                     seed;
27         u32                     offset;
28 };
29
30 static void nft_jhash_eval(const struct nft_expr *expr,
31                            struct nft_regs *regs,
32                            const struct nft_pktinfo *pkt)
33 {
34         struct nft_jhash *priv = nft_expr_priv(expr);
35         const void *data = &regs->data[priv->sreg];
36         u32 h;
37
38         h = reciprocal_scale(jhash(data, priv->len, priv->seed), priv->modulus);
39         regs->data[priv->dreg] = h + priv->offset;
40 }
41
42 struct nft_symhash {
43         u8                      dreg;
44         u32                     modulus;
45         u32                     offset;
46 };
47
48 static void nft_symhash_eval(const struct nft_expr *expr,
49                              struct nft_regs *regs,
50                              const struct nft_pktinfo *pkt)
51 {
52         struct nft_symhash *priv = nft_expr_priv(expr);
53         struct sk_buff *skb = pkt->skb;
54         u32 h;
55
56         h = reciprocal_scale(__skb_get_hash_symmetric(skb), priv->modulus);
57
58         regs->data[priv->dreg] = h + priv->offset;
59 }
60
61 static const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = {
62         [NFTA_HASH_SREG]        = { .type = NLA_U32 },
63         [NFTA_HASH_DREG]        = { .type = NLA_U32 },
64         [NFTA_HASH_LEN]         = { .type = NLA_U32 },
65         [NFTA_HASH_MODULUS]     = { .type = NLA_U32 },
66         [NFTA_HASH_SEED]        = { .type = NLA_U32 },
67         [NFTA_HASH_OFFSET]      = { .type = NLA_U32 },
68         [NFTA_HASH_TYPE]        = { .type = NLA_U32 },
69 };
70
71 static int nft_jhash_init(const struct nft_ctx *ctx,
72                           const struct nft_expr *expr,
73                           const struct nlattr * const tb[])
74 {
75         struct nft_jhash *priv = nft_expr_priv(expr);
76         u32 len;
77         int err;
78
79         if (!tb[NFTA_HASH_SREG] ||
80             !tb[NFTA_HASH_DREG] ||
81             !tb[NFTA_HASH_LEN]  ||
82             !tb[NFTA_HASH_MODULUS])
83                 return -EINVAL;
84
85         if (tb[NFTA_HASH_OFFSET])
86                 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
87
88         err = nft_parse_u32_check(tb[NFTA_HASH_LEN], U8_MAX, &len);
89         if (err < 0)
90                 return err;
91         if (len == 0)
92                 return -ERANGE;
93
94         priv->len = len;
95
96         err = nft_parse_register_load(tb[NFTA_HASH_SREG], &priv->sreg, len);
97         if (err < 0)
98                 return err;
99
100         priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
101         if (priv->modulus <= 1)
102                 return -ERANGE;
103
104         if (priv->offset + priv->modulus - 1 < priv->offset)
105                 return -EOVERFLOW;
106
107         if (tb[NFTA_HASH_SEED]) {
108                 priv->seed = ntohl(nla_get_be32(tb[NFTA_HASH_SEED]));
109         } else {
110                 priv->autogen_seed = true;
111                 get_random_bytes(&priv->seed, sizeof(priv->seed));
112         }
113
114         return nft_parse_register_store(ctx, tb[NFTA_HASH_DREG], &priv->dreg,
115                                         NULL, NFT_DATA_VALUE, sizeof(u32));
116 }
117
118 static int nft_symhash_init(const struct nft_ctx *ctx,
119                             const struct nft_expr *expr,
120                             const struct nlattr * const tb[])
121 {
122         struct nft_symhash *priv = nft_expr_priv(expr);
123
124         if (!tb[NFTA_HASH_DREG]    ||
125             !tb[NFTA_HASH_MODULUS])
126                 return -EINVAL;
127
128         if (tb[NFTA_HASH_OFFSET])
129                 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
130
131         priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
132         if (priv->modulus < 1)
133                 return -ERANGE;
134
135         if (priv->offset + priv->modulus - 1 < priv->offset)
136                 return -EOVERFLOW;
137
138         return nft_parse_register_store(ctx, tb[NFTA_HASH_DREG],
139                                         &priv->dreg, NULL, NFT_DATA_VALUE,
140                                         sizeof(u32));
141 }
142
143 static int nft_jhash_dump(struct sk_buff *skb,
144                           const struct nft_expr *expr)
145 {
146         const struct nft_jhash *priv = nft_expr_priv(expr);
147
148         if (nft_dump_register(skb, NFTA_HASH_SREG, priv->sreg))
149                 goto nla_put_failure;
150         if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg))
151                 goto nla_put_failure;
152         if (nla_put_be32(skb, NFTA_HASH_LEN, htonl(priv->len)))
153                 goto nla_put_failure;
154         if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus)))
155                 goto nla_put_failure;
156         if (!priv->autogen_seed &&
157             nla_put_be32(skb, NFTA_HASH_SEED, htonl(priv->seed)))
158                 goto nla_put_failure;
159         if (priv->offset != 0)
160                 if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset)))
161                         goto nla_put_failure;
162         if (nla_put_be32(skb, NFTA_HASH_TYPE, htonl(NFT_HASH_JENKINS)))
163                 goto nla_put_failure;
164         return 0;
165
166 nla_put_failure:
167         return -1;
168 }
169
170 static int nft_symhash_dump(struct sk_buff *skb,
171                             const struct nft_expr *expr)
172 {
173         const struct nft_symhash *priv = nft_expr_priv(expr);
174
175         if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg))
176                 goto nla_put_failure;
177         if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus)))
178                 goto nla_put_failure;
179         if (priv->offset != 0)
180                 if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset)))
181                         goto nla_put_failure;
182         if (nla_put_be32(skb, NFTA_HASH_TYPE, htonl(NFT_HASH_SYM)))
183                 goto nla_put_failure;
184         return 0;
185
186 nla_put_failure:
187         return -1;
188 }
189
190 static struct nft_expr_type nft_hash_type;
191 static const struct nft_expr_ops nft_jhash_ops = {
192         .type           = &nft_hash_type,
193         .size           = NFT_EXPR_SIZE(sizeof(struct nft_jhash)),
194         .eval           = nft_jhash_eval,
195         .init           = nft_jhash_init,
196         .dump           = nft_jhash_dump,
197 };
198
199 static const struct nft_expr_ops nft_symhash_ops = {
200         .type           = &nft_hash_type,
201         .size           = NFT_EXPR_SIZE(sizeof(struct nft_symhash)),
202         .eval           = nft_symhash_eval,
203         .init           = nft_symhash_init,
204         .dump           = nft_symhash_dump,
205 };
206
207 static const struct nft_expr_ops *
208 nft_hash_select_ops(const struct nft_ctx *ctx,
209                     const struct nlattr * const tb[])
210 {
211         u32 type;
212
213         if (!tb[NFTA_HASH_TYPE])
214                 return &nft_jhash_ops;
215
216         type = ntohl(nla_get_be32(tb[NFTA_HASH_TYPE]));
217         switch (type) {
218         case NFT_HASH_SYM:
219                 return &nft_symhash_ops;
220         case NFT_HASH_JENKINS:
221                 return &nft_jhash_ops;
222         default:
223                 break;
224         }
225         return ERR_PTR(-EOPNOTSUPP);
226 }
227
228 static struct nft_expr_type nft_hash_type __read_mostly = {
229         .name           = "hash",
230         .select_ops     = nft_hash_select_ops,
231         .policy         = nft_hash_policy,
232         .maxattr        = NFTA_HASH_MAX,
233         .owner          = THIS_MODULE,
234 };
235
236 static int __init nft_hash_module_init(void)
237 {
238         return nft_register_expr(&nft_hash_type);
239 }
240
241 static void __exit nft_hash_module_exit(void)
242 {
243         nft_unregister_expr(&nft_hash_type);
244 }
245
246 module_init(nft_hash_module_init);
247 module_exit(nft_hash_module_exit);
248
249 MODULE_LICENSE("GPL");
250 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
251 MODULE_ALIAS_NFT_EXPR("hash");