Skip to content

Commit cb1b69b

Browse files
Laura Garcia Liebanaummakynes
authored andcommitted
netfilter: nf_tables: add hash expression
This patch adds a new hash expression, this provides jhash support but this can be extended to support for other hash functions. The modulus and seed already comes embedded into this new expression. Use case example: ... meta mark set hash ip saddr mod 10 Signed-off-by: Laura Garcia Liebana <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 0ed6389 commit cb1b69b

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

include/uapi/linux/netfilter/nf_tables.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,26 @@ enum nft_meta_keys {
723723
NFT_META_PRANDOM,
724724
};
725725

726+
/**
727+
* enum nft_hash_attributes - nf_tables hash expression netlink attributes
728+
*
729+
* @NFTA_HASH_SREG: source register (NLA_U32)
730+
* @NFTA_HASH_DREG: destination register (NLA_U32)
731+
* @NFTA_HASH_LEN: source data length (NLA_U32)
732+
* @NFTA_HASH_MODULUS: modulus value (NLA_U32)
733+
* @NFTA_HASH_SEED: seed value (NLA_U32)
734+
*/
735+
enum nft_hash_attributes {
736+
NFTA_HASH_UNSPEC,
737+
NFTA_HASH_SREG,
738+
NFTA_HASH_DREG,
739+
NFTA_HASH_LEN,
740+
NFTA_HASH_MODULUS,
741+
NFTA_HASH_SEED,
742+
__NFTA_HASH_MAX,
743+
};
744+
#define NFTA_HASH_MAX (__NFTA_HASH_MAX - 1)
745+
726746
/**
727747
* enum nft_meta_attributes - nf_tables meta expression netlink attributes
728748
*

net/netfilter/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,12 @@ config NFT_COMPAT
563563
x_tables match/target extensions over the nf_tables
564564
framework.
565565

566+
config NFT_HASH
567+
tristate "Netfilter nf_tables hash module"
568+
help
569+
This option adds the "hash" expression that you can use to perform
570+
a hash operation on registers.
571+
566572
if NF_TABLES_NETDEV
567573

568574
config NF_DUP_NETDEV

net/netfilter/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ obj-$(CONFIG_NFT_COUNTER) += nft_counter.o
9292
obj-$(CONFIG_NFT_LOG) += nft_log.o
9393
obj-$(CONFIG_NFT_MASQ) += nft_masq.o
9494
obj-$(CONFIG_NFT_REDIR) += nft_redir.o
95+
obj-$(CONFIG_NFT_HASH) += nft_hash.o
9596

9697
# nf_tables netdev
9798
obj-$(CONFIG_NFT_DUP_NETDEV) += nft_dup_netdev.o

net/netfilter/nft_hash.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright (c) 2016 Laura Garcia <[email protected]>
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_hash {
21+
enum nft_registers sreg:8;
22+
enum nft_registers dreg:8;
23+
u8 len;
24+
u32 modulus;
25+
u32 seed;
26+
};
27+
28+
static void nft_hash_eval(const struct nft_expr *expr,
29+
struct nft_regs *regs,
30+
const struct nft_pktinfo *pkt)
31+
{
32+
struct nft_hash *priv = nft_expr_priv(expr);
33+
const void *data = &regs->data[priv->sreg];
34+
35+
regs->data[priv->dreg] =
36+
reciprocal_scale(jhash(data, priv->len, priv->seed),
37+
priv->modulus);
38+
}
39+
40+
const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = {
41+
[NFTA_HASH_SREG] = { .type = NLA_U32 },
42+
[NFTA_HASH_DREG] = { .type = NLA_U32 },
43+
[NFTA_HASH_LEN] = { .type = NLA_U32 },
44+
[NFTA_HASH_MODULUS] = { .type = NLA_U32 },
45+
[NFTA_HASH_SEED] = { .type = NLA_U32 },
46+
};
47+
48+
static int nft_hash_init(const struct nft_ctx *ctx,
49+
const struct nft_expr *expr,
50+
const struct nlattr * const tb[])
51+
{
52+
struct nft_hash *priv = nft_expr_priv(expr);
53+
u32 len;
54+
55+
if (!tb[NFTA_HASH_SREG] ||
56+
!tb[NFTA_HASH_DREG] ||
57+
!tb[NFTA_HASH_LEN] ||
58+
!tb[NFTA_HASH_SEED] ||
59+
!tb[NFTA_HASH_MODULUS])
60+
return -EINVAL;
61+
62+
priv->sreg = nft_parse_register(tb[NFTA_HASH_SREG]);
63+
priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]);
64+
65+
len = ntohl(nla_get_be32(tb[NFTA_HASH_LEN]));
66+
if (len == 0 || len > U8_MAX)
67+
return -ERANGE;
68+
69+
priv->len = len;
70+
71+
priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
72+
if (priv->modulus <= 1)
73+
return -ERANGE;
74+
75+
priv->seed = ntohl(nla_get_be32(tb[NFTA_HASH_SEED]));
76+
77+
return nft_validate_register_load(priv->sreg, len) &&
78+
nft_validate_register_store(ctx, priv->dreg, NULL,
79+
NFT_DATA_VALUE, sizeof(u32));
80+
}
81+
82+
static int nft_hash_dump(struct sk_buff *skb,
83+
const struct nft_expr *expr)
84+
{
85+
const struct nft_hash *priv = nft_expr_priv(expr);
86+
87+
if (nft_dump_register(skb, NFTA_HASH_SREG, priv->sreg))
88+
goto nla_put_failure;
89+
if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg))
90+
goto nla_put_failure;
91+
if (nft_dump_register(skb, NFTA_HASH_LEN, priv->len))
92+
goto nla_put_failure;
93+
if (nft_dump_register(skb, NFTA_HASH_MODULUS, priv->modulus))
94+
goto nla_put_failure;
95+
if (nft_dump_register(skb, NFTA_HASH_SEED, priv->seed))
96+
goto nla_put_failure;
97+
98+
return 0;
99+
100+
nla_put_failure:
101+
return -1;
102+
}
103+
104+
static struct nft_expr_type nft_hash_type;
105+
static const struct nft_expr_ops nft_hash_ops = {
106+
.type = &nft_hash_type,
107+
.size = NFT_EXPR_SIZE(sizeof(struct nft_hash)),
108+
.eval = nft_hash_eval,
109+
.init = nft_hash_init,
110+
.dump = nft_hash_dump,
111+
};
112+
113+
static struct nft_expr_type nft_hash_type __read_mostly = {
114+
.name = "hash",
115+
.ops = &nft_hash_ops,
116+
.policy = nft_hash_policy,
117+
.maxattr = NFTA_HASH_MAX,
118+
.owner = THIS_MODULE,
119+
};
120+
121+
static int __init nft_hash_module_init(void)
122+
{
123+
return nft_register_expr(&nft_hash_type);
124+
}
125+
126+
static void __exit nft_hash_module_exit(void)
127+
{
128+
nft_unregister_expr(&nft_hash_type);
129+
}
130+
131+
module_init(nft_hash_module_init);
132+
module_exit(nft_hash_module_exit);
133+
134+
MODULE_LICENSE("GPL");
135+
MODULE_AUTHOR("Laura Garcia <[email protected]>");
136+
MODULE_ALIAS_NFT_EXPR("hash");

0 commit comments

Comments
 (0)