libip6t_NETMAP.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
  3. *
  4. * Based on Svenning Soerensen's IPv4 NETMAP target. Development of IPv6 NAT
  5. * funded by Astaro.
  6. */
  7. #include <stdio.h>
  8. #include <netdb.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <getopt.h>
  12. #include <xtables.h>
  13. #include <libiptc/libip6tc.h>
  14. #include <linux/netfilter/nf_nat.h>
  15. #define MODULENAME "NETMAP"
  16. enum {
  17. O_TO = 0,
  18. };
  19. static const struct xt_option_entry NETMAP_opts[] = {
  20. {.name = "to", .id = O_TO, .type = XTTYPE_HOSTMASK,
  21. .flags = XTOPT_MAND},
  22. XTOPT_TABLEEND,
  23. };
  24. static void NETMAP_help(void)
  25. {
  26. printf(MODULENAME" target options:\n"
  27. " --%s address[/mask]\n"
  28. " Network address to map to.\n\n",
  29. NETMAP_opts[0].name);
  30. }
  31. static void NETMAP_parse(struct xt_option_call *cb)
  32. {
  33. struct nf_nat_range *range = cb->data;
  34. unsigned int i;
  35. xtables_option_parse(cb);
  36. range->flags |= NF_NAT_RANGE_MAP_IPS;
  37. for (i = 0; i < 4; i++) {
  38. range->min_addr.ip6[i] = cb->val.haddr.ip6[i] &
  39. cb->val.hmask.ip6[i];
  40. range->max_addr.ip6[i] = range->min_addr.ip6[i] |
  41. ~cb->val.hmask.ip6[i];
  42. }
  43. }
  44. static void NETMAP_print(const void *ip, const struct xt_entry_target *target,
  45. int numeric)
  46. {
  47. const struct nf_nat_range *r = (const void *)target->data;
  48. struct in6_addr a;
  49. unsigned int i;
  50. int bits;
  51. a = r->min_addr.in6;
  52. printf("%s", xtables_ip6addr_to_numeric(&a));
  53. for (i = 0; i < 4; i++)
  54. a.s6_addr32[i] = ~(r->min_addr.ip6[i] ^ r->max_addr.ip6[i]);
  55. bits = ipv6_prefix_length(&a);
  56. if (bits < 0)
  57. printf("/%s", xtables_ip6addr_to_numeric(&a));
  58. else
  59. printf("/%d", bits);
  60. }
  61. static void NETMAP_save(const void *ip, const struct xt_entry_target *target)
  62. {
  63. printf(" --%s ", NETMAP_opts[0].name);
  64. NETMAP_print(ip, target, 0);
  65. }
  66. static struct xtables_target netmap_tg_reg = {
  67. .name = MODULENAME,
  68. .version = XTABLES_VERSION,
  69. .family = NFPROTO_IPV6,
  70. .size = XT_ALIGN(sizeof(struct nf_nat_range)),
  71. .userspacesize = XT_ALIGN(sizeof(struct nf_nat_range)),
  72. .help = NETMAP_help,
  73. .x6_parse = NETMAP_parse,
  74. .print = NETMAP_print,
  75. .save = NETMAP_save,
  76. .x6_options = NETMAP_opts,
  77. };
  78. void _init(void)
  79. {
  80. xtables_register_target(&netmap_tg_reg);
  81. }