xt_REDIRECT.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * (C) 1999-2001 Paul `Rusty' Russell
  4. * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
  5. * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
  6. *
  7. * Based on Rusty Russell's IPv4 REDIRECT target. Development of IPv6
  8. * NAT funded by Astaro.
  9. */
  10. #include <linux/if.h>
  11. #include <linux/inetdevice.h>
  12. #include <linux/ip.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/netfilter.h>
  17. #include <linux/types.h>
  18. #include <linux/netfilter_ipv4.h>
  19. #include <linux/netfilter_ipv6.h>
  20. #include <linux/netfilter/x_tables.h>
  21. #include <net/addrconf.h>
  22. #include <net/checksum.h>
  23. #include <net/protocol.h>
  24. #include <net/netfilter/nf_nat.h>
  25. #include <net/netfilter/nf_nat_redirect.h>
  26. static unsigned int
  27. redirect_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  28. {
  29. return nf_nat_redirect_ipv6(skb, par->targinfo, xt_hooknum(par));
  30. }
  31. static int redirect_tg6_checkentry(const struct xt_tgchk_param *par)
  32. {
  33. const struct nf_nat_range2 *range = par->targinfo;
  34. if (range->flags & NF_NAT_RANGE_MAP_IPS)
  35. return -EINVAL;
  36. return nf_ct_netns_get(par->net, par->family);
  37. }
  38. static void redirect_tg_destroy(const struct xt_tgdtor_param *par)
  39. {
  40. nf_ct_netns_put(par->net, par->family);
  41. }
  42. /* FIXME: Take multiple ranges --RR */
  43. static int redirect_tg4_check(const struct xt_tgchk_param *par)
  44. {
  45. const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
  46. if (mr->range[0].flags & NF_NAT_RANGE_MAP_IPS) {
  47. pr_debug("bad MAP_IPS.\n");
  48. return -EINVAL;
  49. }
  50. if (mr->rangesize != 1) {
  51. pr_debug("bad rangesize %u.\n", mr->rangesize);
  52. return -EINVAL;
  53. }
  54. return nf_ct_netns_get(par->net, par->family);
  55. }
  56. static unsigned int
  57. redirect_tg4(struct sk_buff *skb, const struct xt_action_param *par)
  58. {
  59. return nf_nat_redirect_ipv4(skb, par->targinfo, xt_hooknum(par));
  60. }
  61. static struct xt_target redirect_tg_reg[] __read_mostly = {
  62. {
  63. .name = "REDIRECT",
  64. .family = NFPROTO_IPV6,
  65. .revision = 0,
  66. .table = "nat",
  67. .checkentry = redirect_tg6_checkentry,
  68. .destroy = redirect_tg_destroy,
  69. .target = redirect_tg6,
  70. .targetsize = sizeof(struct nf_nat_range),
  71. .hooks = (1 << NF_INET_PRE_ROUTING) |
  72. (1 << NF_INET_LOCAL_OUT),
  73. .me = THIS_MODULE,
  74. },
  75. {
  76. .name = "REDIRECT",
  77. .family = NFPROTO_IPV4,
  78. .revision = 0,
  79. .table = "nat",
  80. .target = redirect_tg4,
  81. .checkentry = redirect_tg4_check,
  82. .destroy = redirect_tg_destroy,
  83. .targetsize = sizeof(struct nf_nat_ipv4_multi_range_compat),
  84. .hooks = (1 << NF_INET_PRE_ROUTING) |
  85. (1 << NF_INET_LOCAL_OUT),
  86. .me = THIS_MODULE,
  87. },
  88. };
  89. static int __init redirect_tg_init(void)
  90. {
  91. return xt_register_targets(redirect_tg_reg,
  92. ARRAY_SIZE(redirect_tg_reg));
  93. }
  94. static void __exit redirect_tg_exit(void)
  95. {
  96. xt_unregister_targets(redirect_tg_reg, ARRAY_SIZE(redirect_tg_reg));
  97. }
  98. module_init(redirect_tg_init);
  99. module_exit(redirect_tg_exit);
  100. MODULE_LICENSE("GPL");
  101. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  102. MODULE_DESCRIPTION("Xtables: Connection redirection to localhost");
  103. MODULE_ALIAS("ip6t_REDIRECT");
  104. MODULE_ALIAS("ipt_REDIRECT");