xt_MASQUERADE.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Masquerade. Simple mapping which alters range to a local IP address
  3. (depending on route). */
  4. /* (C) 1999-2001 Paul `Rusty' Russell
  5. * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/netfilter/x_tables.h>
  10. #include <net/netfilter/nf_nat.h>
  11. #include <net/netfilter/nf_nat_masquerade.h>
  12. MODULE_LICENSE("GPL");
  13. MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  14. MODULE_DESCRIPTION("Xtables: automatic-address SNAT");
  15. /* FIXME: Multiple targets. --RR */
  16. static int masquerade_tg_check(const struct xt_tgchk_param *par)
  17. {
  18. const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
  19. if (mr->range[0].flags & NF_NAT_RANGE_MAP_IPS) {
  20. pr_debug("bad MAP_IPS.\n");
  21. return -EINVAL;
  22. }
  23. if (mr->rangesize != 1) {
  24. pr_debug("bad rangesize %u\n", mr->rangesize);
  25. return -EINVAL;
  26. }
  27. return nf_ct_netns_get(par->net, par->family);
  28. }
  29. static unsigned int
  30. masquerade_tg(struct sk_buff *skb, const struct xt_action_param *par)
  31. {
  32. struct nf_nat_range2 range;
  33. const struct nf_nat_ipv4_multi_range_compat *mr;
  34. mr = par->targinfo;
  35. range.flags = mr->range[0].flags;
  36. range.min_proto = mr->range[0].min;
  37. range.max_proto = mr->range[0].max;
  38. return nf_nat_masquerade_ipv4(skb, xt_hooknum(par), &range,
  39. xt_out(par));
  40. }
  41. static void masquerade_tg_destroy(const struct xt_tgdtor_param *par)
  42. {
  43. nf_ct_netns_put(par->net, par->family);
  44. }
  45. #if IS_ENABLED(CONFIG_IPV6)
  46. static unsigned int
  47. masquerade_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  48. {
  49. return nf_nat_masquerade_ipv6(skb, par->targinfo, xt_out(par));
  50. }
  51. static int masquerade_tg6_checkentry(const struct xt_tgchk_param *par)
  52. {
  53. const struct nf_nat_range2 *range = par->targinfo;
  54. if (range->flags & NF_NAT_RANGE_MAP_IPS)
  55. return -EINVAL;
  56. return nf_ct_netns_get(par->net, par->family);
  57. }
  58. #endif
  59. static struct xt_target masquerade_tg_reg[] __read_mostly = {
  60. {
  61. #if IS_ENABLED(CONFIG_IPV6)
  62. .name = "MASQUERADE",
  63. .family = NFPROTO_IPV6,
  64. .target = masquerade_tg6,
  65. .targetsize = sizeof(struct nf_nat_range),
  66. .table = "nat",
  67. .hooks = 1 << NF_INET_POST_ROUTING,
  68. .checkentry = masquerade_tg6_checkentry,
  69. .destroy = masquerade_tg_destroy,
  70. .me = THIS_MODULE,
  71. }, {
  72. #endif
  73. .name = "MASQUERADE",
  74. .family = NFPROTO_IPV4,
  75. .target = masquerade_tg,
  76. .targetsize = sizeof(struct nf_nat_ipv4_multi_range_compat),
  77. .table = "nat",
  78. .hooks = 1 << NF_INET_POST_ROUTING,
  79. .checkentry = masquerade_tg_check,
  80. .destroy = masquerade_tg_destroy,
  81. .me = THIS_MODULE,
  82. }
  83. };
  84. static int __init masquerade_tg_init(void)
  85. {
  86. int ret;
  87. ret = xt_register_targets(masquerade_tg_reg,
  88. ARRAY_SIZE(masquerade_tg_reg));
  89. if (ret)
  90. return ret;
  91. ret = nf_nat_masquerade_inet_register_notifiers();
  92. if (ret) {
  93. xt_unregister_targets(masquerade_tg_reg,
  94. ARRAY_SIZE(masquerade_tg_reg));
  95. return ret;
  96. }
  97. return ret;
  98. }
  99. static void __exit masquerade_tg_exit(void)
  100. {
  101. xt_unregister_targets(masquerade_tg_reg, ARRAY_SIZE(masquerade_tg_reg));
  102. nf_nat_masquerade_inet_unregister_notifiers();
  103. }
  104. module_init(masquerade_tg_init);
  105. module_exit(masquerade_tg_exit);
  106. #if IS_ENABLED(CONFIG_IPV6)
  107. MODULE_ALIAS("ip6t_MASQUERADE");
  108. #endif
  109. MODULE_ALIAS("ipt_MASQUERADE");