ipt_MASQUERADE.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Masquerade. Simple mapping which alters range to a local IP address
  2. (depending on route). */
  3. /* (C) 1999-2001 Paul `Rusty' Russell
  4. * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/types.h>
  12. #include <linux/inetdevice.h>
  13. #include <linux/ip.h>
  14. #include <linux/timer.h>
  15. #include <linux/module.h>
  16. #include <linux/netfilter.h>
  17. #include <net/protocol.h>
  18. #include <net/ip.h>
  19. #include <net/checksum.h>
  20. #include <net/route.h>
  21. #include <linux/netfilter_ipv4.h>
  22. #include <linux/netfilter/x_tables.h>
  23. #include <net/netfilter/nf_nat.h>
  24. #include <net/netfilter/ipv4/nf_nat_masquerade.h>
  25. MODULE_LICENSE("GPL");
  26. MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  27. MODULE_DESCRIPTION("Xtables: automatic-address SNAT");
  28. /* FIXME: Multiple targets. --RR */
  29. static int masquerade_tg_check(const struct xt_tgchk_param *par)
  30. {
  31. const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
  32. if (mr->range[0].flags & NF_NAT_RANGE_MAP_IPS) {
  33. pr_debug("bad MAP_IPS.\n");
  34. return -EINVAL;
  35. }
  36. if (mr->rangesize != 1) {
  37. pr_debug("bad rangesize %u\n", mr->rangesize);
  38. return -EINVAL;
  39. }
  40. return 0;
  41. }
  42. static unsigned int
  43. masquerade_tg(struct sk_buff *skb, const struct xt_action_param *par)
  44. {
  45. struct nf_nat_range range;
  46. const struct nf_nat_ipv4_multi_range_compat *mr;
  47. mr = par->targinfo;
  48. range.flags = mr->range[0].flags;
  49. range.min_proto = mr->range[0].min;
  50. range.max_proto = mr->range[0].max;
  51. return nf_nat_masquerade_ipv4(skb, par->hooknum, &range, par->out);
  52. }
  53. static struct xt_target masquerade_tg_reg __read_mostly = {
  54. .name = "MASQUERADE",
  55. .family = NFPROTO_IPV4,
  56. .target = masquerade_tg,
  57. .targetsize = sizeof(struct nf_nat_ipv4_multi_range_compat),
  58. .table = "nat",
  59. .hooks = 1 << NF_INET_POST_ROUTING,
  60. .checkentry = masquerade_tg_check,
  61. .me = THIS_MODULE,
  62. };
  63. static int __init masquerade_tg_init(void)
  64. {
  65. int ret;
  66. ret = xt_register_target(&masquerade_tg_reg);
  67. if (ret == 0)
  68. nf_nat_masquerade_ipv4_register_notifier();
  69. return ret;
  70. }
  71. static void __exit masquerade_tg_exit(void)
  72. {
  73. xt_unregister_target(&masquerade_tg_reg);
  74. nf_nat_masquerade_ipv4_unregister_notifier();
  75. }
  76. module_init(masquerade_tg_init);
  77. module_exit(masquerade_tg_exit);