blob: 09473b415b95b281c3264cda23d446dd5a3d56ab [file] [log] [blame]
Laura Garcia Liebanacb1b69b2016-08-11 18:02:07 +02001/*
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
20struct nft_hash {
21 enum nft_registers sreg:8;
22 enum nft_registers dreg:8;
23 u8 len;
24 u32 modulus;
25 u32 seed;
Laura Garcia Liebana70ca7672016-09-06 08:44:19 +020026 u32 offset;
Laura Garcia Liebanacb1b69b2016-08-11 18:02:07 +020027};
28
29static void nft_hash_eval(const struct nft_expr *expr,
30 struct nft_regs *regs,
31 const struct nft_pktinfo *pkt)
32{
33 struct nft_hash *priv = nft_expr_priv(expr);
34 const void *data = &regs->data[priv->sreg];
Laura Garcia Liebana70ca7672016-09-06 08:44:19 +020035 u32 h;
Laura Garcia Liebanacb1b69b2016-08-11 18:02:07 +020036
Laura Garcia Liebana70ca7672016-09-06 08:44:19 +020037 h = reciprocal_scale(jhash(data, priv->len, priv->seed), priv->modulus);
38 regs->data[priv->dreg] = h + priv->offset;
Laura Garcia Liebanacb1b69b2016-08-11 18:02:07 +020039}
40
Wei Yongjuna5e57332016-08-21 15:21:10 +000041static const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = {
Laura Garcia Liebanacb1b69b2016-08-11 18:02:07 +020042 [NFTA_HASH_SREG] = { .type = NLA_U32 },
43 [NFTA_HASH_DREG] = { .type = NLA_U32 },
44 [NFTA_HASH_LEN] = { .type = NLA_U32 },
45 [NFTA_HASH_MODULUS] = { .type = NLA_U32 },
46 [NFTA_HASH_SEED] = { .type = NLA_U32 },
47};
48
49static int nft_hash_init(const struct nft_ctx *ctx,
50 const struct nft_expr *expr,
51 const struct nlattr * const tb[])
52{
53 struct nft_hash *priv = nft_expr_priv(expr);
54 u32 len;
55
56 if (!tb[NFTA_HASH_SREG] ||
57 !tb[NFTA_HASH_DREG] ||
58 !tb[NFTA_HASH_LEN] ||
59 !tb[NFTA_HASH_SEED] ||
60 !tb[NFTA_HASH_MODULUS])
61 return -EINVAL;
62
Laura Garcia Liebana70ca7672016-09-06 08:44:19 +020063 if (tb[NFTA_HASH_OFFSET])
64 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
65
Laura Garcia Liebanacb1b69b2016-08-11 18:02:07 +020066 priv->sreg = nft_parse_register(tb[NFTA_HASH_SREG]);
67 priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]);
68
69 len = ntohl(nla_get_be32(tb[NFTA_HASH_LEN]));
70 if (len == 0 || len > U8_MAX)
71 return -ERANGE;
72
73 priv->len = len;
74
75 priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
76 if (priv->modulus <= 1)
77 return -ERANGE;
78
Laura Garcia Liebana14e2dee2016-09-13 10:21:46 +020079 if (priv->offset + priv->modulus - 1 < priv->offset)
Laura Garcia Liebana70ca7672016-09-06 08:44:19 +020080 return -EOVERFLOW;
81
Laura Garcia Liebanacb1b69b2016-08-11 18:02:07 +020082 priv->seed = ntohl(nla_get_be32(tb[NFTA_HASH_SEED]));
83
84 return nft_validate_register_load(priv->sreg, len) &&
85 nft_validate_register_store(ctx, priv->dreg, NULL,
86 NFT_DATA_VALUE, sizeof(u32));
87}
88
89static int nft_hash_dump(struct sk_buff *skb,
90 const struct nft_expr *expr)
91{
92 const struct nft_hash *priv = nft_expr_priv(expr);
93
94 if (nft_dump_register(skb, NFTA_HASH_SREG, priv->sreg))
95 goto nla_put_failure;
96 if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg))
97 goto nla_put_failure;
Pablo Neira Ayuso7073b162016-08-26 13:42:17 +020098 if (nla_put_be32(skb, NFTA_HASH_LEN, htonl(priv->len)))
Laura Garcia Liebanacb1b69b2016-08-11 18:02:07 +020099 goto nla_put_failure;
Pablo Neira Ayuso7073b162016-08-26 13:42:17 +0200100 if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus)))
Laura Garcia Liebanacb1b69b2016-08-11 18:02:07 +0200101 goto nla_put_failure;
Pablo Neira Ayuso7073b162016-08-26 13:42:17 +0200102 if (nla_put_be32(skb, NFTA_HASH_SEED, htonl(priv->seed)))
Laura Garcia Liebanacb1b69b2016-08-11 18:02:07 +0200103 goto nla_put_failure;
Laura Garcia Liebana70ca7672016-09-06 08:44:19 +0200104 if (priv->offset != 0)
105 if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset)))
106 goto nla_put_failure;
Laura Garcia Liebanacb1b69b2016-08-11 18:02:07 +0200107 return 0;
108
109nla_put_failure:
110 return -1;
111}
112
113static struct nft_expr_type nft_hash_type;
114static const struct nft_expr_ops nft_hash_ops = {
115 .type = &nft_hash_type,
116 .size = NFT_EXPR_SIZE(sizeof(struct nft_hash)),
117 .eval = nft_hash_eval,
118 .init = nft_hash_init,
119 .dump = nft_hash_dump,
120};
121
122static struct nft_expr_type nft_hash_type __read_mostly = {
123 .name = "hash",
124 .ops = &nft_hash_ops,
125 .policy = nft_hash_policy,
126 .maxattr = NFTA_HASH_MAX,
127 .owner = THIS_MODULE,
128};
129
130static int __init nft_hash_module_init(void)
131{
132 return nft_register_expr(&nft_hash_type);
133}
134
135static void __exit nft_hash_module_exit(void)
136{
137 nft_unregister_expr(&nft_hash_type);
138}
139
140module_init(nft_hash_module_init);
141module_exit(nft_hash_module_exit);
142
143MODULE_LICENSE("GPL");
144MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
145MODULE_ALIAS_NFT_EXPR("hash");