nft_queue.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. enum nft_registers sreg_qnum:8;
  23. u16 queuenum;
  24. u16 queues_total;
  25. u16 flags;
  26. };
  27. static void nft_queue_eval(const struct nft_expr *expr,
  28. struct nft_regs *regs,
  29. const struct nft_pktinfo *pkt)
  30. {
  31. struct nft_queue *priv = nft_expr_priv(expr);
  32. u32 queue = priv->queuenum;
  33. u32 ret;
  34. if (priv->queues_total > 1) {
  35. if (priv->flags & NFT_QUEUE_FLAG_CPU_FANOUT) {
  36. int cpu = raw_smp_processor_id();
  37. queue = priv->queuenum + cpu % priv->queues_total;
  38. } else {
  39. queue = nfqueue_hash(pkt->skb, queue,
  40. priv->queues_total, pkt->pf,
  41. jhash_initval);
  42. }
  43. }
  44. ret = NF_QUEUE_NR(queue);
  45. if (priv->flags & NFT_QUEUE_FLAG_BYPASS)
  46. ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
  47. regs->verdict.code = ret;
  48. }
  49. static void nft_queue_sreg_eval(const struct nft_expr *expr,
  50. struct nft_regs *regs,
  51. const struct nft_pktinfo *pkt)
  52. {
  53. struct nft_queue *priv = nft_expr_priv(expr);
  54. u32 queue, ret;
  55. queue = regs->data[priv->sreg_qnum];
  56. ret = NF_QUEUE_NR(queue);
  57. if (priv->flags & NFT_QUEUE_FLAG_BYPASS)
  58. ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
  59. regs->verdict.code = ret;
  60. }
  61. static const struct nla_policy nft_queue_policy[NFTA_QUEUE_MAX + 1] = {
  62. [NFTA_QUEUE_NUM] = { .type = NLA_U16 },
  63. [NFTA_QUEUE_TOTAL] = { .type = NLA_U16 },
  64. [NFTA_QUEUE_FLAGS] = { .type = NLA_U16 },
  65. [NFTA_QUEUE_SREG_QNUM] = { .type = NLA_U32 },
  66. };
  67. static int nft_queue_init(const struct nft_ctx *ctx,
  68. const struct nft_expr *expr,
  69. const struct nlattr * const tb[])
  70. {
  71. struct nft_queue *priv = nft_expr_priv(expr);
  72. u32 maxid;
  73. priv->queuenum = ntohs(nla_get_be16(tb[NFTA_QUEUE_NUM]));
  74. if (tb[NFTA_QUEUE_TOTAL])
  75. priv->queues_total = ntohs(nla_get_be16(tb[NFTA_QUEUE_TOTAL]));
  76. else
  77. priv->queues_total = 1;
  78. if (priv->queues_total == 0)
  79. return -EINVAL;
  80. maxid = priv->queues_total - 1 + priv->queuenum;
  81. if (maxid > U16_MAX)
  82. return -ERANGE;
  83. if (tb[NFTA_QUEUE_FLAGS]) {
  84. priv->flags = ntohs(nla_get_be16(tb[NFTA_QUEUE_FLAGS]));
  85. if (priv->flags & ~NFT_QUEUE_FLAG_MASK)
  86. return -EINVAL;
  87. }
  88. return 0;
  89. }
  90. static int nft_queue_sreg_init(const struct nft_ctx *ctx,
  91. const struct nft_expr *expr,
  92. const struct nlattr * const tb[])
  93. {
  94. struct nft_queue *priv = nft_expr_priv(expr);
  95. int err;
  96. priv->sreg_qnum = nft_parse_register(tb[NFTA_QUEUE_SREG_QNUM]);
  97. err = nft_validate_register_load(priv->sreg_qnum, sizeof(u32));
  98. if (err < 0)
  99. return err;
  100. if (tb[NFTA_QUEUE_FLAGS]) {
  101. priv->flags = ntohs(nla_get_be16(tb[NFTA_QUEUE_FLAGS]));
  102. if (priv->flags & ~NFT_QUEUE_FLAG_MASK)
  103. return -EINVAL;
  104. if (priv->flags & NFT_QUEUE_FLAG_CPU_FANOUT)
  105. return -EOPNOTSUPP;
  106. }
  107. return 0;
  108. }
  109. static int nft_queue_dump(struct sk_buff *skb, const struct nft_expr *expr)
  110. {
  111. const struct nft_queue *priv = nft_expr_priv(expr);
  112. if (nla_put_be16(skb, NFTA_QUEUE_NUM, htons(priv->queuenum)) ||
  113. nla_put_be16(skb, NFTA_QUEUE_TOTAL, htons(priv->queues_total)) ||
  114. nla_put_be16(skb, NFTA_QUEUE_FLAGS, htons(priv->flags)))
  115. goto nla_put_failure;
  116. return 0;
  117. nla_put_failure:
  118. return -1;
  119. }
  120. static int
  121. nft_queue_sreg_dump(struct sk_buff *skb, const struct nft_expr *expr)
  122. {
  123. const struct nft_queue *priv = nft_expr_priv(expr);
  124. if (nft_dump_register(skb, NFTA_QUEUE_SREG_QNUM, priv->sreg_qnum) ||
  125. nla_put_be16(skb, NFTA_QUEUE_FLAGS, htons(priv->flags)))
  126. goto nla_put_failure;
  127. return 0;
  128. nla_put_failure:
  129. return -1;
  130. }
  131. static struct nft_expr_type nft_queue_type;
  132. static const struct nft_expr_ops nft_queue_ops = {
  133. .type = &nft_queue_type,
  134. .size = NFT_EXPR_SIZE(sizeof(struct nft_queue)),
  135. .eval = nft_queue_eval,
  136. .init = nft_queue_init,
  137. .dump = nft_queue_dump,
  138. };
  139. static const struct nft_expr_ops nft_queue_sreg_ops = {
  140. .type = &nft_queue_type,
  141. .size = NFT_EXPR_SIZE(sizeof(struct nft_queue)),
  142. .eval = nft_queue_sreg_eval,
  143. .init = nft_queue_sreg_init,
  144. .dump = nft_queue_sreg_dump,
  145. };
  146. static const struct nft_expr_ops *
  147. nft_queue_select_ops(const struct nft_ctx *ctx,
  148. const struct nlattr * const tb[])
  149. {
  150. if (tb[NFTA_QUEUE_NUM] && tb[NFTA_QUEUE_SREG_QNUM])
  151. return ERR_PTR(-EINVAL);
  152. init_hashrandom(&jhash_initval);
  153. if (tb[NFTA_QUEUE_NUM])
  154. return &nft_queue_ops;
  155. if (tb[NFTA_QUEUE_SREG_QNUM])
  156. return &nft_queue_sreg_ops;
  157. return ERR_PTR(-EINVAL);
  158. }
  159. static struct nft_expr_type nft_queue_type __read_mostly = {
  160. .name = "queue",
  161. .select_ops = &nft_queue_select_ops,
  162. .policy = nft_queue_policy,
  163. .maxattr = NFTA_QUEUE_MAX,
  164. .owner = THIS_MODULE,
  165. };
  166. static int __init nft_queue_module_init(void)
  167. {
  168. return nft_register_expr(&nft_queue_type);
  169. }
  170. static void __exit nft_queue_module_exit(void)
  171. {
  172. nft_unregister_expr(&nft_queue_type);
  173. }
  174. module_init(nft_queue_module_init);
  175. module_exit(nft_queue_module_exit);
  176. MODULE_LICENSE("GPL");
  177. MODULE_AUTHOR("Eric Leblond <eric@regit.org>");
  178. MODULE_ALIAS_NFT_EXPR("queue");