Linux Audio

Check our new training course

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