2 * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
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.
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>
21 enum nft_registers sreg
:8;
22 enum nft_registers dreg
:8;
29 static void nft_jhash_eval(const struct nft_expr
*expr
,
30 struct nft_regs
*regs
,
31 const struct nft_pktinfo
*pkt
)
33 struct nft_jhash
*priv
= nft_expr_priv(expr
);
34 const void *data
= ®s
->data
[priv
->sreg
];
37 h
= reciprocal_scale(jhash(data
, priv
->len
, priv
->seed
), priv
->modulus
);
38 regs
->data
[priv
->dreg
] = h
+ priv
->offset
;
42 enum nft_registers dreg
:8;
47 static void nft_symhash_eval(const struct nft_expr
*expr
,
48 struct nft_regs
*regs
,
49 const struct nft_pktinfo
*pkt
)
51 struct nft_symhash
*priv
= nft_expr_priv(expr
);
52 struct sk_buff
*skb
= pkt
->skb
;
55 h
= reciprocal_scale(__skb_get_hash_symmetric(skb
), priv
->modulus
);
57 regs
->data
[priv
->dreg
] = h
+ priv
->offset
;
60 static const struct nla_policy nft_hash_policy
[NFTA_HASH_MAX
+ 1] = {
61 [NFTA_HASH_SREG
] = { .type
= NLA_U32
},
62 [NFTA_HASH_DREG
] = { .type
= NLA_U32
},
63 [NFTA_HASH_LEN
] = { .type
= NLA_U32
},
64 [NFTA_HASH_MODULUS
] = { .type
= NLA_U32
},
65 [NFTA_HASH_SEED
] = { .type
= NLA_U32
},
66 [NFTA_HASH_OFFSET
] = { .type
= NLA_U32
},
67 [NFTA_HASH_TYPE
] = { .type
= NLA_U32
},
70 static int nft_jhash_init(const struct nft_ctx
*ctx
,
71 const struct nft_expr
*expr
,
72 const struct nlattr
* const tb
[])
74 struct nft_jhash
*priv
= nft_expr_priv(expr
);
78 if (!tb
[NFTA_HASH_SREG
] ||
79 !tb
[NFTA_HASH_DREG
] ||
81 !tb
[NFTA_HASH_MODULUS
])
84 if (tb
[NFTA_HASH_OFFSET
])
85 priv
->offset
= ntohl(nla_get_be32(tb
[NFTA_HASH_OFFSET
]));
87 priv
->sreg
= nft_parse_register(tb
[NFTA_HASH_SREG
]);
88 priv
->dreg
= nft_parse_register(tb
[NFTA_HASH_DREG
]);
90 err
= nft_parse_u32_check(tb
[NFTA_HASH_LEN
], U8_MAX
, &len
);
98 priv
->modulus
= ntohl(nla_get_be32(tb
[NFTA_HASH_MODULUS
]));
99 if (priv
->modulus
<= 1)
102 if (priv
->offset
+ priv
->modulus
- 1 < priv
->offset
)
105 if (tb
[NFTA_HASH_SEED
])
106 priv
->seed
= ntohl(nla_get_be32(tb
[NFTA_HASH_SEED
]));
108 get_random_bytes(&priv
->seed
, sizeof(priv
->seed
));
110 return nft_validate_register_load(priv
->sreg
, len
) &&
111 nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
112 NFT_DATA_VALUE
, sizeof(u32
));
115 static int nft_symhash_init(const struct nft_ctx
*ctx
,
116 const struct nft_expr
*expr
,
117 const struct nlattr
* const tb
[])
119 struct nft_symhash
*priv
= nft_expr_priv(expr
);
121 if (!tb
[NFTA_HASH_DREG
] ||
122 !tb
[NFTA_HASH_MODULUS
])
125 if (tb
[NFTA_HASH_OFFSET
])
126 priv
->offset
= ntohl(nla_get_be32(tb
[NFTA_HASH_OFFSET
]));
128 priv
->dreg
= nft_parse_register(tb
[NFTA_HASH_DREG
]);
130 priv
->modulus
= ntohl(nla_get_be32(tb
[NFTA_HASH_MODULUS
]));
131 if (priv
->modulus
<= 1)
134 if (priv
->offset
+ priv
->modulus
- 1 < priv
->offset
)
137 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
138 NFT_DATA_VALUE
, sizeof(u32
));
141 static int nft_jhash_dump(struct sk_buff
*skb
,
142 const struct nft_expr
*expr
)
144 const struct nft_jhash
*priv
= nft_expr_priv(expr
);
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 (nla_put_be32(skb
, NFTA_HASH_SEED
, htonl(priv
->seed
)))
155 goto nla_put_failure
;
156 if (priv
->offset
!= 0)
157 if (nla_put_be32(skb
, NFTA_HASH_OFFSET
, htonl(priv
->offset
)))
158 goto nla_put_failure
;
159 if (nla_put_be32(skb
, NFTA_HASH_TYPE
, htonl(NFT_HASH_JENKINS
)))
160 goto nla_put_failure
;
167 static int nft_symhash_dump(struct sk_buff
*skb
,
168 const struct nft_expr
*expr
)
170 const struct nft_symhash
*priv
= nft_expr_priv(expr
);
172 if (nft_dump_register(skb
, NFTA_HASH_DREG
, priv
->dreg
))
173 goto nla_put_failure
;
174 if (nla_put_be32(skb
, NFTA_HASH_MODULUS
, htonl(priv
->modulus
)))
175 goto nla_put_failure
;
176 if (priv
->offset
!= 0)
177 if (nla_put_be32(skb
, NFTA_HASH_OFFSET
, htonl(priv
->offset
)))
178 goto nla_put_failure
;
179 if (nla_put_be32(skb
, NFTA_HASH_TYPE
, htonl(NFT_HASH_SYM
)))
180 goto nla_put_failure
;
187 static struct nft_expr_type nft_hash_type
;
188 static const struct nft_expr_ops nft_jhash_ops
= {
189 .type
= &nft_hash_type
,
190 .size
= NFT_EXPR_SIZE(sizeof(struct nft_jhash
)),
191 .eval
= nft_jhash_eval
,
192 .init
= nft_jhash_init
,
193 .dump
= nft_jhash_dump
,
196 static const struct nft_expr_ops nft_symhash_ops
= {
197 .type
= &nft_hash_type
,
198 .size
= NFT_EXPR_SIZE(sizeof(struct nft_symhash
)),
199 .eval
= nft_symhash_eval
,
200 .init
= nft_symhash_init
,
201 .dump
= nft_symhash_dump
,
204 static const struct nft_expr_ops
*
205 nft_hash_select_ops(const struct nft_ctx
*ctx
,
206 const struct nlattr
* const tb
[])
210 if (!tb
[NFTA_HASH_TYPE
])
211 return &nft_jhash_ops
;
213 type
= ntohl(nla_get_be32(tb
[NFTA_HASH_TYPE
]));
216 return &nft_symhash_ops
;
217 case NFT_HASH_JENKINS
:
218 return &nft_jhash_ops
;
222 return ERR_PTR(-EOPNOTSUPP
);
225 static struct nft_expr_type nft_hash_type __read_mostly
= {
227 .select_ops
= &nft_hash_select_ops
,
228 .policy
= nft_hash_policy
,
229 .maxattr
= NFTA_HASH_MAX
,
230 .owner
= THIS_MODULE
,
233 static int __init
nft_hash_module_init(void)
235 return nft_register_expr(&nft_hash_type
);
238 static void __exit
nft_hash_module_exit(void)
240 nft_unregister_expr(&nft_hash_type
);
243 module_init(nft_hash_module_init
);
244 module_exit(nft_hash_module_exit
);
246 MODULE_LICENSE("GPL");
247 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
248 MODULE_ALIAS_NFT_EXPR("hash");