Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 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 | |
| 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; |
Laura Garcia Liebana | 70ca767 | 2016-09-06 08:44:19 +0200 | [diff] [blame] | 26 | u32 offset; |
Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | static 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 = ®s->data[priv->sreg]; |
Laura Garcia Liebana | 70ca767 | 2016-09-06 08:44:19 +0200 | [diff] [blame] | 35 | u32 h; |
Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 36 | |
Laura Garcia Liebana | 70ca767 | 2016-09-06 08:44:19 +0200 | [diff] [blame] | 37 | h = reciprocal_scale(jhash(data, priv->len, priv->seed), priv->modulus); |
| 38 | regs->data[priv->dreg] = h + priv->offset; |
Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Wei Yongjun | a5e5733 | 2016-08-21 15:21:10 +0000 | [diff] [blame] | 41 | static const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = { |
Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 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 }, |
Liping Zhang | 5751e17 | 2016-10-12 21:10:45 +0800 | [diff] [blame] | 47 | [NFTA_HASH_OFFSET] = { .type = NLA_U32 }, |
Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | static 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; |
Laura Garcia Liebana | abd66e9 | 2016-11-14 22:33:34 +0100 | [diff] [blame] | 56 | int err; |
Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 57 | |
| 58 | if (!tb[NFTA_HASH_SREG] || |
| 59 | !tb[NFTA_HASH_DREG] || |
| 60 | !tb[NFTA_HASH_LEN] || |
| 61 | !tb[NFTA_HASH_SEED] || |
| 62 | !tb[NFTA_HASH_MODULUS]) |
| 63 | return -EINVAL; |
| 64 | |
Laura Garcia Liebana | 70ca767 | 2016-09-06 08:44:19 +0200 | [diff] [blame] | 65 | if (tb[NFTA_HASH_OFFSET]) |
| 66 | priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET])); |
| 67 | |
Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 68 | priv->sreg = nft_parse_register(tb[NFTA_HASH_SREG]); |
| 69 | priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]); |
| 70 | |
Laura Garcia Liebana | abd66e9 | 2016-11-14 22:33:34 +0100 | [diff] [blame] | 71 | err = nft_parse_u32_check(tb[NFTA_HASH_LEN], U8_MAX, &len); |
| 72 | if (err < 0) |
| 73 | return err; |
| 74 | if (len == 0) |
Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 75 | return -ERANGE; |
| 76 | |
| 77 | priv->len = len; |
| 78 | |
| 79 | priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS])); |
| 80 | if (priv->modulus <= 1) |
| 81 | return -ERANGE; |
| 82 | |
Laura Garcia Liebana | 14e2dee | 2016-09-13 10:21:46 +0200 | [diff] [blame] | 83 | if (priv->offset + priv->modulus - 1 < priv->offset) |
Laura Garcia Liebana | 70ca767 | 2016-09-06 08:44:19 +0200 | [diff] [blame] | 84 | return -EOVERFLOW; |
| 85 | |
Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 86 | priv->seed = ntohl(nla_get_be32(tb[NFTA_HASH_SEED])); |
| 87 | |
| 88 | return nft_validate_register_load(priv->sreg, len) && |
| 89 | nft_validate_register_store(ctx, priv->dreg, NULL, |
| 90 | NFT_DATA_VALUE, sizeof(u32)); |
| 91 | } |
| 92 | |
| 93 | static int nft_hash_dump(struct sk_buff *skb, |
| 94 | const struct nft_expr *expr) |
| 95 | { |
| 96 | const struct nft_hash *priv = nft_expr_priv(expr); |
| 97 | |
| 98 | if (nft_dump_register(skb, NFTA_HASH_SREG, priv->sreg)) |
| 99 | goto nla_put_failure; |
| 100 | if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg)) |
| 101 | goto nla_put_failure; |
Pablo Neira Ayuso | 7073b16 | 2016-08-26 13:42:17 +0200 | [diff] [blame] | 102 | if (nla_put_be32(skb, NFTA_HASH_LEN, htonl(priv->len))) |
Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 103 | goto nla_put_failure; |
Pablo Neira Ayuso | 7073b16 | 2016-08-26 13:42:17 +0200 | [diff] [blame] | 104 | if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus))) |
Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 105 | goto nla_put_failure; |
Pablo Neira Ayuso | 7073b16 | 2016-08-26 13:42:17 +0200 | [diff] [blame] | 106 | if (nla_put_be32(skb, NFTA_HASH_SEED, htonl(priv->seed))) |
Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 107 | goto nla_put_failure; |
Laura Garcia Liebana | 70ca767 | 2016-09-06 08:44:19 +0200 | [diff] [blame] | 108 | if (priv->offset != 0) |
| 109 | if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset))) |
| 110 | goto nla_put_failure; |
Laura Garcia Liebana | cb1b69b | 2016-08-11 18:02:07 +0200 | [diff] [blame] | 111 | return 0; |
| 112 | |
| 113 | nla_put_failure: |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | static struct nft_expr_type nft_hash_type; |
| 118 | static const struct nft_expr_ops nft_hash_ops = { |
| 119 | .type = &nft_hash_type, |
| 120 | .size = NFT_EXPR_SIZE(sizeof(struct nft_hash)), |
| 121 | .eval = nft_hash_eval, |
| 122 | .init = nft_hash_init, |
| 123 | .dump = nft_hash_dump, |
| 124 | }; |
| 125 | |
| 126 | static struct nft_expr_type nft_hash_type __read_mostly = { |
| 127 | .name = "hash", |
| 128 | .ops = &nft_hash_ops, |
| 129 | .policy = nft_hash_policy, |
| 130 | .maxattr = NFTA_HASH_MAX, |
| 131 | .owner = THIS_MODULE, |
| 132 | }; |
| 133 | |
| 134 | static int __init nft_hash_module_init(void) |
| 135 | { |
| 136 | return nft_register_expr(&nft_hash_type); |
| 137 | } |
| 138 | |
| 139 | static void __exit nft_hash_module_exit(void) |
| 140 | { |
| 141 | nft_unregister_expr(&nft_hash_type); |
| 142 | } |
| 143 | |
| 144 | module_init(nft_hash_module_init); |
| 145 | module_exit(nft_hash_module_exit); |
| 146 | |
| 147 | MODULE_LICENSE("GPL"); |
| 148 | MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>"); |
| 149 | MODULE_ALIAS_NFT_EXPR("hash"); |