]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/nft_hash.c
netfilter: handle NF_REPEAT from nf_conntrack_in()
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / nft_hash.c
CommitLineData
cb1b69b0
LGL
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
20struct nft_hash {
21 enum nft_registers sreg:8;
22 enum nft_registers dreg:8;
23 u8 len;
24 u32 modulus;
25 u32 seed;
70ca767e 26 u32 offset;
cb1b69b0
LGL
27};
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];
70ca767e 35 u32 h;
cb1b69b0 36
70ca767e
LGL
37 h = reciprocal_scale(jhash(data, priv->len, priv->seed), priv->modulus);
38 regs->data[priv->dreg] = h + priv->offset;
cb1b69b0
LGL
39}
40
a5e57336 41static const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = {
cb1b69b0
LGL
42 [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 },
5751e175 47 [NFTA_HASH_OFFSET] = { .type = NLA_U32 },
cb1b69b0
LGL
48};
49
50static int nft_hash_init(const struct nft_ctx *ctx,
51 const struct nft_expr *expr,
52 const struct nlattr * const tb[])
53{
54 struct nft_hash *priv = nft_expr_priv(expr);
55 u32 len;
56
57 if (!tb[NFTA_HASH_SREG] ||
58 !tb[NFTA_HASH_DREG] ||
59 !tb[NFTA_HASH_LEN] ||
60 !tb[NFTA_HASH_SEED] ||
61 !tb[NFTA_HASH_MODULUS])
62 return -EINVAL;
63
70ca767e
LGL
64 if (tb[NFTA_HASH_OFFSET])
65 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
66
cb1b69b0
LGL
67 priv->sreg = nft_parse_register(tb[NFTA_HASH_SREG]);
68 priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]);
69
70 len = ntohl(nla_get_be32(tb[NFTA_HASH_LEN]));
71 if (len == 0 || len > U8_MAX)
72 return -ERANGE;
73
74 priv->len = len;
75
76 priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
77 if (priv->modulus <= 1)
78 return -ERANGE;
79
14e2dee0 80 if (priv->offset + priv->modulus - 1 < priv->offset)
70ca767e
LGL
81 return -EOVERFLOW;
82
cb1b69b0
LGL
83 priv->seed = ntohl(nla_get_be32(tb[NFTA_HASH_SEED]));
84
85 return nft_validate_register_load(priv->sreg, len) &&
86 nft_validate_register_store(ctx, priv->dreg, NULL,
87 NFT_DATA_VALUE, sizeof(u32));
88}
89
90static int nft_hash_dump(struct sk_buff *skb,
91 const struct nft_expr *expr)
92{
93 const struct nft_hash *priv = nft_expr_priv(expr);
94
95 if (nft_dump_register(skb, NFTA_HASH_SREG, priv->sreg))
96 goto nla_put_failure;
97 if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg))
98 goto nla_put_failure;
7073b16f 99 if (nla_put_be32(skb, NFTA_HASH_LEN, htonl(priv->len)))
cb1b69b0 100 goto nla_put_failure;
7073b16f 101 if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus)))
cb1b69b0 102 goto nla_put_failure;
7073b16f 103 if (nla_put_be32(skb, NFTA_HASH_SEED, htonl(priv->seed)))
cb1b69b0 104 goto nla_put_failure;
70ca767e
LGL
105 if (priv->offset != 0)
106 if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset)))
107 goto nla_put_failure;
cb1b69b0
LGL
108 return 0;
109
110nla_put_failure:
111 return -1;
112}
113
114static struct nft_expr_type nft_hash_type;
115static const struct nft_expr_ops nft_hash_ops = {
116 .type = &nft_hash_type,
117 .size = NFT_EXPR_SIZE(sizeof(struct nft_hash)),
118 .eval = nft_hash_eval,
119 .init = nft_hash_init,
120 .dump = nft_hash_dump,
121};
122
123static struct nft_expr_type nft_hash_type __read_mostly = {
124 .name = "hash",
125 .ops = &nft_hash_ops,
126 .policy = nft_hash_policy,
127 .maxattr = NFTA_HASH_MAX,
128 .owner = THIS_MODULE,
129};
130
131static int __init nft_hash_module_init(void)
132{
133 return nft_register_expr(&nft_hash_type);
134}
135
136static void __exit nft_hash_module_exit(void)
137{
138 nft_unregister_expr(&nft_hash_type);
139}
140
141module_init(nft_hash_module_init);
142module_exit(nft_hash_module_exit);
143
144MODULE_LICENSE("GPL");
145MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
146MODULE_ALIAS_NFT_EXPR("hash");