nft_hash.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/netlink.h>
  13. #include <linux/netfilter.h>
  14. #include <linux/netfilter/nf_tables.h>
  15. #include <net/netfilter/nf_tables.h>
  16. #include <net/netfilter/nf_tables_core.h>
  17. #include <linux/jhash.h>
  18. struct nft_hash {
  19. enum nft_registers sreg:8;
  20. enum nft_registers dreg:8;
  21. u8 len;
  22. u32 modulus;
  23. u32 seed;
  24. u32 offset;
  25. };
  26. static void nft_hash_eval(const struct nft_expr *expr,
  27. struct nft_regs *regs,
  28. const struct nft_pktinfo *pkt)
  29. {
  30. struct nft_hash *priv = nft_expr_priv(expr);
  31. const void *data = &regs->data[priv->sreg];
  32. u32 h;
  33. h = reciprocal_scale(jhash(data, priv->len, priv->seed), priv->modulus);
  34. regs->data[priv->dreg] = h + priv->offset;
  35. }
  36. static const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = {
  37. [NFTA_HASH_SREG] = { .type = NLA_U32 },
  38. [NFTA_HASH_DREG] = { .type = NLA_U32 },
  39. [NFTA_HASH_LEN] = { .type = NLA_U32 },
  40. [NFTA_HASH_MODULUS] = { .type = NLA_U32 },
  41. [NFTA_HASH_SEED] = { .type = NLA_U32 },
  42. [NFTA_HASH_OFFSET] = { .type = NLA_U32 },
  43. };
  44. static int nft_hash_init(const struct nft_ctx *ctx,
  45. const struct nft_expr *expr,
  46. const struct nlattr * const tb[])
  47. {
  48. struct nft_hash *priv = nft_expr_priv(expr);
  49. u32 len;
  50. int err;
  51. if (!tb[NFTA_HASH_SREG] ||
  52. !tb[NFTA_HASH_DREG] ||
  53. !tb[NFTA_HASH_LEN] ||
  54. !tb[NFTA_HASH_SEED] ||
  55. !tb[NFTA_HASH_MODULUS])
  56. return -EINVAL;
  57. if (tb[NFTA_HASH_OFFSET])
  58. priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET]));
  59. priv->sreg = nft_parse_register(tb[NFTA_HASH_SREG]);
  60. priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]);
  61. err = nft_parse_u32_check(tb[NFTA_HASH_LEN], U8_MAX, &len);
  62. if (err < 0)
  63. return err;
  64. if (len == 0)
  65. return -ERANGE;
  66. priv->len = len;
  67. priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS]));
  68. if (priv->modulus <= 1)
  69. return -ERANGE;
  70. if (priv->offset + priv->modulus - 1 < priv->offset)
  71. return -EOVERFLOW;
  72. priv->seed = ntohl(nla_get_be32(tb[NFTA_HASH_SEED]));
  73. return nft_validate_register_load(priv->sreg, len) &&
  74. nft_validate_register_store(ctx, priv->dreg, NULL,
  75. NFT_DATA_VALUE, sizeof(u32));
  76. }
  77. static int nft_hash_dump(struct sk_buff *skb,
  78. const struct nft_expr *expr)
  79. {
  80. const struct nft_hash *priv = nft_expr_priv(expr);
  81. if (nft_dump_register(skb, NFTA_HASH_SREG, priv->sreg))
  82. goto nla_put_failure;
  83. if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg))
  84. goto nla_put_failure;
  85. if (nla_put_be32(skb, NFTA_HASH_LEN, htonl(priv->len)))
  86. goto nla_put_failure;
  87. if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus)))
  88. goto nla_put_failure;
  89. if (nla_put_be32(skb, NFTA_HASH_SEED, htonl(priv->seed)))
  90. goto nla_put_failure;
  91. if (priv->offset != 0)
  92. if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset)))
  93. goto nla_put_failure;
  94. return 0;
  95. nla_put_failure:
  96. return -1;
  97. }
  98. static struct nft_expr_type nft_hash_type;
  99. static const struct nft_expr_ops nft_hash_ops = {
  100. .type = &nft_hash_type,
  101. .size = NFT_EXPR_SIZE(sizeof(struct nft_hash)),
  102. .eval = nft_hash_eval,
  103. .init = nft_hash_init,
  104. .dump = nft_hash_dump,
  105. };
  106. static struct nft_expr_type nft_hash_type __read_mostly = {
  107. .name = "hash",
  108. .ops = &nft_hash_ops,
  109. .policy = nft_hash_policy,
  110. .maxattr = NFTA_HASH_MAX,
  111. .owner = THIS_MODULE,
  112. };
  113. static int __init nft_hash_module_init(void)
  114. {
  115. return nft_register_expr(&nft_hash_type);
  116. }
  117. static void __exit nft_hash_module_exit(void)
  118. {
  119. nft_unregister_expr(&nft_hash_type);
  120. }
  121. module_init(nft_hash_module_init);
  122. module_exit(nft_hash_module_exit);
  123. MODULE_LICENSE("GPL");
  124. MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
  125. MODULE_ALIAS_NFT_EXPR("hash");