xt_NETMAP.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * (C) 2000-2001 Svenning Soerensen <svenning@post5.tele.dk>
  3. * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
  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. #include <linux/ip.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/ipv6.h>
  14. #include <linux/netfilter.h>
  15. #include <linux/netfilter_ipv4.h>
  16. #include <linux/netfilter_ipv6.h>
  17. #include <linux/netfilter/x_tables.h>
  18. #include <net/netfilter/nf_nat.h>
  19. static unsigned int
  20. netmap_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  21. {
  22. const struct nf_nat_range *range = par->targinfo;
  23. struct nf_nat_range newrange;
  24. struct nf_conn *ct;
  25. enum ip_conntrack_info ctinfo;
  26. union nf_inet_addr new_addr, netmask;
  27. unsigned int i;
  28. ct = nf_ct_get(skb, &ctinfo);
  29. for (i = 0; i < ARRAY_SIZE(range->min_addr.ip6); i++)
  30. netmask.ip6[i] = ~(range->min_addr.ip6[i] ^
  31. range->max_addr.ip6[i]);
  32. if (par->hooknum == NF_INET_PRE_ROUTING ||
  33. par->hooknum == NF_INET_LOCAL_OUT)
  34. new_addr.in6 = ipv6_hdr(skb)->daddr;
  35. else
  36. new_addr.in6 = ipv6_hdr(skb)->saddr;
  37. for (i = 0; i < ARRAY_SIZE(new_addr.ip6); i++) {
  38. new_addr.ip6[i] &= ~netmask.ip6[i];
  39. new_addr.ip6[i] |= range->min_addr.ip6[i] &
  40. netmask.ip6[i];
  41. }
  42. newrange.flags = range->flags | NF_NAT_RANGE_MAP_IPS;
  43. newrange.min_addr = new_addr;
  44. newrange.max_addr = new_addr;
  45. newrange.min_proto = range->min_proto;
  46. newrange.max_proto = range->max_proto;
  47. return nf_nat_setup_info(ct, &newrange, HOOK2MANIP(par->hooknum));
  48. }
  49. static int netmap_tg6_checkentry(const struct xt_tgchk_param *par)
  50. {
  51. const struct nf_nat_range *range = par->targinfo;
  52. if (!(range->flags & NF_NAT_RANGE_MAP_IPS))
  53. return -EINVAL;
  54. return 0;
  55. }
  56. static unsigned int
  57. netmap_tg4(struct sk_buff *skb, const struct xt_action_param *par)
  58. {
  59. struct nf_conn *ct;
  60. enum ip_conntrack_info ctinfo;
  61. __be32 new_ip, netmask;
  62. const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
  63. struct nf_nat_range newrange;
  64. NF_CT_ASSERT(par->hooknum == NF_INET_PRE_ROUTING ||
  65. par->hooknum == NF_INET_POST_ROUTING ||
  66. par->hooknum == NF_INET_LOCAL_OUT ||
  67. par->hooknum == NF_INET_LOCAL_IN);
  68. ct = nf_ct_get(skb, &ctinfo);
  69. netmask = ~(mr->range[0].min_ip ^ mr->range[0].max_ip);
  70. if (par->hooknum == NF_INET_PRE_ROUTING ||
  71. par->hooknum == NF_INET_LOCAL_OUT)
  72. new_ip = ip_hdr(skb)->daddr & ~netmask;
  73. else
  74. new_ip = ip_hdr(skb)->saddr & ~netmask;
  75. new_ip |= mr->range[0].min_ip & netmask;
  76. memset(&newrange.min_addr, 0, sizeof(newrange.min_addr));
  77. memset(&newrange.max_addr, 0, sizeof(newrange.max_addr));
  78. newrange.flags = mr->range[0].flags | NF_NAT_RANGE_MAP_IPS;
  79. newrange.min_addr.ip = new_ip;
  80. newrange.max_addr.ip = new_ip;
  81. newrange.min_proto = mr->range[0].min;
  82. newrange.max_proto = mr->range[0].max;
  83. /* Hand modified range to generic setup. */
  84. return nf_nat_setup_info(ct, &newrange, HOOK2MANIP(par->hooknum));
  85. }
  86. static int netmap_tg4_check(const struct xt_tgchk_param *par)
  87. {
  88. const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
  89. if (!(mr->range[0].flags & NF_NAT_RANGE_MAP_IPS)) {
  90. pr_debug("bad MAP_IPS.\n");
  91. return -EINVAL;
  92. }
  93. if (mr->rangesize != 1) {
  94. pr_debug("bad rangesize %u.\n", mr->rangesize);
  95. return -EINVAL;
  96. }
  97. return 0;
  98. }
  99. static struct xt_target netmap_tg_reg[] __read_mostly = {
  100. {
  101. .name = "NETMAP",
  102. .family = NFPROTO_IPV6,
  103. .revision = 0,
  104. .target = netmap_tg6,
  105. .targetsize = sizeof(struct nf_nat_range),
  106. .table = "nat",
  107. .hooks = (1 << NF_INET_PRE_ROUTING) |
  108. (1 << NF_INET_POST_ROUTING) |
  109. (1 << NF_INET_LOCAL_OUT) |
  110. (1 << NF_INET_LOCAL_IN),
  111. .checkentry = netmap_tg6_checkentry,
  112. .me = THIS_MODULE,
  113. },
  114. {
  115. .name = "NETMAP",
  116. .family = NFPROTO_IPV4,
  117. .revision = 0,
  118. .target = netmap_tg4,
  119. .targetsize = sizeof(struct nf_nat_ipv4_multi_range_compat),
  120. .table = "nat",
  121. .hooks = (1 << NF_INET_PRE_ROUTING) |
  122. (1 << NF_INET_POST_ROUTING) |
  123. (1 << NF_INET_LOCAL_OUT) |
  124. (1 << NF_INET_LOCAL_IN),
  125. .checkentry = netmap_tg4_check,
  126. .me = THIS_MODULE,
  127. },
  128. };
  129. static int __init netmap_tg_init(void)
  130. {
  131. return xt_register_targets(netmap_tg_reg, ARRAY_SIZE(netmap_tg_reg));
  132. }
  133. static void netmap_tg_exit(void)
  134. {
  135. xt_unregister_targets(netmap_tg_reg, ARRAY_SIZE(netmap_tg_reg));
  136. }
  137. module_init(netmap_tg_init);
  138. module_exit(netmap_tg_exit);
  139. MODULE_LICENSE("GPL");
  140. MODULE_DESCRIPTION("Xtables: 1:1 NAT mapping of subnets");
  141. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  142. MODULE_ALIAS("ip6t_NETMAP");
  143. MODULE_ALIAS("ipt_NETMAP");