nft_queue.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2013 Eric Leblond <eric@regit.org>
  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. * Development of this code partly funded by OISF
  9. * (http://www.openinfosecfoundation.org/)
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/netlink.h>
  15. #include <linux/jhash.h>
  16. #include <linux/netfilter.h>
  17. #include <linux/netfilter/nf_tables.h>
  18. #include <net/netfilter/nf_tables.h>
  19. #include <net/netfilter/nf_queue.h>
  20. static u32 jhash_initval __read_mostly;
  21. struct nft_queue {
  22. u16 queuenum;
  23. u16 queues_total;
  24. u16 flags;
  25. };
  26. static void nft_queue_eval(const struct nft_expr *expr,
  27. struct nft_regs *regs,
  28. const struct nft_pktinfo *pkt)
  29. {
  30. struct nft_queue *priv = nft_expr_priv(expr);
  31. u32 queue = priv->queuenum;
  32. u32 ret;
  33. if (priv->queues_total > 1) {
  34. if (priv->flags & NFT_QUEUE_FLAG_CPU_FANOUT) {
  35. int cpu = raw_smp_processor_id();
  36. queue = priv->queuenum + cpu % priv->queues_total;
  37. } else {
  38. queue = nfqueue_hash(pkt->skb, queue,
  39. priv->queues_total, pkt->pf,
  40. jhash_initval);
  41. }
  42. }
  43. ret = NF_QUEUE_NR(queue);
  44. if (priv->flags & NFT_QUEUE_FLAG_BYPASS)
  45. ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
  46. regs->verdict.code = ret;
  47. }
  48. static const struct nla_policy nft_queue_policy[NFTA_QUEUE_MAX + 1] = {
  49. [NFTA_QUEUE_NUM] = { .type = NLA_U16 },
  50. [NFTA_QUEUE_TOTAL] = { .type = NLA_U16 },
  51. [NFTA_QUEUE_FLAGS] = { .type = NLA_U16 },
  52. };
  53. static int nft_queue_init(const struct nft_ctx *ctx,
  54. const struct nft_expr *expr,
  55. const struct nlattr * const tb[])
  56. {
  57. struct nft_queue *priv = nft_expr_priv(expr);
  58. if (tb[NFTA_QUEUE_NUM] == NULL)
  59. return -EINVAL;
  60. init_hashrandom(&jhash_initval);
  61. priv->queuenum = ntohs(nla_get_be16(tb[NFTA_QUEUE_NUM]));
  62. if (tb[NFTA_QUEUE_TOTAL] != NULL)
  63. priv->queues_total = ntohs(nla_get_be16(tb[NFTA_QUEUE_TOTAL]));
  64. if (tb[NFTA_QUEUE_FLAGS] != NULL) {
  65. priv->flags = ntohs(nla_get_be16(tb[NFTA_QUEUE_FLAGS]));
  66. if (priv->flags & ~NFT_QUEUE_FLAG_MASK)
  67. return -EINVAL;
  68. }
  69. return 0;
  70. }
  71. static int nft_queue_dump(struct sk_buff *skb, const struct nft_expr *expr)
  72. {
  73. const struct nft_queue *priv = nft_expr_priv(expr);
  74. if (nla_put_be16(skb, NFTA_QUEUE_NUM, htons(priv->queuenum)) ||
  75. nla_put_be16(skb, NFTA_QUEUE_TOTAL, htons(priv->queues_total)) ||
  76. nla_put_be16(skb, NFTA_QUEUE_FLAGS, htons(priv->flags)))
  77. goto nla_put_failure;
  78. return 0;
  79. nla_put_failure:
  80. return -1;
  81. }
  82. static struct nft_expr_type nft_queue_type;
  83. static const struct nft_expr_ops nft_queue_ops = {
  84. .type = &nft_queue_type,
  85. .size = NFT_EXPR_SIZE(sizeof(struct nft_queue)),
  86. .eval = nft_queue_eval,
  87. .init = nft_queue_init,
  88. .dump = nft_queue_dump,
  89. };
  90. static struct nft_expr_type nft_queue_type __read_mostly = {
  91. .name = "queue",
  92. .ops = &nft_queue_ops,
  93. .policy = nft_queue_policy,
  94. .maxattr = NFTA_QUEUE_MAX,
  95. .owner = THIS_MODULE,
  96. };
  97. static int __init nft_queue_module_init(void)
  98. {
  99. return nft_register_expr(&nft_queue_type);
  100. }
  101. static void __exit nft_queue_module_exit(void)
  102. {
  103. nft_unregister_expr(&nft_queue_type);
  104. }
  105. module_init(nft_queue_module_init);
  106. module_exit(nft_queue_module_exit);
  107. MODULE_LICENSE("GPL");
  108. MODULE_AUTHOR("Eric Leblond <eric@regit.org>");
  109. MODULE_ALIAS_NFT_EXPR("queue");