xt_NFQUEUE.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* iptables module for using new netfilter netlink queue
  2. *
  3. * (C) 2005 by Harald Welte <laforge@netfilter.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. */
  10. #include <linux/module.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/netfilter.h>
  13. #include <linux/netfilter_arp.h>
  14. #include <linux/netfilter/x_tables.h>
  15. #include <linux/netfilter/xt_NFQUEUE.h>
  16. #include <net/netfilter/nf_queue.h>
  17. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  18. MODULE_DESCRIPTION("Xtables: packet forwarding to netlink");
  19. MODULE_LICENSE("GPL");
  20. MODULE_ALIAS("ipt_NFQUEUE");
  21. MODULE_ALIAS("ip6t_NFQUEUE");
  22. MODULE_ALIAS("arpt_NFQUEUE");
  23. static u32 jhash_initval __read_mostly;
  24. static unsigned int
  25. nfqueue_tg(struct sk_buff *skb, const struct xt_action_param *par)
  26. {
  27. const struct xt_NFQ_info *tinfo = par->targinfo;
  28. return NF_QUEUE_NR(tinfo->queuenum);
  29. }
  30. static unsigned int
  31. nfqueue_tg_v1(struct sk_buff *skb, const struct xt_action_param *par)
  32. {
  33. const struct xt_NFQ_info_v1 *info = par->targinfo;
  34. u32 queue = info->queuenum;
  35. if (info->queues_total > 1) {
  36. queue = nfqueue_hash(skb, queue, info->queues_total,
  37. par->family, jhash_initval);
  38. }
  39. return NF_QUEUE_NR(queue);
  40. }
  41. static unsigned int
  42. nfqueue_tg_v2(struct sk_buff *skb, const struct xt_action_param *par)
  43. {
  44. const struct xt_NFQ_info_v2 *info = par->targinfo;
  45. unsigned int ret = nfqueue_tg_v1(skb, par);
  46. if (info->bypass)
  47. ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
  48. return ret;
  49. }
  50. static int nfqueue_tg_check(const struct xt_tgchk_param *par)
  51. {
  52. const struct xt_NFQ_info_v3 *info = par->targinfo;
  53. u32 maxid;
  54. init_hashrandom(&jhash_initval);
  55. if (info->queues_total == 0) {
  56. pr_err("NFQUEUE: number of total queues is 0\n");
  57. return -EINVAL;
  58. }
  59. maxid = info->queues_total - 1 + info->queuenum;
  60. if (maxid > 0xffff) {
  61. pr_err("NFQUEUE: number of queues (%u) out of range (got %u)\n",
  62. info->queues_total, maxid);
  63. return -ERANGE;
  64. }
  65. if (par->target->revision == 2 && info->flags > 1)
  66. return -EINVAL;
  67. if (par->target->revision == 3 && info->flags & ~NFQ_FLAG_MASK)
  68. return -EINVAL;
  69. return 0;
  70. }
  71. static unsigned int
  72. nfqueue_tg_v3(struct sk_buff *skb, const struct xt_action_param *par)
  73. {
  74. const struct xt_NFQ_info_v3 *info = par->targinfo;
  75. u32 queue = info->queuenum;
  76. int ret;
  77. if (info->queues_total > 1) {
  78. if (info->flags & NFQ_FLAG_CPU_FANOUT) {
  79. int cpu = smp_processor_id();
  80. queue = info->queuenum + cpu % info->queues_total;
  81. } else {
  82. queue = nfqueue_hash(skb, queue, info->queues_total,
  83. par->family, jhash_initval);
  84. }
  85. }
  86. ret = NF_QUEUE_NR(queue);
  87. if (info->flags & NFQ_FLAG_BYPASS)
  88. ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
  89. return ret;
  90. }
  91. static struct xt_target nfqueue_tg_reg[] __read_mostly = {
  92. {
  93. .name = "NFQUEUE",
  94. .family = NFPROTO_UNSPEC,
  95. .target = nfqueue_tg,
  96. .targetsize = sizeof(struct xt_NFQ_info),
  97. .me = THIS_MODULE,
  98. },
  99. {
  100. .name = "NFQUEUE",
  101. .revision = 1,
  102. .family = NFPROTO_UNSPEC,
  103. .checkentry = nfqueue_tg_check,
  104. .target = nfqueue_tg_v1,
  105. .targetsize = sizeof(struct xt_NFQ_info_v1),
  106. .me = THIS_MODULE,
  107. },
  108. {
  109. .name = "NFQUEUE",
  110. .revision = 2,
  111. .family = NFPROTO_UNSPEC,
  112. .checkentry = nfqueue_tg_check,
  113. .target = nfqueue_tg_v2,
  114. .targetsize = sizeof(struct xt_NFQ_info_v2),
  115. .me = THIS_MODULE,
  116. },
  117. {
  118. .name = "NFQUEUE",
  119. .revision = 3,
  120. .family = NFPROTO_UNSPEC,
  121. .checkentry = nfqueue_tg_check,
  122. .target = nfqueue_tg_v3,
  123. .targetsize = sizeof(struct xt_NFQ_info_v3),
  124. .me = THIS_MODULE,
  125. },
  126. };
  127. static int __init nfqueue_tg_init(void)
  128. {
  129. return xt_register_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
  130. }
  131. static void __exit nfqueue_tg_exit(void)
  132. {
  133. xt_unregister_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
  134. }
  135. module_init(nfqueue_tg_init);
  136. module_exit(nfqueue_tg_exit);