ip6t_NPT.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2011, 2012 Patrick McHardy <kaber@trash.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/ipv6.h>
  11. #include <net/ipv6.h>
  12. #include <linux/netfilter.h>
  13. #include <linux/netfilter_ipv6.h>
  14. #include <linux/netfilter_ipv6/ip6t_NPT.h>
  15. #include <linux/netfilter/x_tables.h>
  16. static int ip6t_npt_checkentry(const struct xt_tgchk_param *par)
  17. {
  18. struct ip6t_npt_tginfo *npt = par->targinfo;
  19. struct in6_addr pfx;
  20. __wsum src_sum, dst_sum;
  21. if (npt->src_pfx_len > 64 || npt->dst_pfx_len > 64)
  22. return -EINVAL;
  23. /* Ensure that LSB of prefix is zero */
  24. ipv6_addr_prefix(&pfx, &npt->src_pfx.in6, npt->src_pfx_len);
  25. if (!ipv6_addr_equal(&pfx, &npt->src_pfx.in6))
  26. return -EINVAL;
  27. ipv6_addr_prefix(&pfx, &npt->dst_pfx.in6, npt->dst_pfx_len);
  28. if (!ipv6_addr_equal(&pfx, &npt->dst_pfx.in6))
  29. return -EINVAL;
  30. src_sum = csum_partial(&npt->src_pfx.in6, sizeof(npt->src_pfx.in6), 0);
  31. dst_sum = csum_partial(&npt->dst_pfx.in6, sizeof(npt->dst_pfx.in6), 0);
  32. npt->adjustment = ~csum_fold(csum_sub(src_sum, dst_sum));
  33. return 0;
  34. }
  35. static bool ip6t_npt_map_pfx(const struct ip6t_npt_tginfo *npt,
  36. struct in6_addr *addr)
  37. {
  38. unsigned int pfx_len;
  39. unsigned int i, idx;
  40. __be32 mask;
  41. __sum16 sum;
  42. pfx_len = max(npt->src_pfx_len, npt->dst_pfx_len);
  43. for (i = 0; i < pfx_len; i += 32) {
  44. if (pfx_len - i >= 32)
  45. mask = 0;
  46. else
  47. mask = htonl((1 << (i - pfx_len + 32)) - 1);
  48. idx = i / 32;
  49. addr->s6_addr32[idx] &= mask;
  50. addr->s6_addr32[idx] |= ~mask & npt->dst_pfx.in6.s6_addr32[idx];
  51. }
  52. if (pfx_len <= 48)
  53. idx = 3;
  54. else {
  55. for (idx = 4; idx < ARRAY_SIZE(addr->s6_addr16); idx++) {
  56. if ((__force __sum16)addr->s6_addr16[idx] !=
  57. CSUM_MANGLED_0)
  58. break;
  59. }
  60. if (idx == ARRAY_SIZE(addr->s6_addr16))
  61. return false;
  62. }
  63. sum = ~csum_fold(csum_add(csum_unfold((__force __sum16)addr->s6_addr16[idx]),
  64. csum_unfold(npt->adjustment)));
  65. if (sum == CSUM_MANGLED_0)
  66. sum = 0;
  67. *(__force __sum16 *)&addr->s6_addr16[idx] = sum;
  68. return true;
  69. }
  70. static unsigned int
  71. ip6t_snpt_tg(struct sk_buff *skb, const struct xt_action_param *par)
  72. {
  73. const struct ip6t_npt_tginfo *npt = par->targinfo;
  74. if (!ip6t_npt_map_pfx(npt, &ipv6_hdr(skb)->saddr)) {
  75. icmpv6_send(skb, ICMPV6_PARAMPROB, ICMPV6_HDR_FIELD,
  76. offsetof(struct ipv6hdr, saddr));
  77. return NF_DROP;
  78. }
  79. return XT_CONTINUE;
  80. }
  81. static unsigned int
  82. ip6t_dnpt_tg(struct sk_buff *skb, const struct xt_action_param *par)
  83. {
  84. const struct ip6t_npt_tginfo *npt = par->targinfo;
  85. if (!ip6t_npt_map_pfx(npt, &ipv6_hdr(skb)->daddr)) {
  86. icmpv6_send(skb, ICMPV6_PARAMPROB, ICMPV6_HDR_FIELD,
  87. offsetof(struct ipv6hdr, daddr));
  88. return NF_DROP;
  89. }
  90. return XT_CONTINUE;
  91. }
  92. static struct xt_target ip6t_npt_target_reg[] __read_mostly = {
  93. {
  94. .name = "SNPT",
  95. .table = "mangle",
  96. .target = ip6t_snpt_tg,
  97. .targetsize = sizeof(struct ip6t_npt_tginfo),
  98. .checkentry = ip6t_npt_checkentry,
  99. .family = NFPROTO_IPV6,
  100. .hooks = (1 << NF_INET_LOCAL_IN) |
  101. (1 << NF_INET_POST_ROUTING),
  102. .me = THIS_MODULE,
  103. },
  104. {
  105. .name = "DNPT",
  106. .table = "mangle",
  107. .target = ip6t_dnpt_tg,
  108. .targetsize = sizeof(struct ip6t_npt_tginfo),
  109. .checkentry = ip6t_npt_checkentry,
  110. .family = NFPROTO_IPV6,
  111. .hooks = (1 << NF_INET_PRE_ROUTING) |
  112. (1 << NF_INET_LOCAL_OUT),
  113. .me = THIS_MODULE,
  114. },
  115. };
  116. static int __init ip6t_npt_init(void)
  117. {
  118. return xt_register_targets(ip6t_npt_target_reg,
  119. ARRAY_SIZE(ip6t_npt_target_reg));
  120. }
  121. static void __exit ip6t_npt_exit(void)
  122. {
  123. xt_unregister_targets(ip6t_npt_target_reg,
  124. ARRAY_SIZE(ip6t_npt_target_reg));
  125. }
  126. module_init(ip6t_npt_init);
  127. module_exit(ip6t_npt_exit);
  128. MODULE_LICENSE("GPL");
  129. MODULE_DESCRIPTION("IPv6-to-IPv6 Network Prefix Translation (RFC 6296)");
  130. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  131. MODULE_ALIAS("ip6t_SNPT");
  132. MODULE_ALIAS("ip6t_DNPT");